7 Python tricks π π»
Sep 2, 2018 Β· 1 min read
Welcome September! π
1. Reversing a list
arr = [1, 2, 3, 4, 5]arr[::-1]
# [5, 4, 3, 2, 1]
Explanation
The colons between the array squared braces are used for βslicing syntaxβ: [start : stop : step]
2. Simple server for sharing files over WiFi
python -m http.server [<port>]Default port is 8000.
3. Argument unpacking
print(*range(3), sep='.\n', end='.\n')# 0.
# 1.
# 2.
4. itertools
import itertools# My favorites
itertools.cycle('123')
itertools.permutations('123')
itertools.combinations('123')
Reference [https://docs.python.org/3/library/itertools.html]
5. Good old xkcd
import antigravityβοΈ π¦ π¦ π
6. Enums
class CardSuit:
HEARTS, SPADES, CLUBS, DIAMONDS = range(4)
CardSuit.SPADES
# 1
β₯οΈ β οΈ β£οΈ β¦οΈ
7. Moment of Zen
import thisβͺοΈ β«οΈ
What are your favorites? Comment below!