Photo by Brendan Church on Unsplash

Follow Your Gut!

But not this time…

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'))
['', '', '', '']
  • When split() is given the whitespace character to use to split the string, it splits the string on every occurrence of this whitespace.
  • When the whitespace is implicit, split() processes the string before splitting it. It cuts off the leading and trailing whitespaces and compresses the remaining whitespaces. After turning the multiple whitespaces into a single occurrence, it is used as a marker to split the string.
  • If the string is empty, split() returns an empty list.
  • When split() is given a string containing only the character to split the string with, it returns a list that is +1 longer than the initial string.

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
  • In the first example, 256 is allocated when the interpreter starts. This 256 object is then referenced as a, then b but it is the same object being referenced as two different variables.
  • In the second example, 257 is created when assigned to a, then another 257 is assigned to b. We are dealing with two different 257 objects.
  • The last case shows that object allocation is done at the same time when put on the same line. The object 257 is created once then assigned to a and b.
# 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