50 + python 3 tips & tricks
TRANSCRIPT
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 1/18
PROGRAMMING, PYTHON
50+ Python 3 Tips & TricksThese Python Gems Will Make Your Code Beautiful and Elegant
Eyal TrabelsiJul 18, 2019 · 4 min read
Here is a list of python tips and tricks to help you write an elegant Python 3 code! This
article is divided into different kinds of tricks:
Python iterable tricks.
Python comprehension tricks.
Python unpacking tricks.
Python itertools tricks.
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 2/18
Python collections tricks.
Python other tricks.
Python easter eggs.
Python tricks to understand the context.
Python Iterables tricks
Creating a sequence of numbers (zero to ten with skips).
Summing a sequence of numbers (calculating the sum of zero to ten with skips).
Checking whether any element in the sequence is Truthful (checking whether any elements
between zero and ten with skips are even).
Checking whether all elements in the sequence are Truthful (checking whether all elements
between zero and ten with skips are even).
view raw
1
2
3
python-tricks_iterables_1.py hosted with ❤ by GitHub
>>> range(0,10,2)
[0, 2, 4, 6, 8]
view raw
1
2
3
python-tricks_iterables_2.py hosted with ❤ by GitHub
>>> l = range(0,10,2)
>>> sum(l)
20
view raw
1
2
python-tricks_iterables_3.py hosted with ❤ by GitHub
>>> any(a % 2==0 for a in range(0,10,2))
True
view raw
1
2
3
python-tricks_iterables_4.py hosted with ❤ by GitHub
>>> all(a % 2==0 for a in range(0,10,2))
True
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 3/18
Cumulative summing a sequence of numbers (calculating the cumulative sum of zero to ten
with skips).
Given each iterable we construct a tuple by adding an index.
Concatenating iterable to a single string.
Combining two iterable of tuples or pivot nested iterables.
Getting min/max from iterable (with/without specific function).
view raw
1
2
3
4
python-tricks_iterables_5.py hosted with ❤ by GitHub
>>> import numpy as np
>>> res = list(np.cumsum(range(0,10,2)))
>>> res
[ 0, 2, 6, 12, 20]
view raw
1
2
3
python-tricks_iterables_3.py hosted with ❤ by GitHub
>>> a = ['Hello', 'world', '!']
>>> list(enumerate(a))
[(0, 'Hello'), (1, 'world'), (2, '!')]
view raw
1
2
3
python-tricks_iterables_5.py hosted with ❤ by GitHub
>>> a = ["python","really", "rocks"]
>>> " ".join(a)
'python really rocks'
view raw
1
2
3
4
5
6
7
8
9
10
python-tricks_comprehension_6.py hosted with ❤ by GitHub
# Combining two iterables
>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> z = zip(a, b)
>>> z
[(1, 'a'), (2, 'b'), (3, 'c')]
# Pivoting list of tuples
>>> zip(*z)
[(1, 2, 3), ('a', 'b', 'c')]
1 # Getting maximum from iterable
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 4/18
Getting sorted iterable (can sort by “compare” function).
Splitting a single string to list.
Initializing a list filled with some repetitive number.
Merging/Upserting two dictionaries.
view raw
2
3
4
5
6
7
8
9
10
11
12
python-tricks_iterables_7.py hosted with ❤ by GitHub
>>> a = [1, 2, -3]
>>> max(a)
2
# Getting maximum from iterable
>>> min(a)
1
# Bot min/max has key value to allow to get maximum by appliing function
>>> max(a,key=abs)
3
view raw
1
2
3
4
5
6
python-tricks_iterables_8.py hosted with ❤ by GitHub
>>> a = [1, 2, -3]
>>> sorted(a)
[-3, 1, 2]
>>> sorted(a,key=abs)
[1, 2, -3]
view raw
1
2
3
python-tricks_iterables_9.py hosted with ❤ by GitHub
>>> s = "a,b,c"
>>> s.split(",")
["a", "b", "c"]
view raw
1
2
python-tricks_iterables_10.py hosted with ❤ by GitHub
>> [1]* 10
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
1
2
3
>>> a = {"a":1, "b":1}
>>> b = {"b":2, "c":1}
>>> a.update(b)
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 5/18
Naming and saving slices of iterables.
Finding the index of an item in a list.
Finding the index of the min/max item in an iterable.
Rotating iterable by k elements.
Removing useless characters on the end/start/both of your string.
view raw
4
5
python-tricks_iterables_11.py hosted with ❤ by GitHub
>>> a
{"a":1, "b":2, "c":1}
view raw
1
2
3
4
5
6
7
8
python-tricks_iterables_13.py hosted with ❤ by GitHub
# Naming slices (slice(start, end, step))
>>> a = [0, 1, 2, 3, 4, 5]
>>> LASTTHREE = slice(-3, None)
>>> LASTTHREE
slice(-3, None, None)
>>> a[LASTTHREE]
[3, 4, 5]
view raw
1
2
3
python-tricks_iterables_14.py hosted with ❤ by GitHub
>>> a = ["foo", "bar", "baz"]
>>> a.index("bar")
1
view raw
1
2
3
python-tricks_iterables_15.py hosted with ❤ by GitHub
>>> a = [2, 3, 1]
>>> min(enumerate(a),key=lambda x: x[1])[0]
2
view raw
1
2
3
4
python-tricks_iterables_16.py hosted with ❤ by GitHub
>>> a = [1, 2, 3, 4]
>>> k = 2
>>> a[-2:] + a[:-2]
[3, 4, 1, 2]
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 6/18
Reversing an iterable wit order (string, list etc).
Python branching tricks
Multiple predicates short-cut.
For-else construct useful when searched for something and find it.
view raw
1
2
3
4
5
6
7
python-tricks_iterables_17.py hosted with ❤ by GitHub
>>> name = "//George//"
>>> name.strip("/")
'George'
>>> name.rstrip("/")
'//George'
>>> name.lstrip("/")
'George//'
view raw
1
2
3
4
5
6
7
8
9
python-tricks_iterables_18.py hosted with ❤ by GitHub
# Reversing string
>>> s = "abc"
>>> s[::-1]
"cba"
# Reversing list
>>> l = ["a", "b", "c"]
>>> l[::-1]
["c", "b", "a"]
view raw
1
2
3
python-tricks_predicate_2.py hosted with ❤ by GitHub
>>> n = 10
>>> 1 < n < 20
True
1
2
3
4
5
6
7
8
9
# For example assume that I need to search through a list and process each item until a flag ite
# then stop processing. If the flag item is missing then an exception needs to be raised.
for i in mylist:
if i == theflag:
break
process(i)
else:
raise ValueError("List argument missing terminal flag.")
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 7/18
Trenary operator.
Try-catch-else construct.
While-else construct.
Python comprehensions tricks
List comprehension.
view rawpython-tricks_predicate_3.py hosted with ❤ by GitHub
view raw
1
2
python-tricks_predicate_1.py hosted with ❤ by GitHub
>>> "Python ROCK" if True else " I AM GRUMPY"
"Python ROCK"
view raw
1
2
3
4
5
6
7
8
python-tricks_predicate_4.py hosted with ❤ by GitHub
try:
foo()
except Exception:
print("Exception occured")
else:
print("Exception didnt occur")
finally:
print("Always gets here")
view raw
1
2
3
4
5
6
7
8
9
python-tricks_predicate_6.py hosted with ❤ by GitHub
i = 5
while i > 1:
print("Whil-ing away!")
i -= 1
if i == 3:
break
else:
print("Finished up!")
view raw
1
2
3
python-tricks_comprehension_2.py hosted with ❤ by GitHub
>>> m = [x ** 2 for x in range(5)]
>>> m
[0, 1, 4, 9, 16]
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 8/18
Set comprehension.
Dict comprehension.
Generator comprehension.
List comprehension with the current and previous value.
Note: all comprehension can use predicates with if statement.
Python unpacking tricks
view raw
1
2
3
python-tricks_comprehension_3.py hosted with ❤ by GitHub
>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}
view raw
1
2
3
python-tricks_comprehension_1.py hosted with ❤ by GitHub
>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
view raw
1
2
3
4
5
6
7
8
9
10
11
12
python-tricks_comprehension_4.py hosted with ❤ by GitHub
# A generator comprehension is the lazy version of a list comprehension.
>>> m = (x ** 2 for x in range(5))
>>> m
<generator object <genexpr> at 0x108efe408>
>>> list(m)
[0, 1, 4, 9, 16]
>>> m = (x ** 2 for x in range(5))
>>> next(m)
0
>>> list(m)
[1, 4, 9, 16]
view raw
1
2
3
python-tricks_comprehension_7.py hosted with ❤ by GitHub
>>> a = [1, 2, 4,2]
>>> [y - x for x,y in zip(a,a[1:])]
[1, 2, -2]
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 9/18
Unpack variables from iterable.
Swap variables values.
Unpack variables from iterable without indicating all elements.
Unpack variables using the splat operator.
view raw
1
2
3
4
5
6
7
8
python-tricks_unpacking_1.py hosted with ❤ by GitHub
# One can unpack all iterables (tuples, list etc)
>>> a, b, c = 1, 2, 3
>>> a, b, c
(1, 2, 3)
>>> a, b, c = [1, 2, 3]
>>> a, b, c
(1, 2, 3)
view raw
1
2
3
4
python-tricks_unpacking_2.py hosted with ❤ by GitHub
>>> a, b = 1, 2
>>> a, b = b, a
>>> a, b
(2, 1)
view raw
1
2
3
4
5
6
7
python-tricks_unpacking_3.py hosted with ❤ by GitHub
>>> a, *b, c = [1, 2, 3, 4, 5]
>>> a
1
>>> b
[2, 3, 4]
>>> c
5
view raw
1
2
3
4
5
6
python-tricks_unpacking_4.py hosted with ❤ by GitHub
>>> def test(x, y, z):
>>> print(x, y, z)
>>> res = test(*[10, 20, 30])
10 20 30
>>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
10 20 30
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 10/18
Python Itertools tricks
Flatten iterables.
Creating cartesian products from iterables.
Creating permutation from iterable.
Creating ngram from iterable.
view raw
1
2
3
python-tricks_itertools_1.py hosted with ❤ by GitHub
>>> a = [[1, 2], [3, 4], [5, 6]]
>>> list(itertools.chain.from_iterable(a))
[1, 2, 3, 4, 5, 6]
view raw
1
2
3
4
5
6
7
8
9
python-tricks_itertools_2.py hosted with ❤ by GitHub
>>> for p in itertools.product([1, 2, 3], [4, 5]):
>>> print(''.join(str(x) for x in p))
(1, 4)
(1, 5)
(2, 4)
(2, 5)
(3, 4)
(3, 5)
view raw
1
2
3
4
5
6
7
8
python-tricks_itertools_3.py hosted with ❤ by GitHub
>>> for p in itertools.permutations([1, 2, 3, 4]):
>>> print(''.join(str(x) for x in p))
123
132
213
231
312
321
1
2
3
4
5
6
7
>>> from itertools import islice
>>> def n_grams(a, n):
... z = (islice(a, i, None) for i in range(n))
... return zip(*z)
...
>>> a = [1, 2, 3, 4, 5, 6]
>>> ( 3)
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 11/18
Combining two iterables of tuples with padding or pivot nested iterable with padding.
Creating a combination of k things from an iterable of n
Creating accumulated results of iterable given a function
Creating an iterator that returns elements from the iterable as long as the predicate is true
view raw
7
8
9
10
11
12
python-tricks_itertools_4.py hosted with ❤ by GitHub
>>> n_grams(a, 3)
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]
>>> n_grams(a, 2)
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
>>> n_grams(a, 4)
[(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)]
view raw
1
2
3
4
5
6
7
8
python-tricks_itertools_5.py hosted with ❤ by GitHub
>>> import itertools as it
>>> x = [1, 2, 3, 4, 5]
>>> y = ['a', 'b', 'c']
>>> list(zip(x, y))
[(1, 'a'), (2, 'b'), (3, 'c')]
>>> list(it.zip_longest(x, y))
[(1, 'a'), (2, 'b'), (3, 'c'), (4, None), (5, None)]
view raw
1
2
3
4
python-tricks_itertools_6.py hosted with ❤ by GitHub
>>> import itertools
>>> bills = [20, 20, 20, 10, 10, 10, 10, 10, 5, 5, 1, 1, 1, 1, 1]
>>> list(itertools.combinations(bills, 3))
[(20, 20, 20), (20, 20, 10), (20, 20, 10), ... ]
view raw
1
2
3
python-tricks_itertools_7.py hosted with ❤ by GitHub
>>> import itertools
>>> list(itertools.accumulate([9, 21, 17, 5, 11, 12, 2, 6], min))
[9, 9, 9, 5, 5, 5, 2, 2]
1
2
3
4
5
>>> import itertools
>>> itertools.takewhile(lambda x: x < 3, [0, 1, 2, 3, 4])
[0, 1, 2]
>>> it dropwhile(lambda x: x < 3 [0 1 2 3 4])
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 12/18
Creating an iterator that filters elements from iterable returning only those for which the
predicate is False
Creating an iterator that computes the function using arguments obtained from the
iterable of iterables
Python collections tricks
Set basic operations.
view raw
5
6
python-tricks_itertools_8.py hosted with ❤ by GitHub
>>> it.dropwhile(lambda x: x < 3, [0, 1, 2, 3, 4])
[3, 4]
view raw
1
2
3
4
python-tricks_itertools_9.py hosted with ❤ by GitHub
>>> import itertools
# keeping only false values
>>> list(itertools.filterfalse(bool, [None, False, 1, 0, 10]))
[None, False, 0]
view raw
1
2
3
4
5
python-tricks_itertools_10.py hosted with ❤ by GitHub
>>> import itertools
>>> import operator
>>> a = [(2, 6), (8, 4), (7, 3)]
>>> list(itertools.starmap(operator.mul, a))
[12, 32, 21]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>> A = {1, 2, 3, 3}
>>> A
set([1, 2, 3])
>>> B = {3, 4, 5, 6, 7}
>>> B
set([3, 4, 5, 6, 7])
>>> A | B
set([1, 2, 3, 4, 5, 6, 7])
>>> A & B
set([3])
>>> A - B
set([1, 2])
>>> B - A
set([4, 5, 6, 7])
>>> A ^ B
set([1, 2, 4, 5, 6, 7])
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 13/18
Counter data structure (an unordered collection where elements are stored as dictionary
keys and their counts are stored as dictionary values).
https://docs.python.org/3/library/collections.html#collections.Counter
Default dictionary structure (a subclass of dictionary that retrieves default value when
non-existing key getting accessed).
view raw
17
18
python-tricks_collection_1.py hosted with ❤ by GitHub
>>> (A ^ B) == ((A - B) | (B - A))
True
view raw
1
2
3
4
5
6
7
8
9
python-tricks_collection_2.py hosted with ❤ by GitHub
import collections
>>> A = collections.Counter([1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7])
>>> A
Counter({3: 4, 1: 2, 2: 2, 4: 1, 5: 1, 6: 1, 7: 1})
>>> A.most_common(1)
[(3, 4)]
>>> A.most_common(3)
[(3, 4), (1, 2), (2, 2)]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> import collections
>>> m = collections.defaultdict(int)
>>> m['a']
0
>>> m = collections.defaultdict(str)
>>> m['a']
''
>>> m['b'] += 'a'
>>> m['b']
'a'
>>> m = collections.defaultdict(lambda: '[default value]')
>>> m['a']
'[default value]'
>>> m['b']
'[default value]'
>>> m = collections.defaultdict(list)
>>> m['a']
[]
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 14/18
https://docs.python.org/3/library/collections.html#collections.defaultdict
Ordered dict structure (a subclass of dictionary that keeps order).
https://docs.python.org/3/library/collections.html#collections.OrderedDict
Deques structure (Deques are a generalization of stacks and queues).
view rawpython-tricks_collection_3.py hosted with ❤ by GitHub
view raw
1
2
3
4
5
6
7
8
9
10
python-tricks_collection_5.py hosted with ❤ by GitHub
>>> from collections import OrderedDict
>>> d = OrderedDict.fromkeys('abcde')
>>> d.move_to_end('b')
>>> ''.join(d.keys())
'acdeb'
>>> d.move_to_end('b', last=False)
>>> ''.join(d.keys())
'bacde'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> import collection
>>> Q = collections.deque()
>>> Q.append(1)
>>> Q.appendleft(2)
>>> Q.extend([3, 4])
>>> Q.extendleft([5, 6])
>>> Q
deque([6, 5, 2, 1, 3, 4])
>>> Q.pop()
4
>>> Q.popleft()
6
>>> Q
deque([5, 2, 1, 3])
>>> Q.rotate(3)
>>> Q
deque([2, 1, 3, 5])
>>> Q.rotate(-3)
>>> Q
deque([5, 2, 1, 3])
>>> last_three = collections.deque(maxlen=3)
>>> for i in range(4):
... last_three.append(i)
i ' ' j i ( ( ) f i l h )
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 15/18
https://docs.python.org/3/library/collections.html#collections.deque
Named tuples structure (create tuple-like objects that have fields accessible by attribute
lookup as well as being indexable and iterable).
https://docs.python.org/3/library/collections.html#collections.namedtuple
Use A Dictionary To Store A Switch.
Data classes structure
https://docs.python.org/3/library/dataclasses.html
Other Python tricks
Generating uuid.
Memoization using LRU cache.
Suppression of expressions
https://docs.python.org/3/library/contextlib.html
view raw
25
26
27
28
29
30
31
python-tricks_collection_4.py hosted with ❤ by GitHub
... print ', '.join(str(x) for x in last_three)
...
0
0, 1
0, 1, 2
1, 2, 3
2, 3, 4
view rawpython-tricks_others_1.py hosted with ❤ by GitHub
1
2
3
4
5
6
7
# This creates a randomized 128-bit number that will almost certainly be unique.
# In fact, there are over 2¹²² possible UUIDs that can be generated. That’s over five undecillio
>>> import uuid
>>> user_id = uuid.uuid4()
>>> user_id
UUID('7c2faedd-805a-478e-bd6a-7b26210425c7')
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 16/18
Creating context managers when setup and teardown is needed
28.7. contextlib - Utilities for with-statement contexts - Python 2.7.16 documentation
Edit description
docs.python.org
An elegant way to deal with a file path (3.4≥)
https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-�le-paths-on-windows-mac-and-linux-11a072b58d5f
Implementing standard operators as functions for our classes
operator - Standard operators as functions - Python 3.7.4 documentation
The module exports a set of e�cient functions corresponding to the intrinsic operators of Python. Forexample, is…
docs.python.org
Creating decorator to separate concerns
For great long explanation visit https://gist.github.com/Zearin/2f40b7b9cfc51132851a
Using yield to create a simple iterator
yield from use cases and tricks
In practice, what are the main uses for the new "yield from"syntax in Python 3.3?
Thanks for contributing an answer to Stack Over�ow! Please be sure toanswer the question. Provide details and share…
stackover�ow.com
Python easter eggs
Anti-gravity
http://python-history.blogspot.com/2010/06/import-antigravity.html
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 17/18
The Zen of Python
Another cool Easter-eggs can be found
OrkoHunter/python-easter-eggs
Curated list of all the easter eggs and hidden jokes in Python - OrkoHunter/python-easter-eggs
github.com
Python tricks to understand the context
Explicitly mark entry point using __main__.py file
Use __main__.py
We have all seen the __init__.py �le and know its role, but what is ? I haveseen many Python projects either on…
shaneoneill.io
List object attributes
Additional information on a live object
inspect - Inspect live objects - Python 3.7.4 documentation
The module provides several useful functions to help get information about live objects such asmodules, classes…
docs.python.org
Summary
If you think I should add any more or have suggestions please do let me know in the
comments. I’ll keep on updating this blog.
view rawpython-tricks_context_1.py hosted with ❤ by GitHub
1
2
3
4
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec_
>>> dir("Hello World")
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__forma
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.
6/29/2020 50+ Python 3 Tips & Tricks - Towards AI — Multidisciplinary Science Journal - Medium
https://medium.com/towards-artificial-intelligence/50-python-3-tips-tricks-e5dbe05212d7 18/18
Sign up for Towards AI Newsletter from Towards AI — Multidisciplinary Science Journal
Towards AI publishes the best of tech, science, and engineering. Subscribe with us to receive ournewsletter right on your inbox.
Get this newsletterCreate a free Medium account to get Towards AINewsletter in your inbox.
Python Python Tricks Python3 Programming Coding
About Help Legal
Get the Medium app
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, includingcookie policy.