#!/usr/bin/env python
# coding: utf-8
# # Python Quick Reference by [Data School](http://www.dataschool.io/)
#
# **Related:** [GitHub repository](https://github.com/justmarkham/python-reference) and [blog post](http://www.dataschool.io/python-quick-reference/)
#
# ## Table of contents
#
# 1. Imports
# 2. Data Types
# 3. Math
# 4. Comparisons and Boolean Operations
# 5. Conditional Statements
# 6. Lists
# 7. Tuples
# 8. Strings
# 9. Dictionaries
# 10. Sets
# 11. Defining Functions
# 12. Anonymous (Lambda) Functions
# 13. For Loops and While Loops
# 14. Comprehensions
# 15. Map and Filter
# ## 1. Imports
# In[1]:
# 'generic import' of math module
import math
math.sqrt(25)
# In[2]:
# import a function
from math import sqrt
sqrt(25) # no longer have to reference the module
# In[3]:
# import multiple functions at once
from math import cos, floor
# In[4]:
# import all functions in a module (generally discouraged)
from csv import *
# In[5]:
# define an alias
import datetime as dt
# In[6]:
# show all functions in math module
print(dir(math))
# [Back to top]
# ## 2. Data Types
#
# **Determine the type of an object:**
# In[7]:
type(2)
# In[8]:
type(2.0)
# In[9]:
type('two')
# In[10]:
type(True)
# In[11]:
type(None)
# **Check if an object is of a given type:**
# In[12]:
isinstance(2.0, int)
# In[13]:
isinstance(2.0, (int, float))
# **Convert an object to a given type:**
# In[14]:
float(2)
# In[15]:
int(2.9)
# In[16]:
str(2.9)
# **Zero, `None`, and empty containers are converted to `False`:**
# In[17]:
bool(0)
# In[18]:
bool(None)
# In[19]:
bool('') # empty string
# In[20]:
bool([]) # empty list
# In[21]:
bool({}) # empty dictionary
# **Non-empty containers and non-zeros are converted to `True`:**
# In[22]:
bool(2)
# In[23]:
bool('two')
# In[24]:
bool([2])
# [Back to top]
# ## 3. Math
# In[25]:
10 + 4
# In[26]:
10 - 4
# In[27]:
10 * 4
# In[28]:
10 ** 4 # exponent
# In[29]:
5 % 4 # modulo - computes the remainder
# In[30]:
# Python 2: returns 2 (because both types are 'int')
# Python 3: returns 2.5
10 / 4
# In[31]:
10 / float(4)
# In[32]:
# force '/' in Python 2 to perform 'true division' (unnecessary in Python 3)
from __future__ import division
# In[33]:
10 / 4 # true division
# In[34]:
10 // 4 # floor division
# [Back to top]
# ## 4. Comparisons and Boolean Operations
# **Assignment statement:**
# In[35]:
x = 5
# **Comparisons:**
# In[36]:
x > 3
# In[37]:
x >= 3
# In[38]:
x != 3
# In[39]:
x == 5
# **Boolean operations:**
# In[40]:
5 > 3 and 6 > 3
# In[41]:
5 > 3 or 5 < 3
# In[42]:
not False
# In[43]:
False or not False and True # evaluation order: not, and, or
# [Back to top]
# ## 5. Conditional Statements
# In[44]:
# if statement
if x > 0:
print('positive')
# In[45]:
# if/else statement
if x > 0:
print('positive')
else:
print('zero or negative')
# In[46]:
# if/elif/else statement
if x > 0:
print('positive')
elif x == 0:
print('zero')
else:
print('negative')
# In[47]:
# single-line if statement (sometimes discouraged)
if x > 0: print('positive')
# In[48]:
# single-line if/else statement (sometimes discouraged), known as a 'ternary operator'
'positive' if x > 0 else 'zero or negative'
# [Back to top]
# ## 6. Lists
#
# - **List properties:** ordered, iterable, mutable, can contain multiple data types
# In[49]:
# create an empty list (two ways)
empty_list = []
empty_list = list()
# In[50]:
# create a list
simpsons = ['homer', 'marge', 'bart']
# **Examine a list:**
# In[51]:
# print element 0
simpsons[0]
# In[52]:
len(simpsons)
# **Modify a list (does not return the list):**
# In[53]:
# append element to end
simpsons.append('lisa')
simpsons
# In[54]:
# append multiple elements to end
simpsons.extend(['itchy', 'scratchy'])
simpsons
# In[55]:
# insert element at index 0 (shifts everything right)
simpsons.insert(0, 'maggie')
simpsons
# In[56]:
# search for first instance and remove it
simpsons.remove('bart')
simpsons
# In[57]:
# remove element 0 and return it
simpsons.pop(0)
# In[58]:
# remove element 0 (does not return it)
del simpsons[0]
simpsons
# In[59]:
# replace element 0
simpsons[0] = 'krusty'
simpsons
# In[60]:
# concatenate lists (slower than 'extend' method)
neighbors = simpsons + ['ned', 'rod', 'todd']
neighbors
# **Find elements in a list:**
# In[61]:
# counts the number of instances
simpsons.count('lisa')
# In[62]:
# returns index of first instance
simpsons.index('itchy')
# **List slicing:**
# In[63]:
weekdays = ['mon', 'tues', 'wed', 'thurs', 'fri']
# In[64]:
# element 0
weekdays[0]
# In[65]:
# elements 0 (inclusive) to 3 (exclusive)
weekdays[0:3]
# In[66]:
# starting point is implied to be 0
weekdays[:3]
# In[67]:
# elements 3 (inclusive) through the end
weekdays[3:]
# In[68]:
# last element
weekdays[-1]
# In[69]:
# every 2nd element (step by 2)
weekdays[::2]
# In[70]:
# backwards (step by -1)
weekdays[::-1]
# In[71]:
# alternative method for returning the list backwards
list(reversed(weekdays))
# **Sort a list in place (modifies but does not return the list):**
# In[72]:
simpsons.sort()
simpsons
# In[73]:
# sort in reverse
simpsons.sort(reverse=True)
simpsons
# In[74]:
# sort by a key
simpsons.sort(key=len)
simpsons
# **Return a sorted list (does not modify the original list):**
# In[75]:
sorted(simpsons)
# In[76]:
sorted(simpsons, reverse=True)
# In[77]:
sorted(simpsons, key=len)
# **Insert into an already sorted list, and keep it sorted:**
# In[78]:
num = [10, 20, 40, 50]
from bisect import insort
insort(num, 30)
num
# **Object references and copies:**
# In[79]:
# create a second reference to the same list
same_num = num
# In[80]:
# modifies both 'num' and 'same_num'
same_num[0] = 0
print(num)
print(same_num)
# In[81]:
# copy a list (two ways)
new_num = num[:]
new_num = list(num)
# **Examine objects:**
# In[82]:
num is same_num # checks whether they are the same object
# In[83]:
num is new_num
# In[84]:
num == same_num # checks whether they have the same contents
# In[85]:
num == new_num
# [Back to top]
# ## 7. Tuples
#
# - **Tuple properties:** ordered, iterable, immutable, can contain multiple data types
# - Like lists, but they don't change size
# In[86]:
# create a tuple directly
digits = (0, 1, 'two')
# In[87]:
# create a tuple from a list
digits = tuple([0, 1, 'two'])
# In[88]:
# trailing comma is required to indicate it's a tuple
zero = (0,)
# **Examine a tuple:**
# In[89]:
digits[2]
# In[90]:
len(digits)
# In[91]:
# counts the number of instances of that value
digits.count(0)
# In[92]:
# returns the index of the first instance of that value
digits.index(1)
# **Modify a tuple:**
# In[93]:
# elements of a tuple cannot be modified (this would throw an error)
# digits[2] = 2
# In[94]:
# concatenate tuples
digits = digits + (3, 4)
digits
# **Other tuple operations:**
# In[95]:
# create a single tuple with elements repeated (also works with lists)
(3, 4) * 2
# In[96]:
# sort a list of tuples
tens = [(20, 60), (10, 40), (20, 30)]
sorted(tens) # sorts by first element in tuple, then second element
# In[97]:
# tuple unpacking
bart = ('male', 10, 'simpson') # create a tuple
(sex, age, surname) = bart # assign three values at once
print(sex)
print(age)
print(surname)
# [Back to top]
# ## 8. Strings
#
# - **String properties:** iterable, immutable
# In[98]:
# convert another data type into a string
s = str(42)
s
# In[99]:
# create a string directly
s = 'I like you'
# **Examine a string:**
# In[100]:
s[0]
# In[101]:
len(s)
# **String slicing is like list slicing:**
# In[102]:
s[:6]
# In[103]:
s[7:]
# In[104]:
s[-1]
# **Basic string methods (does not modify the original string):**
# In[105]:
s.lower()
# In[106]:
s.upper()
# In[107]:
s.startswith('I')
# In[108]:
s.endswith('you')
# In[109]:
# checks whether every character in the string is a digit
s.isdigit()
# In[110]:
# returns index of first occurrence, but doesn't support regex
s.find('like')
# In[111]:
# returns -1 since not found
s.find('hate')
# In[112]:
# replaces all instances of 'like' with 'love'
s.replace('like', 'love')
# **Split a string:**
# In[113]:
# split a string into a list of substrings separated by a delimiter
s.split(' ')
# In[114]:
# equivalent (since space is the default delimiter)
s.split()
# In[115]:
s2 = 'a, an, the'
s2.split(',')
# **Join or concatenate strings:**
# In[116]:
# join a list of strings into one string using a delimiter
stooges = ['larry', 'curly', 'moe']
' '.join(stooges)
# In[117]:
# concatenate strings
s3 = 'The meaning of life is'
s4 = '42'
s3 + ' ' + s4
# **Remove whitespace from the start and end of a string:**
# In[118]:
s5 = ' ham and cheese '
s5.strip()
# **String substitutions:**
# In[119]:
# old way
'raining %s and %s' % ('cats', 'dogs')
# In[120]:
# new way
'raining {} and {}'.format('cats', 'dogs')
# In[121]:
# new way (using named arguments)
'raining {arg1} and {arg2}'.format(arg1='cats', arg2='dogs')
# **String formatting ([more examples](https://mkaz.tech/python-string-format.html)):**
# In[122]:
# use 2 decimal places
'pi is {:.2f}'.format(3.14159)
# **Normal strings versus raw strings:**
# In[123]:
# normal strings allow for escaped characters
print('first line\nsecond line')
# In[124]:
# raw strings treat backslashes as literal characters
print(r'first line\nfirst line')
# [Back to top]
# ## 9. Dictionaries
#
# - **Dictionary properties:** unordered, iterable, mutable, can contain multiple data types
# - Made of key-value pairs
# - Keys must be unique, and can be strings, numbers, or tuples
# - Values can be any type
# In[125]:
# create an empty dictionary (two ways)
empty_dict = {}
empty_dict = dict()
# In[126]:
# create a dictionary (two ways)
family = {'dad':'homer', 'mom':'marge', 'size':6}
family = dict(dad='homer', mom='marge', size=6)
family
# In[127]:
# convert a list of tuples into a dictionary
list_of_tuples = [('dad', 'homer'), ('mom', 'marge'), ('size', 6)]
family = dict(list_of_tuples)
family
# **Examine a dictionary:**
# In[128]:
# pass a key to return its value
family['dad']
# In[129]:
# return the number of key-value pairs
len(family)
# In[130]:
# check if key exists in dictionary
'mom' in family
# In[131]:
# dictionary values are not checked
'marge' in family
# In[132]:
# returns a list of keys (Python 2) or an iterable view (Python 3)
family.keys()
# In[133]:
# returns a list of values (Python 2) or an iterable view (Python 3)
family.values()
# In[134]:
# returns a list of key-value pairs (Python 2) or an iterable view (Python 3)
family.items()
# **Modify a dictionary (does not return the dictionary):**
# In[135]:
# add a new entry
family['cat'] = 'snowball'
family
# In[136]:
# edit an existing entry
family['cat'] = 'snowball ii'
family
# In[137]:
# delete an entry
del family['cat']
family
# In[138]:
# dictionary value can be a list
family['kids'] = ['bart', 'lisa']
family
# In[139]:
# remove an entry and return the value
family.pop('dad')
# In[140]:
# add multiple entries
family.update({'baby':'maggie', 'grandpa':'abe'})
family
# **Access values more safely with `get`:**
# In[141]:
family['mom']
# In[142]:
# equivalent to a dictionary lookup
family.get('mom')
# In[143]:
# this would throw an error since the key does not exist
# family['grandma']
# In[144]:
# return None if not found
family.get('grandma')
# In[145]:
# provide a default return value if not found
family.get('grandma', 'not found')
# **Access a list element within a dictionary:**
# In[146]:
family['kids'][0]
# In[147]:
family['kids'].remove('lisa')
family
# **String substitution using a dictionary:**
# In[148]:
'youngest child is %(baby)s' % family
# [Back to top]
# ## 10. Sets
#
# - **Set properties:** unordered, iterable, mutable, can contain multiple data types
# - Made of unique elements (strings, numbers, or tuples)
# - Like dictionaries, but with keys only (no values)
# In[149]:
# create an empty set
empty_set = set()
# In[150]:
# create a set directly
languages = {'python', 'r', 'java'}
# In[151]:
# create a set from a list
snakes = set(['cobra', 'viper', 'python'])
# **Examine a set:**
# In[152]:
len(languages)
# In[153]:
'python' in languages
# **Set operations:**
# In[154]:
# intersection
languages & snakes
# In[155]:
# union
languages | snakes
# In[156]:
# set difference
languages - snakes
# In[157]:
# set difference
snakes - languages
# **Modify a set (does not return the set):**
# In[158]:
# add a new element
languages.add('sql')
languages
# In[159]:
# try to add an existing element (ignored, no error)
languages.add('r')
languages
# In[160]:
# remove an element
languages.remove('java')
languages
# In[161]:
# try to remove a non-existing element (this would throw an error)
# languages.remove('c')
# In[162]:
# remove an element if present, but ignored otherwise
languages.discard('c')
languages
# In[163]:
# remove and return an arbitrary element
languages.pop()
# In[164]:
# remove all elements
languages.clear()
languages
# In[165]:
# add multiple elements (can also pass a set)
languages.update(['go', 'spark'])
languages
# **Get a sorted list of unique elements from a list:**
# In[166]:
sorted(set([9, 0, 2, 1, 0]))
# [Back to top]
# ## 11. Defining Functions
# **Define a function with no arguments and no return values:**
# In[167]:
def print_text():
print('this is text')
# In[168]:
# call the function
print_text()
# **Define a function with one argument and no return values:**
# In[169]:
def print_this(x):
print(x)
# In[170]:
# call the function
print_this(3)
# In[171]:
# prints 3, but doesn't assign 3 to n because the function has no return statement
n = print_this(3)
# **Define a function with one argument and one return value:**
# In[172]:
def square_this(x):
return x**2
# In[173]:
# include an optional docstring to describe the effect of a function
def square_this(x):
"""Return the square of a number."""
return x**2
# In[174]:
# call the function
square_this(3)
# In[175]:
# assigns 9 to var, but does not print 9
var = square_this(3)
# **Define a function with two 'positional arguments' (no default values) and one 'keyword argument' (has a default value):**
#
# In[176]:
def calc(a, b, op='add'):
if op == 'add':
return a + b
elif op == 'sub':
return a - b
else:
print('valid operations are add and sub')
# In[177]:
# call the function
calc(10, 4, op='add')
# In[178]:
# unnamed arguments are inferred by position
calc(10, 4, 'add')
# In[179]:
# default for 'op' is 'add'
calc(10, 4)
# In[180]:
calc(10, 4, 'sub')
# In[181]:
calc(10, 4, 'div')
# **Use `pass` as a placeholder if you haven't written the function body:**
# In[182]:
def stub():
pass
# **Return two values from a single function:**
# In[183]:
def min_max(nums):
return min(nums), max(nums)
# In[184]:
# return values can be assigned to a single variable as a tuple
nums = [1, 2, 3]
min_max_num = min_max(nums)
min_max_num
# In[185]:
# return values can be assigned into multiple variables using tuple unpacking
min_num, max_num = min_max(nums)
print(min_num)
print(max_num)
# [Back to top]
# ## 12. Anonymous (Lambda) Functions
#
# - Primarily used to temporarily define a function for use by another function
# In[186]:
# define a function the "usual" way
def squared(x):
return x**2
# In[187]:
# define an identical function using lambda
squared = lambda x: x**2
# **Sort a list of strings by the last letter:**
# In[188]:
# without using lambda
simpsons = ['homer', 'marge', 'bart']
def last_letter(word):
return word[-1]
sorted(simpsons, key=last_letter)
# In[189]:
# using lambda
sorted(simpsons, key=lambda word: word[-1])
# [Back to top]
# ## 13. For Loops and While Loops
# **`range` returns a list of integers (Python 2) or a sequence (Python 3):**
# In[190]:
# includes the start value but excludes the stop value
range(0, 3)
# In[191]:
# default start value is 0
range(3)
# In[192]:
# third argument is the step value
range(0, 5, 2)
# In[193]:
# Python 2 only: use xrange to create a sequence rather than a list (saves memory)
xrange(100, 100000, 5)
# **`for` loops:**
# In[194]:
# not the recommended style
fruits = ['apple', 'banana', 'cherry']
for i in range(len(fruits)):
print(fruits[i].upper())
# In[195]:
# recommended style
for fruit in fruits:
print(fruit.upper())
# In[196]:
# iterate through two things at once (using tuple unpacking)
family = {'dad':'homer', 'mom':'marge', 'size':6}
for key, value in family.items():
print(key, value)
# In[197]:
# use enumerate if you need to access the index value within the loop
for index, fruit in enumerate(fruits):
print(index, fruit)
# **`for`/`else` loop:**
# In[198]:
for fruit in fruits:
if fruit == 'banana':
print('Found the banana!')
break # exit the loop and skip the 'else' block
else:
# this block executes ONLY if the for loop completes without hitting 'break'
print("Can't find the banana")
# **`while` loop:**
# In[199]:
count = 0
while count < 5:
print('This will print 5 times')
count += 1 # equivalent to 'count = count + 1'
# [Back to top]
# ## 14. Comprehensions
# **List comprehension:**
# In[200]:
# for loop to create a list of cubes
nums = [1, 2, 3, 4, 5]
cubes = []
for num in nums:
cubes.append(num**3)
cubes
# In[201]:
# equivalent list comprehension
cubes = [num**3 for num in nums]
cubes
# In[202]:
# for loop to create a list of cubes of even numbers
cubes_of_even = []
for num in nums:
if num % 2 == 0:
cubes_of_even.append(num**3)
cubes_of_even
# In[203]:
# equivalent list comprehension
# syntax: [expression for variable in iterable if condition]
cubes_of_even = [num**3 for num in nums if num % 2 == 0]
cubes_of_even
# In[204]:
# for loop to cube even numbers and square odd numbers
cubes_and_squares = []
for num in nums:
if num % 2 == 0:
cubes_and_squares.append(num**3)
else:
cubes_and_squares.append(num**2)
cubes_and_squares
# In[205]:
# equivalent list comprehension (using a ternary expression)
# syntax: [true_condition if condition else false_condition for variable in iterable]
cubes_and_squares = [num**3 if num % 2 == 0 else num**2 for num in nums]
cubes_and_squares
# In[206]:
# for loop to flatten a 2d-matrix
matrix = [[1, 2], [3, 4]]
items = []
for row in matrix:
for item in row:
items.append(item)
items
# In[207]:
# equivalent list comprehension
items = [item for row in matrix
for item in row]
items
# **Set comprehension:**
# In[208]:
fruits = ['apple', 'banana', 'cherry']
unique_lengths = {len(fruit) for fruit in fruits}
unique_lengths
# **Dictionary comprehension:**
# In[209]:
fruit_lengths = {fruit:len(fruit) for fruit in fruits}
fruit_lengths
# In[210]:
fruit_indices = {fruit:index for index, fruit in enumerate(fruits)}
fruit_indices
# [Back to top]
# ## 15. Map and Filter
# **`map` applies a function to every element of a sequence and returns a list (Python 2) or iterator (Python 3):**
# In[211]:
simpsons = ['homer', 'marge', 'bart']
map(len, simpsons)
# In[212]:
# equivalent list comprehension
[len(word) for word in simpsons]
# In[213]:
map(lambda word: word[-1], simpsons)
# In[214]:
# equivalent list comprehension
[word[-1] for word in simpsons]
# **`filter` returns a list (Python 2) or iterator (Python 3) containing the elements from a sequence for which a condition is `True`:**
# In[215]:
nums = range(5)
filter(lambda x: x % 2 == 0, nums)
# In[216]:
# equivalent list comprehension
[num for num in nums if num % 2 == 0]
# [Back to top]