7 Python tricks 🐍 πŸ’»

Marco Montalto Monella
Sep 2, 2018 Β· 1 min read
Photo by Chris Ried on Unsplash

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!

Marco Montalto Monella

Written by

Software engineer, gymnast and runner. https://mmmarco.com