Follow Your Gut!

Booleans

Numerical values

>>> print(f"False = {False * 4}\nTrue = {True * 3}") False = 0
True = 3

Empty vs. Non-Empty

>>> d = {} 
>>> d == True
False>>> if d: # Instead of `if d == True:`
... print('not empty')
... else:
... print('empty')
empty
>>> lt = ['', 'item']
>>> lf = []
>>> print(f"lt = {'' in lt}\nlf = {'' in lf}\n[] == [''] = {[] == ['']}")lt = True
lf = False
[] == [''] = False
>>> d = {} 
>>> print(f"d == True: {d == True}\nall(d): {all(d)}\nany(d): {any(d)}")
d == True: False
all(d): True
any(d): False
>>> d = {''}
>>> print(f"d == True: {d == True}\nall(d): {all(d)}\nany(d): {any(d)}")
d == True: False
all(d): False
any(d): False

Strings

Automatic Concatenation

>>> 'Hello' 'World'
'HelloWorld'
>>> list('Hello World')
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

split()

>>> print('          Hello    '.split(' ')) 
['', '', '', '', '', '', '', '', '', '', 'Hello', '', '', '', '']
>>> print(' Hello World '.split())
['Hello', 'World']
>>> print(''.split(' '))
['']
>>> print('aaa'.split('a'))
['', '', '', '']

Integers

Special Cases

>>> float('inf') == float('inf')
True
>>> float('NaN') == float('NaN')
False

Pre-allocation

>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False
>>> a, b = 257, 257
>>> a is b
True
# Don't use temporary variables when it is not necessary
>>> def fibonacci(n):
>>> x = 0
>>> y = 1
>>> for i in range(n):
>>> print(x)
>>> t = y
>>> y = x + y
>>> x = t

# Multi assignment saves the day
>>> def fibonacci(n):
>>> x, y = 0, 1
>>> for i in range(n):
>>> print(x)
>>> x, y = y, x + y

Tuples

>>> t = 'item',
>>> type(t)
tuple
>>> t = ('item' 'other_item')
>>> print(t)
itemother_item

L0lz !!1

Data Scientist

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store