Cheatsheet-v2
Python Cheatsheet
About
Contribute
Read It
Python Cheatsheet
The Zen of Python
Python Basics
Math Operators
Data Types
String Concatenation and Replication
Variables
Comments
The print() Function
The input() Function
The len() Function
The str(), int(), and float() Functions
Flow Control
Comparison Operators
Boolean evaluation
Boolean Operators
Mixing Boolean and Comparison Operators
if Statements
else Statements
elif Statements
while Loop Statements
break Statements
continue Statements
for Loops and the range() Function
For else statement
Importing Modules
Ending a Program Early with sys.exit()
Functions
Return Values and return Statements
The None Value
Keyword Arguments and print()
Local and Global Scope
The global Statement
Exception Handling
Basic exception handling
Final code in exception handling
Lists
Getting Individual Values in a List with Indexes
Negative Indexes
Getting Sublists with Slices
Getting a List's Length with len()
Changing Values in a List with Indexes
List Concatenation and List Replication
Removing Values from Lists with del Statements
Using for Loops with Lists
Looping Through Multiple Lists with zip()
The in and not in Operators
The Multiple Assignment Trick
Augmented Assignment Operators
Finding a Value in a List with the index() Method
Adding Values to Lists with the append() and insert() Methods
Removing Values from Lists with remove()
Removing Values from Lists with pop()
Sorting the Values in a List with the sort() Method
Tuple Data Type
Converting Types with the list() and tuple() Functions
Dictionaries and Structuring Data
The keys(), values(), and items() Methods
Checking Whether a Key or Value Exists in a Dictionary
The get() Method
The setdefault() Method
Pretty Printing
Merge two dictionaries
sets
Initializing a set
sets: unordered collections of unique elements
set add() and update()
set remove() and discard()
set union()
set intersection
set difference
set symetric_difference
itertools Module
accumulate()
combinations()
combinations_with_replacement()
count()
cycle()
chain()
compress()
dropwhile()
filterfalse()
groupby()
islice()
permutations()
product()
repeat()
starmap()
takewhile()
tee()
zip_longest()
Comprehensions
List comprehension
Set comprehension
Dict comprehension
Manipulating Strings
Escape Characters
Raw Strings
Multiline Strings with Triple Quotes
Indexing and Slicing Strings
The in and not in Operators with Strings
The in and not in Operators with list
The upper(), lower(), isupper(), and islower() String Methods
The isX String Methods
The startswith() and endswith() String Methods
The join() and split() String Methods
Justifying Text with rjust(), ljust(), and center()
Removing Whitespace with strip(), rstrip(), and lstrip()
Copying and Pasting Strings with the pyperclip Module (need pip install)
String Formatting
% operator
String Formatting (str.format)
Lazy string formatting
Formatted String Literals or f-strings (Python 3.6+)
Template Strings
Regular Expressions
Matching Regex Objects
Grouping with Parentheses
Matching Multiple Groups with the Pipe
Optional Matching with the Question Mark
Matching Zero or More with the Star
Matching One or More with the Plus
Matching Specific Repetitions with Curly Brackets
Greedy and Nongreedy Matching
The findall() Method
Making Your Own Character Classes
The Caret and Dollar Sign Characters
The Wildcard Character
Matching Everything with Dot-Star
Matching Newlines with the Dot Character
Review of Regex Symbols
Case-Insensitive Matching
Substituting Strings with the sub() Method
Managing Complex Regexes
Handling File and Directory Paths
Backslash on Windows and Forward Slash on OS X and Linux
The Current Working Directory
Creating New Folders
Absolute vs. Relative Paths
Handling Absolute and Relative Paths
Checking Path Validity
Finding File Sizes and Folder Contents
Copying Files and Folders
Moving and Renaming Files and Folders
Permanently Deleting Files and Folders
Safe Deletes with the send2trash Module
Walking a Directory Tree
Reading and Writing Files
The File Reading/Writing Process
Opening and reading files with the open() function
Writing to Files
Saving Variables with the shelve Module
Saving Variables with the pprint.pformat() Function
Reading ZIP Files
Extracting from ZIP Files
Creating and Adding to ZIP Files
JSON, YAML and configuration files
JSON
YAML
Anyconfig
Debugging
Raising Exceptions
Getting the Traceback as a String
Assertions
Logging
Logging Levels
Disabling Logging
Logging to a File
Lambda Functions
Ternary Conditional Operator
args and kwargs
Things to Remember(args)
Things to Remember(kwargs)
Context Manager
with statement
Writing your own contextmanager using generator syntax
__main__
Top-level script environmentAdvantages
setup.py
Dataclasses
Features
Default values
Type hints
Virtual Environment
virtualenv
poetry
pipenv
anaconda
The Zen of Python
From the PEP 20 -- The Zen of Python:
Long time Pythoneer Tim Peters succinctly channels the BDFL's guiding principles for Python's design into 20 aphorisms, only 19 of which have been written down.
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Return to the Top
Python Basics
Math Operators
From Highest to Lowest precedence:
**
Exponent
2 ** 3 = 8
%
Modulus/Remainder
22 % 8 = 6
//
Integer division
22 // 8 = 2
/
Division
22 / 8 = 2.75
*
Multiplication
3 * 3 = 9
-
Subtraction
5 - 2 = 3
+
Addition
2 + 2 = 4
Examples of expressions in the interactive shell:
>>> 2 + 3 * 6
20
>>> (2 + 3) * 6
30
>>> 2 ** 8
256
>>> 23 // 7
3
>>> 23 % 7
2
>>> (5 - 1) * ((7 + 1) / (3 - 1))
16.0
Return to the Top
Data Types
Integers
-2, -1, 0, 1, 2, 3, 4, 5
Floating-point numbers
-1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25
Strings
'a', 'aa', 'aaa', 'Hello!', '11 cats'
Return to the Top
String Concatenation and Replication
String concatenation:
>>> 'Alice' 'Bob'
'AliceBob'
Note: Avoid +
operator for string concatenation. Prefer string formatting.
String Replication:
>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'
Return to the Top
Variables
You can name a variable anything as long as it obeys the following rules:
It can be only one word.
It can use only letters, numbers, and the underscore (
_
) character.It can't begin with a number.
Variable name starting with an underscore (
_
) are considered as "unuseful`.
Example:
>>> spam = 'Hello'
>>> spam
'Hello'
>>> _spam = 'Hello'
_spam
should not be used again in the code.
Return to the Top
Comments
Inline comment:
# This is a comment
Multiline comment:
# This is a
# multiline comment
Code with a comment:
a = 1 # initialization
Please note the two spaces in front of the comment.
Function docstring:
def foo():
"""
This is a function docstring
You can also use:
''' Function Docstring '''
"""
Return to the Top
The print() Function
>>> print('Hello world!')
Hello world!
>>> a = 1
>>> print('Hello world!', a)
Hello world! 1
Return to the Top
The input() Function
Example Code:
>>> print('What is your name?') # ask for their name
>>> myName = input()
>>> print('It is good to meet you, {}'.format(myName))
What is your name?
Al
It is good to meet you, Al
Return to the Top
The len() Function
Evaluates to the integer value of the number of characters in a string:
>>> len('hello')
5
Note: test of emptiness of strings, lists, dictionary, etc, should not use len, but prefer direct boolean evaluation.
>>> a = [1, 2, 3]
>>> if a:
>>> print("the list is not empty!")
Return to the Top
The str(), int(), and float() Functions
Integer to String or Float:
>>> str(29)
'29'
>>> print('I am {} years old.'.format(str(29)))
I am 29 years old.
>>> str(-3.14)
'-3.14'
Float to Integer:
>>> int(7.7)
7
>>> int(7.7) + 1
8
Return to the Top
Flow Control
Comparison Operators
==
Equal to
!=
Not equal to
<
Less than
>
Greater Than
<=
Less than or Equal to
>=
Greater than or Equal to
These operators evaluate to True or False depending on the values you give them.
Examples:
>>> 42 == 42
True
>>> 40 == 42
False
>>> 'hello' == 'hello'
True
>>> 'hello' == 'Hello'
False
>>> 'dog' != 'cat'
True
>>> 42 == 42.0
True
>>> 42 == '42'
False
Boolean evaluation
Never use ==
or !=
operator to evaluate boolean operation. Use the is
or is not
operators, or use implicit boolean evaluation.
NO (even if they are valid Python):
>>> True == True
True
>>> True != False
True
YES (even if they are valid Python):
>>> True is True
True
>>> True is not False
True
These statements are equivalent:
>>> if a is True:
>>> pass
>>> if a is not False:
>>> pass
>>> if a:
>>> pass
And these as well:
>>> if a is False:
>>> pass
>>> if a is not True:
>>> pass
>>> if not a:
>>> pass
Return to the Top
Boolean Operators
There are three Boolean operators: and, or, and not.
The and Operator's Truth Table:
True and True
True
True and False
False
False and True
False
False and False
False
The or Operator's Truth Table:
True or True
True
True or False
True
False or True
True
False or False
False
The not Operator's Truth Table:
not True
False
not False
True
Return to the Top
Mixing Boolean and Comparison Operators
>>> (4 < 5) and (5 < 6)
True
>>> (4 < 5) and (9 < 6)
False
>>> (1 == 2) or (2 == 2)
True
You can also use multiple Boolean operators in an expression, along with the comparison operators:
>>> 2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2
True
Return to the Top
if Statements
if name == 'Alice':
print('Hi, Alice.')
Return to the Top
else Statements
name = 'Bob'
if name == 'Alice':
print('Hi, Alice.')
else:
print('Hello, stranger.')
Return to the Top
elif Statements
name = 'Bob'
age = 5
if name == 'Alice':
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')
name = 'Bob'
age = 30
if name == 'Alice':
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')
else:
print('You are neither Alice nor a little kid.')
Return to the Top
while Loop Statements
spam = 0
while spam < 5:
print('Hello, world.')
spam = spam + 1
Return to the Top
break Statements
If the execution reaches a break statement, it immediately exits the while loop's clause:
while True:
print('Please type your name.')
name = input()
if name == 'your name':
break
print('Thank you!')
Return to the Top
continue Statements
When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop.
while True:
print('Who are you?')
name = input()
if name != 'Joe':
continue
print('Hello, Joe. What is the password? (It is a fish.)')
password = input()
if password == 'swordfish':
break
print('Access granted.')
Return to the Top
for Loops and the range() Function
>>> print('My name is')
>>> for i in range(5):
>>> print('Jimmy Five Times ({})'.format(str(i)))
My name is
Jimmy Five Times (0)
Jimmy Five Times (1)
Jimmy Five Times (2)
Jimmy Five Times (3)
Jimmy Five Times (4)
The range() function can also be called with three arguments. The first two arguments will be the start and stop values, and the third will be the step argument. The step is the amount that the variable is increased by after each iteration.
>>> for i in range(0, 10, 2):
>>> print(i)
0
2
4
6
8
You can even use a negative number for the step argument to make the for loop count down instead of up.
>>> for i in range(5, -1, -1):
>>> print(i)
5
4
3
2
1
0
For else statement
This allows to specify a statement to execute in case of the full loop has been executed. Only useful when a break
condition can occur in the loop:
>>> for i in [1, 2, 3, 4, 5]:
>>> if i == 3:
>>> break
>>> else:
>>> print("only executed when no item of the list is equal to 3")
Return to the Top
Importing Modules
import random
for i in range(5):
print(random.randint(1, 10))
import random, sys, os, math
from random import *
Return to the Top
Ending a Program Early with sys.exit()
import sys
while True:
print('Type exit to exit.')
response = input()
if response == 'exit':
sys.exit()
print('You typed {}.'.format(response))
Return to the Top
Functions
>>> def hello(name):
>>> print('Hello {}'.format(name))
>>>
>>> hello('Alice')
>>> hello('Bob')
Hello Alice
Hello Bob
Return to the Top
Return Values and return Statements
When creating a function using the def statement, you can specify what the return value should be with a return statement. A return statement consists of the following:
The return keyword.
The value or expression that the function should return.
import random
def getAnswer(answerNumber):
if answerNumber == 1:
return 'It is certain'
elif answerNumber == 2:
return 'It is decidedly so'
elif answerNumber == 3:
return 'Yes'
elif answerNumber == 4:
return 'Reply hazy try again'
elif answerNumber == 5:
return 'Ask again later'
elif answerNumber == 6:
return 'Concentrate and ask again'
elif answerNumber == 7:
return 'My reply is no'
elif answerNumber == 8:
return 'Outlook not so good'
elif answerNumber == 9:
return 'Very doubtful'
r = random.randint(1, 9)
fortune = getAnswer(r)
print(fortune)
Return to the Top
The None Value
>>> spam = print('Hello!')
Hello!
>>> spam is None
True
Note: never compare to None
with the ==
operator. Always use is
.
Return to the Top
Keyword Arguments and print()
>>> print('Hello', end='')
>>> print('World')
HelloWorld
>>> print('cats', 'dogs', 'mice')
cats dogs mice
>>> print('cats', 'dogs', 'mice', sep=',')
cats,dogs,mice
Return to the Top
Local and Global Scope
Code in the global scope cannot use any local variables.
However, a local scope can access global variables.
Code in a function's local scope cannot use variables in any other local scope.
You can use the same name for different variables if they are in different scopes. That is, there can be a local variable named spam and a global variable also named spam.
Return to the Top
The global Statement
If you need to modify a global variable from within a function, use the global statement:
>>> def spam():
>>> global eggs
>>> eggs = 'spam'
>>>
>>> eggs = 'global'
>>> spam()
>>> print(eggs)
spam
There are four rules to tell whether a variable is in a local scope or global scope:
If a variable is being used in the global scope (that is, outside of all functions), then it is always a global variable.
If there is a global statement for that variable in a function, it is a global variable.
Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.
But if the variable is not used in an assignment statement, it is a global variable.
Return to the Top
Exception Handling
Basic exception handling
>>> def spam(divideBy):
>>> try:
>>> return 42 / divideBy
>>> except ZeroDivisionError as e:
>>> print('Error: Invalid argument: {}'.format(e))
>>>
>>> print(spam(2))
>>> print(spam(12))
>>> print(spam(0))
>>> print(spam(1))
21.0
3.5
Error: Invalid argument: division by zero
None
42.0
Return to the Top
Final code in exception handling
Code inside the finally
section is always executed, no matter if an exception has been raised or not, and even if an exception is not caught.
>>> def spam(divideBy):
>>> try:
>>> return 42 / divideBy
>>> except ZeroDivisionError as e:
>>> print('Error: Invalid argument: {}'.format(e))
>>> finally:
>>> print("-- division finished --")
>>> print(spam(2))
-- division finished --
21.0
>>> print(spam(12))
-- division finished --
3.5
>>> print(spam(0))
Error: Invalid Argument division by zero
-- division finished --
None
>>> print(spam(1))
-- division finished --
42.0
Return to the Top
Lists
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam
['cat', 'bat', 'rat', 'elephant']
Return to the Top
Getting Individual Values in a List with Indexes
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[0]
'cat'
>>> spam[1]
'bat'
>>> spam[2]
'rat'
>>> spam[3]
'elephant'
Return to the Top
Negative Indexes
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[-1]
'elephant'
>>> spam[-3]
'bat'
>>> 'The {} is afraid of the {}.'.format(spam[-1], spam[-3])
'The elephant is afraid of the bat.'
Return to the Top
Getting Sublists with Slices
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[0:4]
['cat', 'bat', 'rat', 'elephant']
>>> spam[1:3]
['bat', 'rat']
>>> spam[0:-1]
['cat', 'bat', 'rat']
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[:2]
['cat', 'bat']
>>> spam[1:]
['bat', 'rat', 'elephant']
Slicing the complete list will perform a copy:
>>> spam2 = spam[:]
['cat', 'bat', 'rat', 'elephant']
>>> spam.append('dog')
>>> spam
['cat', 'bat', 'rat', 'elephant', 'dog']
>>> spam2
['cat', 'bat', 'rat', 'elephant']
Return to the Top
Getting a List's Length with len()
>>> spam = ['cat', 'dog', 'moose']
>>> len(spam)
3
Return to the Top
Changing Values in a List with Indexes
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam[1] = 'aardvark'
>>> spam
['cat', 'aardvark', 'rat', 'elephant']
>>> spam[2] = spam[1]
>>> spam
['cat', 'aardvark', 'aardvark', 'elephant']
>>> spam[-1] = 12345
>>> spam
['cat', 'aardvark', 'aardvark', 12345]
Return to the Top
List Concatenation and List Replication
>>> [1, 2, 3] + ['A', 'B', 'C']
[1, 2, 3, 'A', 'B', 'C']
>>> ['X', 'Y', 'Z'] * 3
['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']
>>> spam = [1, 2, 3]
>>> spam = spam + ['A', 'B', 'C']
>>> spam
[1, 2, 3, 'A', 'B', 'C']
Return to the Top
Removing Values from Lists with del Statements
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat']
Return to the Top
Using for Loops with Lists
>>> supplies = ['pens', 'staplers', 'flame-throwers', 'binders']
>>> for i, supply in enumerate(supplies):
>>> print('Index {} in supplies is: {}'.format(str(i), supply))
Index 0 in supplies is: pens
Index 1 in supplies is: staplers
Index 2 in supplies is: flame-throwers
Index 3 in supplies is: binders
Return to the Top
Looping Through Multiple Lists with zip()
>>> name = ['Pete', 'John', 'Elizabeth']
>>> age = [6, 23, 44]
>>> for n, a in zip(name, age):
>>> print('{} is {} years old'.format(n, a))
Pete is 6 years old
John is 23 years old
Elizabeth is 44 years old
The in and not in Operators
>>> 'howdy' in ['hello', 'hi', 'howdy', 'heyas']
True
>>> spam = ['hello', 'hi', 'howdy', 'heyas']
>>> 'cat' in spam
False
>>> 'howdy' not in spam
False
>>> 'cat' not in spam
True
Return to the Top
The Multiple Assignment Trick
The multiple assignment trick is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this:
>>> cat = ['fat', 'orange', 'loud']
>>> size = cat[0]
>>> color = cat[1]
>>> disposition = cat[2]
You could type this line of code:
>>> cat = ['fat', 'orange', 'loud']
>>> size, color, disposition = cat
The multiple assignment trick can also be used to swap the values in two variables:
>>> a, b = 'Alice', 'Bob'
>>> a, b = b, a
>>> print(a)
'Bob'
>>> print(b)
'Alice'
Return to the Top
Augmented Assignment Operators
spam += 1
spam = spam + 1
spam -= 1
spam = spam - 1
spam *= 1
spam = spam * 1
spam /= 1
spam = spam / 1
spam %= 1
spam = spam % 1
Examples:
>>> spam = 'Hello'
>>> spam += ' world!'
>>> spam
'Hello world!'
>>> bacon = ['Zophie']
>>> bacon *= 3
>>> bacon
['Zophie', 'Zophie', 'Zophie']
Return to the Top
Finding a Value in a List with the index() Method
>>> spam = ['Zophie', 'Pooka', 'Fat-tail', 'Pooka']
>>> spam.index('Pooka')
1
Return to the Top
Adding Values to Lists with the append() and insert() Methods
append():
>>> spam = ['cat', 'dog', 'bat']
>>> spam.append('moose')
>>> spam
['cat', 'dog', 'bat', 'moose']
insert():
>>> spam = ['cat', 'dog', 'bat']
>>> spam.insert(1, 'chicken')
>>> spam
['cat', 'chicken', 'dog', 'bat']
Return to the Top
Removing Values from Lists with remove()
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam.remove('bat')
>>> spam
['cat', 'rat', 'elephant']
If the value appears multiple times in the list, only the first instance of the value will be removed.
Return to the Top
Removing Values from Lists with pop()
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam.pop()
'elephant'
>>> spam
['cat', 'bat', 'rat']
>>> spam.pop(0)
'cat'
>>> spam
['bat', 'rat']
Return to the Top
Sorting the Values in a List with the sort() Method
>>> spam = [2, 5, 3.14, 1, -7]
>>> spam.sort()
>>> spam
[-7, 1, 2, 3.14, 5]
>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
>>> spam.sort()
>>> spam
['ants', 'badgers', 'cats', 'dogs', 'elephants']
You can also pass True for the reverse keyword argument to have sort() sort the values in reverse order:
>>> spam.sort(reverse=True)
>>> spam
['elephants', 'dogs', 'cats', 'badgers', 'ants']
If you need to sort the values in regular alphabetical order, pass str. lower for the key keyword argument in the sort() method call:
>>> spam = ['a', 'z', 'A', 'Z']
>>> spam.sort(key=str.lower)
>>> spam
['a', 'A', 'z', 'Z']
You can use the built-in function sorted
to return a new list:
>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
>>> sorted(spam)
['ants', 'badgers', 'cats', 'dogs', 'elephants']
Return to the Top
Tuple Data Type
>>> eggs = ('hello', 42, 0.5)
>>> eggs[0]
'hello'
>>> eggs[1:3]
(42, 0.5)
>>> len(eggs)
3
The main way that tuples are different from lists is that tuples, like strings, are immutable.
Return to the Top
Converting Types with the list() and tuple() Functions
>>> tuple(['cat', 'dog', 5])
('cat', 'dog', 5)
>>> list(('cat', 'dog', 5))
['cat', 'dog', 5]
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
Return to the Top
Dictionaries and Structuring Data
Example Dictionary:
myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}
Return to the Top
The keys(), values(), and items() Methods
values():
>>> spam = {'color': 'red', 'age': 42}
>>> for v in spam.values():
>>> print(v)
red
42
keys():
>>> for k in spam.keys():
>>> print(k)
color
age
items():
>>> for i in spam.items():
>>> print(i)
('color', 'red')
('age', 42)
Using the keys(), values(), and items() methods, a for loop can iterate over the keys, values, or key-value pairs in a dictionary, respectively.
>>> spam = {'color': 'red', 'age': 42}
>>>
>>> for k, v in spam.items():
>>> print('Key: {} Value: {}'.format(k, str(v)))
Key: age Value: 42
Key: color Value: red
Return to the Top
Checking Whether a Key or Value Exists in a Dictionary
>>> spam = {'name': 'Zophie', 'age': 7}
>>> 'name' in spam.keys()
True
>>> 'Zophie' in spam.values()
True
>>> # You can omit the call to keys() when checking for a key
>>> 'color' in spam
False
>>> 'color' not in spam
True
Return to the Top
The get() Method
Get has two parameters: key and default value if the key did not exist
>>> picnic_items = {'apples': 5, 'cups': 2}
>>> 'I am bringing {} cups.'.format(str(picnic_items.get('cups', 0)))
'I am bringing 2 cups.'
>>> 'I am bringing {} eggs.'.format(str(picnic_items.get('eggs', 0)))
'I am bringing 0 eggs.'
Return to the Top
The setdefault() Method
Let's consider this code:
spam = {'name': 'Pooka', 'age': 5}
if 'color' not in spam:
spam['color'] = 'black'
Using setdefault
we could write the same code more succinctly:
>>> spam = {'name': 'Pooka', 'age': 5}
>>> spam.setdefault('color', 'black')
'black'
>>> spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}
>>> spam.setdefault('color', 'white')
'black'
>>> spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}
Return to the Top
Pretty Printing
>>> import pprint
>>>
>>> message = 'It was a bright cold day in April, and the clocks were striking
>>> thirteen.'
>>> count = {}
>>>
>>> for character in message:
>>> count.setdefault(character, 0)
>>> count[character] = count[character] + 1
>>>
>>> pprint.pprint(count)
{' ': 13,
',': 1,
'.': 1,
'A': 1,
'I': 1,
'a': 4,
'b': 1,
'c': 3,
'd': 3,
'e': 5,
'g': 2,
'h': 3,
'i': 6,
'k': 2,
'l': 3,
'n': 4,
'o': 2,
'p': 1,
'r': 5,
's': 3,
't': 6,
'w': 2,
'y': 1}
Return to the Top
Merge two dictionaries
# in Python 3.5+:
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 3, 'c': 4}
>>> z = {**x, **y}
>>> z
{'c': 4, 'a': 1, 'b': 3}
# in Python 2.7
>>> z = dict(x, **y)
>>> z
{'c': 4, 'a': 1, 'b': 3}
sets
From the Python 3 documentation
A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
Initializing a set
There are two ways to create sets: using curly braces {}
and the built-in function set()
>>> s = {1, 2, 3}
>>> s = set([1, 2, 3])
When creating an empty set, be sure to not use the curly braces {}
or you will get an empty dictionary instead.
>>> s = {}
>>> type(s)
<class 'dict'>
sets: unordered collections of unique elements
A set automatically remove all the duplicate values.
>>> s = {1, 2, 3, 2, 3, 4}
>>> s
{1, 2, 3, 4}
And as an unordered data type, they can't be indexed.
>>> s = {1, 2, 3}
>>> s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>>
set add() and update()
Using the add()
method we can add a single element to the set.
>>> s = {1, 2, 3}
>>> s.add(4)
>>> s
{1, 2, 3, 4}
And with update()
, multiple ones .
>>> s = {1, 2, 3}
>>> s.update([2, 3, 4, 5, 6])
>>> s
{1, 2, 3, 4, 5, 6} # remember, sets automatically remove duplicates
set remove() and discard()
Both methods will remove an element from the set, but remove()
will raise a key error
if the value doesn't exist.
>>> s = {1, 2, 3}
>>> s.remove(3)
>>> s
{1, 2}
>>> s.remove(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 3
discard()
won't raise any errors.
>>> s = {1, 2, 3}
>>> s.discard(3)
>>> s
{1, 2}
>>> s.discard(3)
>>>
set union()
union()
or |
will create a new set that contains all the elements from the sets provided.
>>> s1 = {1, 2, 3}
>>> s2 = {3, 4, 5}
>>> s1.union(s2) # or 's1 | s2'
{1, 2, 3, 4, 5}
set intersection
intersection
or &
will return a set containing only the elements that are common to all of them.
>>> s1 = {1, 2, 3}
>>> s2 = {2, 3, 4}
>>> s3 = {3, 4, 5}
>>> s1.intersection(s2, s3) # or 's1 & s2 & s3'
{3}
set difference
difference
or -
will return only the elements that are unique to the first set (invoked set).
>>> s1 = {1, 2, 3}
>>> s2 = {2, 3, 4}
>>> s1.difference(s2) # or 's1 - s2'
{1}
>>> s2.difference(s1) # or 's2 - s1'
{4}
set symetric_difference
symetric_difference
or ^
will return all the elements that are not common between them.
>>> s1 = {1, 2, 3}
>>> s2 = {2, 3, 4}
>>> s1.symmetric_difference(s2) # or 's1 ^ s2'
{1, 4}
Return to the Top
itertools Module
The itertools module is a collection of tools intended to be fast and use memory efficiently when handling iterators (like lists or dictionaries).
From the official Python 3.x documentation:
The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an "iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.
The itertools module comes in the standard library and must be imported.
The operator module will also be used. This module is not necessary when using itertools, but needed for some of the examples below.
Return to the Top
accumulate()
Makes an iterator that returns the results of a function.
itertools.accumulate(iterable[, func])
Example:
>>> data = [1, 2, 3, 4, 5]
>>> result = itertools.accumulate(data, operator.mul)
>>> for each in result:
>>> print(each)
1
2
6
24
120
The operator.mul takes two numbers and multiplies them:
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
Passing a function is optional:
>>> data = [5, 2, 6, 4, 5, 9, 1]
>>> result = itertools.accumulate(data)
>>> for each in result:
>>> print(each)
5
7
13
17
22
31
32
If no function is designated the items will be summed:
5
5 + 2 = 7
7 + 6 = 13
13 + 4 = 17
17 + 5 = 22
22 + 9 = 31
31 + 1 = 32
Return to the Top
combinations()
Takes an iterable and a integer. This will create all the unique combination that have r members.
itertools.combinations(iterable, r)
Example:
>>> shapes = ['circle', 'triangle', 'square',]
>>> result = itertools.combinations(shapes, 2)
>>> for each in result:
>>> print(each)
('circle', 'triangle')
('circle', 'square')
('triangle', 'square')
Return to the Top
combinations_with_replacement()
Just like combinations(), but allows individual elements to be repeated more than once.
itertools.combinations_with_replacement(iterable, r)
Example:
>>> shapes = ['circle', 'triangle', 'square']
>>> result = itertools.combinations_with_replacement(shapes, 2)
>>> for each in result:
>>> print(each)
('circle', 'circle')
('circle', 'triangle')
('circle', 'square')
('triangle', 'triangle')
('triangle', 'square')
('square', 'square')
Return to the Top
count()
Makes an iterator that returns evenly spaced values starting with number start.
itertools.count(start=0, step=1)
Example:
>>> for i in itertools.count(10,3):
>>> print(i)
>>> if i > 20:
>>> break
10
13
16
19
22
Return to the Top
cycle()
This function cycles through an iterator endlessly.
itertools.cycle(iterable)
Example:
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet']
>>> for color in itertools.cycle(colors):
>>> print(color)
red
orange
yellow
green
blue
violet
red
orange
When reached the end of the iterable it start over again from the beginning.
Return to the Top
chain()
Take a series of iterables and return them as one long iterable.
itertools.chain(*iterables)
Example:
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
>>> shapes = ['circle', 'triangle', 'square', 'pentagon']
>>> result = itertools.chain(colors, shapes)
>>> for each in result:
>>> print(each)
red
orange
yellow
green
blue
circle
triangle
square
pentagon
Return to the Top
compress()
Filters one iterable with another.
itertools.compress(data, selectors)
Example:
>>> shapes = ['circle', 'triangle', 'square', 'pentagon']
>>> selections = [True, False, True, False]
>>> result = itertools.compress(shapes, selections)
>>> for each in result:
>>> print(each)
circle
square
Return to the Top
dropwhile()
Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element.
itertools.dropwhile(predicate, iterable)
Example:
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
>>> result = itertools.dropwhile(lambda x: x<5, data)
>>> for each in result:
>>> print(each)
5
6
7
8
9
10
1
Return to the Top
filterfalse()
Makes an iterator that filters elements from iterable returning only those for which the predicate is False.
itertools.filterfalse(predicate, iterable)
Example:
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
>>> result = itertools.filterfalse(lambda x: x<5, data)
>>> for each in result:
>>> print(each)
5
6
7
8
9
10
Return to the Top
groupby()
Simply put, this function groups things together.
itertools.groupby(iterable, key=None)
Example:
>>> robots = [{
'name': 'blaster',
'faction': 'autobot'
}, {
'name': 'galvatron',
'faction': 'decepticon'
}, {
'name': 'jazz',
'faction': 'autobot'
}, {
'name': 'metroplex',
'faction': 'autobot'
}, {
'name': 'megatron',
'faction': 'decepticon'
}, {
'name': 'starcream',
'faction': 'decepticon'
}]
>>> for key, group in itertools.groupby(robots, key=lambda x: x['faction']):
>>> print(key)
>>> print(list(group))
autobot
[{'name': 'blaster', 'faction': 'autobot'}]
decepticon
[{'name': 'galvatron', 'faction': 'decepticon'}]
autobot
[{'name': 'jazz', 'faction': 'autobot'}, {'name': 'metroplex', 'faction': 'autobot'}]
decepticon
[{'name': 'megatron', 'faction': 'decepticon'}, {'name': 'starcream', 'faction': 'decepticon'}]
Return to the Top
islice()
This function is very much like slices. This allows you to cut out a piece of an iterable.
itertools.islice(iterable, start, stop[, step])
Example:
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue',]
>>> few_colors = itertools.islice(colors, 2)
>>> for each in few_colors:
>>> print(each)
red
orange
Return to the Top
permutations()
itertools.permutations(iterable, r=None)
Example:
>>> alpha_data = ['a', 'b', 'c']
>>> result = itertools.permutations(alpha_data)
>>> for each in result:
>>> print(each)
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
Return to the Top
product()
Creates the cartesian products from a series of iterables.
>>> num_data = [1, 2, 3]
>>> alpha_data = ['a', 'b', 'c']
>>> result = itertools.product(num_data, alpha_data)
>>> for each in result:
print(each)
(1, 'a')
(1, 'b')
(1, 'c')
(2, 'a')
(2, 'b')
(2, 'c')
(3, 'a')
(3, 'b')
(3, 'c')
Return to the Top
repeat()
This function will repeat an object over and over again. Unless, there is a times argument.
itertools.repeat(object[, times])
Example:
>>> for i in itertools.repeat("spam", 3):
print(i)
spam
spam
spam
Return to the Top
starmap()
Makes an iterator that computes the function using arguments obtained from the iterable.
itertools.starmap(function, iterable)
Example:
>>> data = [(2, 6), (8, 4), (7, 3)]
>>> result = itertools.starmap(operator.mul, data)
>>> for each in result:
>>> print(each)
12
32
21
Return to the Top
takewhile()
The opposite of dropwhile(). Makes an iterator and returns elements from the iterable as long as the predicate is true.
itertools.takewhile(predicate, iterable)
Example:
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
>>> result = itertools.takewhile(lambda x: x<5, data)
>>> for each in result:
>>> print(each)
1
2
3
4
Return to the Top
tee()
Return n independent iterators from a single iterable.
itertools.tee(iterable, n=2)
Example:
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
>>> alpha_colors, beta_colors = itertools.tee(colors)
>>> for each in alpha_colors:
>>> print(each)
red
orange
yellow
green
blue
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
>>> alpha_colors, beta_colors = itertools.tee(colors)
>>> for each in beta_colors:
>>> print(each)
red
orange
yellow
green
blue
Return to the Top
zip_longest()
Makes an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.
itertools.zip_longest(*iterables, fillvalue=None)
Example:
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue',]
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,]
>>> for each in itertools.zip_longest(colors, data, fillvalue=None):
>>> print(each)
('red', 1)
('orange', 2)
('yellow', 3)
('green', 4)
('blue', 5)
(None, 6)
(None, 7)
(None, 8)
(None, 9)
(None, 10)
Return to the Top
Comprehensions
List comprehension
>>> a = [1, 3, 5, 7, 9, 11]
>>> [i - 1 for i in a]
[0, 2, 4, 6, 8, 10]