Python Cheat Sheet PDF
Python Cheat Sheet PDF
md 11/27/2018
Basic cheatsheet for Python mostly based on the book written by Al Sweigart, Automate the Boring Stuff with
Python under the Creative Commons license and many other sources.
Contribute
All contributions are welcome:
Read It
Website
Github
PDF
Jupyter Notebook
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
1 / 96
README.md 11/27/2018
2 / 96
README.md 11/27/2018
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
3 / 96
README.md 11/27/2018
4 / 96
README.md 11/27/2018
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.
5 / 96
README.md 11/27/2018
Python Basics
Math Operators
** Exponent 2 ** 3 = 8
% Modulus/Remaider 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
>>> 2 + 3 * 6
20
>>> (2 + 3) * 6
30
>>> 2 ** 8
256
>>> 23 // 7
3
6 / 96
README.md 11/27/2018
>>> 23 % 7
2
Data Types
String concatenation:
String Replication:
>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'
Variables
You can name a variable anything as long as it obeys the following three rules:
7 / 96
README.md 11/27/2018
Example:
Comments
Inline comment:
# This is a comment
Multiline comment:
# This is a
# multiline comment
a = 1 # initialization
Function docstring:
def foo():
"""
This is a function docstring
You can also use:
''' Function Docstring '''
"""
>>> a = 1
>>> print('Hello world!', a)
Hello world! 1
Example Code:
>>> 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!")
9 / 96
README.md 11/27/2018
>>> str(29)
'29'
>>> str(-3.14)
'-3.14'
Float to Integer:
>>> int(7.7)
7
>>> int(7.7) + 1
8
Flow Control
Comparison Operators
Operator Meaning
== Equal to
!= Not equal to
These operators evaluate to True or False depending on the values you give them.
Examples:
10 / 96
README.md 11/27/2018
>>> 42 == 42
True
>>> 40 == 42
False
>>> 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.
11 / 96
README.md 11/27/2018
>>> if a is True:
>>> pass
>>> if a is not False:
>>> pass
>>> if a:
>>> pass
>>> if a is False:
>>> pass
>>> if a is not True:
>>> pass
>>> if not a:
>>> pass
Boolean Operators
Expression Evaluates to
12 / 96
README.md 11/27/2018
Expression Evaluates to
Expression Evaluates to
>>> (1 == 2) or (2 == 2)
True
You can also use multiple Boolean operators in an expression, along with the comparison operators:
if Statements
if name == 'Alice':
print('Hi, Alice.')
13 / 96
README.md 11/27/2018
else Statements
name = 'Bob'
if name == 'Alice':
print('Hi, Alice.')
else:
print('Hello, stranger.')
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.')
spam = 0
while spam < 5:
print('Hello, world.')
spam = spam + 1
break Statements
If the execution reaches a break statement, it immediately exits the while loop’s clause:
14 / 96
README.md 11/27/2018
while True:
print('Please type your name.')
name = input()
if name == 'your name':
break
print('Thank you!')
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.')
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.
15 / 96
README.md 11/27/2018
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.
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:
Importing Modules
import random
for i in range(5):
print(random.randint(1, 10))
16 / 96
README.md 11/27/2018
import sys
while True:
print('Type exit to exit.')
response = input()
if response == 'exit':
sys.exit()
print('You typed {}.'.format(response))
Functions
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:
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'
17 / 96
README.md 11/27/2018
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)
Note: never compare to None with the == operator. Always use is.
18 / 96
README.md 11/27/2018
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.
If you need to modify a global variable from within a function, use the global statement:
There are four rules to tell whether a variable is in a local scope or global scope:
1. If a variable is being used in the global scope (that is, outside of all functions), then it is always a global
variable.
3. Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.
Exception Handling
Basic exception handling
21.0
3.5
Error: Invalid argument: division by zero
None
42.0
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.
Lists
>>> spam
['cat', 'bat', 'rat', 'elephant']
20 / 96
README.md 11/27/2018
>>> spam[1]
'bat'
>>> spam[2]
'rat'
>>> spam[3]
'elephant'
Negative Indexes
>>> spam[-3]
'bat'
21 / 96
README.md 11/27/2018
>>> spam[1:3]
['bat', 'rat']
>>> spam[0:-1]
['cat', 'bat', 'rat']
>>> spam[1:]
['bat', 'rat', 'elephant']
>>> spam
22 / 96
README.md 11/27/2018
>>> spam
['cat', 'aardvark', 'aardvark', 'elephant']
>>> spam
['cat', 'aardvark', 'aardvark', 12345]
>>> spam
[1, 2, 3, 'A', 'B', 'C']
23 / 96
README.md 11/27/2018
24 / 96
README.md 11/27/2018
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:
The multiple assignment trick can also be used to swap the values in two variables:
>>> print(b)
'Alice'
Operator Equivalent
Examples:
25 / 96
README.md 11/27/2018
>>> spam.index('Pooka')
1
append():
>>> spam.append('moose')
>>> spam
['cat', 'dog', 'bat', 'moose']
insert():
>>> spam
['cat', 'chicken', 'dog', 'bat']
26 / 96
README.md 11/27/2018
>>> 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.
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:
You can use the built-in function sorted to return a new list:
>>> 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.
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
28 / 96
README.md 11/27/2018
values():
keys():
items():
Using the keys(), values(), and items() methods, a for loop can iterate over the keys, values, or key-value pairs
in a dictionary, respectively.
29 / 96
README.md 11/27/2018
>>> # You can omit the call to keys() when checking for a key
>>> 'color' in spam
False
30 / 96
README.md 11/27/2018
>>> spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}
>>> spam
{'color': 'black', 'age': 5, 'name': 'Pooka'}
Pretty Printing
31 / 96
README.md 11/27/2018
'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}
# 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 bult-in function set()
>>> s = {1, 2, 3}
>>> s = set([1, 2, 3])
32 / 96
README.md 11/27/2018
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'>
>>> s = {1, 2, 3, 2, 3, 4}
>>> s
{1, 2, 3, 4}
>>> s = {1, 2, 3}
>>> s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>>
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}
>>> s = {1, 2, 3}
>>> s.update([2, 3, 4, 5, 6])
>>> s
{1, 2, 3, 4, 5, 6} # remember, sets automatically remove duplicates
33 / 96
README.md 11/27/2018
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
>>> 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 in one of the sets.
34 / 96
README.md 11/27/2018
>>> s1 = {1, 2, 3}
>>> s2 = {2, 3, 4}
>>> s1.difference(s2) # or 's1 - s2'
{1}
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}
itertools Module
The itertools module is a colection of tools intented to be fast and use memory efficiently when handling
iterators (like lists or dictionaries).
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.
accumulate()
itertools.accumulate(iterable[, func])
Example:
1
2
6
24
120
operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120
5
5 + 2 = 7
7 + 6 = 13
13 + 4 = 17
17 + 5 = 22
22 + 9 = 31
31 + 1 = 32
combinations()
Takes an iterable and a integer. This will create all the unique combination that have r members.
36 / 96
README.md 11/27/2018
itertools.combinations(iterable, r)
Example:
combinations_with_replacement()
Just like combinations(), but allows individual elements to be repeated more than once.
itertools.combinations_with_replacement(iterable, r)
Example:
count()
Makes an iterator that returns evenly spaced values starting with number start.
itertools.count(start=0, step=1)
Example:
37 / 96
README.md 11/27/2018
cycle()
itertools.cycle(iterable)
Example:
When reached the end of the iterable it start over again from the beginning.
chain()
itertools.chain(*iterables)
Example:
38 / 96
README.md 11/27/2018
compress()
itertools.compress(data, selectors)
Example:
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:
39 / 96
README.md 11/27/2018
filterfalse()
Makes an iterator that filters elements from iterable returning only those for which the predicate is False.
itertools.filterfalse(predicate, iterable)
Example:
groupby()
itertools.groupby(iterable, key=None)
Example:
40 / 96
README.md 11/27/2018
>>> 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'}]
islice()
This function is very much like slices. This allows you to cut out a piece of an iterable.
Example:
41 / 96
README.md 11/27/2018
red
orange
permutations()
itertools.permutations(iterable, r=None)
Example:
product()
repeat()
42 / 96
README.md 11/27/2018
This function will repeat an object over and over again. Unless, there is a times argument.
itertools.repeat(object[, times])
Example:
starmap()
Makes an iterator that computes the function using arguments obtained from the iterable.
itertools.starmap(function, iterable)
Example:
takewhile()
The opposite of dropwhile(). Makes an iterator and returns elements from the iterable as long as the predicate
is true.
itertools.takwwhile(predicate, iterable)
Example:
43 / 96
README.md 11/27/2018
tee()
itertools.tee(iterable, n=2)
Example:
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.
44 / 96
README.md 11/27/2018
itertools.zip_longest(*iterables, fillvalue=None)
Example:
Comprehensions
List comprehension
>>> [i - 1 for i in a]
[0, 2, 4, 6, 8, 10]
Set comprehension
Dict comprehension
45 / 96
README.md 11/27/2018
Manipulating Strings
Escape Characters
\t Tab
\\ Backslash
Example:
Raw Strings
A raw string completely ignores all escape characters and prints any backslash that appears in the string.
>>>
>>> Sincerely,
>>> Bob''')
Dear Alice,
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob
To keep a nicer flow in your code, you can use the dedent function from the textwrap standard package.
H e l l o w o r l d !
0 1 2 3 4 5 6 7 8 9 10 11
>>> spam[0]
'H'
>>> spam[4]
'o'
47 / 96
README.md 11/27/2018
>>> spam[-1]
'!'
Slicing:
>>> spam[0:5]
'Hello'
>>> spam[:5]
'Hello'
>>> spam[6:]
'world!'
>>> spam[6:-1]
'world'
>>> spam[:-1]
'Hello world'
>>> spam[::-1]
'!dlrow olleH'
>>> a = [1, 2, 3, 4]
>>> 5 in a
False
>>> 2 in a
True
>>> spam.isupper()
False
>>> 'HELLO'.isupper()
True
>>> 'abc12345'.islower()
True
>>> '12345'.islower()
False
>>> '12345'.isupper()
False
isalpha() returns True if the string consists only of letters and is not blank.
isalnum() returns True if the string consists only of lettersand numbers and is not blank.
isdecimal() returns True if the string consists only ofnumeric characters and is not blank.
isspace() returns True if the string consists only of spaces,tabs, and new-lines and is not blank.
istitle() returns True if the string consists only of wordsthat begin with an uppercase letter followed by
onlylowercase letters.
50 / 96
README.md 11/27/2018
>>> 'abc123'.startswith('abcdef')
False
>>> 'abc123'.endswith('12')
False
join():
split():
51 / 96
README.md 11/27/2018
>>> 'MyABCnameABCisABCSimon'.split('ABC')
['My', 'name', 'is', 'Simon']
>>> 'Hello'.rjust(10)
' Hello'
>>> 'Hello'.rjust(20)
' Hello'
>>> 'Hello'.ljust(10)
'Hello '
An optional second argument to rjust() and ljust() will specify a fill character other than a space character.
Enter the following into the interactive shell:
52 / 96
README.md 11/27/2018
center():
>>> 'Hello'.center(20)
' Hello '
>>> spam.lstrip()
'Hello World '
>>> spam.rstrip()
' Hello World'
Copying and Pasting Strings with the pyperclip Module (need pip install)
53 / 96
README.md 11/27/2018
>>> pyperclip.paste()
'Hello world!'
String Formatting
% operator
>>> num = 5
>>> 'I have %x apples' % num
"I have 5 apples"
Note: For new code, using str.format or f-strings (Python 3.6+) is strongly recommended over the % operator.
Python 3 introduced a new way to do string formatting that was later back-ported to Python 2.7. This makes
the syntax for string formatting more regular.
The official Python 3.x documentation recommend str.format over the % operator:
The formatting operations described here exhibit a variety of quirks that lead to a number of common
errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string
literals or the str.format() interface helps avoid these errors. These alternatives also provide more
powerful, flexible and extensible approaches to formatting text.
54 / 96
README.md 11/27/2018
You would only use %s string formatting on functions that can do lazy parameters evaluation, the most
common being logging:
Prefer:
Over:
Or:
>>> a = 5
>>> b = 10
>>> f'Five plus ten is {a + b} and not {2 * (a + b)}.'
'Five plus ten is 15 and not 30.'
Template Strings
A simpler and less powerful mechanism, but it is recommended when handling format strings generated by
users. Due to their reduced complexity template strings are a safer choice.
55 / 96
README.md 11/27/2018
Regular Expressions
1. Import the regex module with import re.
2. Create a Regex object with the re.compile() function. (Remember to use a raw string.)
3. Pass the string you want to search into the Regex object’s search() method. This returns a Match
object.
4. Call the Match object’s group() method to return a string of the actual matched text.
>>> import re
>>> mo.group(1)
'415'
>>> mo.group(2)
'555-4242'
56 / 96
README.md 11/27/2018
>>> mo.group(0)
'415-555-4242'
>>> mo.group()
'415-555-4242'
To retrieve all the groups at once: use the groups() method—note the plural form for the name.
>>> mo.groups()
('415', '555-4242')
>>> print(area_code)
415
>>> print(main_number)
555-4242
The | character is called a pipe. You can use it anywhere you want to match one of many expressions. For
example, the regular expression r'Batman|Tina Fey' will match either 'Batman' or 'Tina Fey'.
>>> mo1.group()
'Batman'
>>> mo2.group()
'Tina Fey'
You can also use the pipe to match one of several patterns as part of your regex:
>>> mo.group()
'Batmobile'
57 / 96
README.md 11/27/2018
>>> mo.group(1)
'mobile'
The ? character flags the group that precedes it as an optional part of the pattern.
The * (called the star or asterisk) means “match zero or more”—the group that precedes the star can occur
any number of times in the text.
While * means “match zero or more,” the + (or plus) means “match one or more”. The group preceding a plus
must appear at least once. It is not optional:
>>> mo1.group()
'Batwoman'
If you have a group that you want to repeat a specific number of times, follow the group in your regex with a
number in curly brackets. For example, the regex (Ha){3} will match the string 'HaHaHa', but it will not match
'HaHa', since the latter has only two repeats of the (Ha) group.
Instead of one number, you can specify a range by writing a minimum, a comma, and a maximum in between
the curly brackets. For example, the regex (Ha){3,5} will match 'HaHaHa', 'HaHaHaHa', and 'HaHaHaHaHa'.
Python’s regular expressions are greedy by default, which means that in ambiguous situations they will match
the longest string possible. The non-greedy version of the curly brackets, which matches the shortest string
possible, has the closing curly bracket followed by a question mark.
59 / 96
README.md 11/27/2018
>>> mo1.group()
'HaHaHaHaHa'
In addition to the search() method, Regex objects also have a findall() method. While search() will return a
Match object of the first matched text in the searched string, the findall() method will return the strings of
every match in the searched string.
When called on a regex with no groups, such as \d-\d\d\d-\d\d\d\d, the method findall() returns a list
of ng matches, such as ['415-555-9999', '212-555-0000'].
When called on a regex that has groups, such as (\d\d\d)-d\d)-(\d\ d\d\d), the method findall() returns
a list of es of strings (one string for each group), such as [('415', ', '9999'), ('212', '555', '0000')].
There are times when you want to match a set of characters but the shorthand character classes (\d, \w, \s,
and so on) are too broad. You can define your own character class using square brackets. For example, the
character class [aeiouAEIOU] will match any vowel, both lowercase and uppercase.
You can also include ranges of letters or numbers by using a hyphen. For example, the character class [a-zA-
Z0-9] will match all lowercase letters, uppercase letters, and numbers.
60 / 96
README.md 11/27/2018
By placing a caret character (^) just after the character class’s opening bracket, you can make a negative
character class. A negative character class will match all the characters that are not in the character class. For
example, enter the following into the interactive shell:
You can also use the caret symbol (^) at the start of a regex to indicate that a match must occur at the
beginning of the searched text.
Likewise, you can put a dollar sign ($) at the end of the regex to indicate the string must end with this
regex pattern.
And you can use the ^ and $ together to indicate that the entire string must match the regex—that is,
it’s not enough for a match to be made on some subset of the string.
The r'^Hello' regular expression string matches strings that begin with 'Hello':
The r'\d$' regular expression string matches strings that end with a numeric character from 0 to 9:
>>> whole_string_is_num.search('1234567890')
<_sre.SRE_Match object; span=(0, 10), match='1234567890'>
The . (or dot) character in a regular expression is called a wildcard and will match any character except for a
newline:
>>> mo.group(1)
'Al'
>>> mo.group(2)
'Sweigart'
The dot-star uses greedy mode: It will always try to match as much text as possible. To match any and all text
in a nongreedy fashion, use the dot, star, and question mark (.*?). The question mark tells Python to match in
a nongreedy way:
62 / 96
README.md 11/27/2018
The dot-star will match everything except a newline. By passing re.DOTALL as the second argument to
re.compile(), you can make the dot character match all characters, including the newline character:
Symbol Matches
\D, \W, and \S anything except a digit, word, or space acter, respectively.
Case-Insensitive Matching
63 / 96
README.md 11/27/2018
To make your regex case-insensitive, you can pass re.IGNORECASE or re.I as a second argument to
re.compile():
>>> robocop.search('Al, why does your programming book talk about robocop so
much?').group()
'robocop'
Another example:
>>> agent_names_regex.sub(r'\1****', 'Agent Alice told Agent Carol that Agent Eve
knew Agent Bob was a double agent.')
A**** told C**** that E**** knew B**** was a double agent.'
64 / 96
README.md 11/27/2018
To tell the re.compile() function to ignore whitespace and comments inside the regular expression string,
“verbose mode” can be enabled by passing the variable re.VERBOSE as the second argument to re.compile().
phone_regex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}(\s*
(ext|x|ext.)\s*\d{2,5})?)')
you can spread the regular expression over multiple lines with comments like this:
phone_regex = re.compile(r'''(
(\d{3}|\(\d{3}\))? # area code
(\s|-|\.)? # separator
\d{3} # first 3 digits
(\s|-|\.) # separator
\d{4} # last 4 digits
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
)''', re.VERBOSE)
On Windows, paths are written using backslashes () as the separator between folder names. On Unix based
operating system such as macOS, Linux, and BSDs, the forward slash (/) is used as the path separator. Joining
paths can be a headache if your code needs to work on different platforms.
Fortunately, Python provides easy ways to handle this. We will showcase how to deal with this with both
os.path.join and pathlib.Path.joinpath
>>> import os
65 / 96
README.md 11/27/2018
>>> print(Path('usr').joinpath('bin').joinpath('spam'))
usr/bin/spam
Notice the path separator is different between Windows and Unix based operating system, that's why you
want to use one of the above methods instead of adding strings together to join paths together.
Joining paths is helpful if you need to create different file paths under the same directory.
Using os on Windows:
66 / 96
README.md 11/27/2018
>>> import os
>>> os.getcwd()
'C:\\Python34'
>>> os.chdir('C:\\Windows\\System32')
>>> os.getcwd()
'C:\\Windows\\System32'
>>> print(Path.cwd())
/home/asweigart
>>> chdir('/usr/lib/python3.6')
>>> print(Path.cwd())
/usr/lib/python3.6
Using os on Windows:
>>> import os
>>> os.makedirs('C:\\delicious\\walnut\\waffles')
67 / 96
README.md 11/27/2018
Oh no, we got a nasty error! The reason is that the 'delicious' directory does not exist, so we cannot make the
'walnut' and the 'waffles' directories under it. To fix this, do:
There are also the dot (.) and dot-dot (..) folders. These are not real folders but special names that can be used
in a path. A single period (“dot”) for a folder name is shorthand for “this directory.” Two periods (“dot-dot”)
means “the parent folder.”
>>> import os
>>> os.path.isabs('/')
True
>>> os.path.isabs('..')
False
You can extract an absolute path with both os.path and pathlib
>>> import os
>>> os.getcwd()
'/home/asweigart'
>>> os.path.abspath('..')
'/home'
You can get a relative path from a starting path to another path.
>>> import os
>>> os.path.relpath('/etc/passwd', '/')
'etc/passwd'
import os
>>> os.path.exists('.')
True
>>> os.path.exists('setup.py')
True
>>> os.path.exists('/etc')
True
69 / 96
README.md 11/27/2018
>>> os.path.exists('nonexistentfile')
False
>>> import os
>>> os.path.isfile('setup.py')
True
>>> os.path.isfile('/home')
False
>>> os.path.isfile('nonexistentfile')
False
>>> import os
>>> os.path.isdir('/')
True
>>> os.path.isdir('setup.py')
70 / 96
README.md 11/27/2018
False
>>> os.path.isdir('/spam')
False
>>> import os
>>> os.path.getsize('C:\\Windows\\System32\\calc.exe')
776192
>>> import os
>>> os.listdir('C:\\Windows\\System32')
['0409', '12520437.cpx', '12520850.cpx', '5U877.ax', 'aaclient.dll',
--snip--
'xwtpdui.dll', 'xwtpw32.dll', 'zh-CN', 'zh-HK', 'zh-TW', 'zipfldr.dll']
71 / 96
README.md 11/27/2018
WARNING: Directories themselves also have a size! So you might want to check for whether a path is a file or
directory using the methods in the methods discussed in the above section!
>>> import os
>>> total_size = 0
>>> print(total_size)
1117846456
The shutil module provides functions for copying files, as well as entire folders.
>>> os.chdir('C:\\')
While shutil.copy() will copy a single file, shutil.copytree() will copy an entire folder and every folder and file
contained in it:
>>> os.chdir('C:\\')
The destination path can also specify a filename. In the following example, the source file is moved and
renamed:
If there is no eggs folder, then move() will rename bacon.txt to a file named eggs.
73 / 96
README.md 11/27/2018
Calling os.rmdir(path) or Path.rmdir() will delete the folder at path. This folder must be empty of any
files or folders.
Calling shutil.rmtree(path) will remove the folder at path, and all files and folders it contains will also be
deleted.
You can install this module by running pip install send2trash from a Terminal window.
>>> send2trash.send2trash('bacon.txt')
>>> import os
>>>
>>> for folder_name, subfolders, filenames in os.walk('C:\\delicious'):
>>> print('The current folder is {}'.format(folder_name))
>>>
>>> for subfolder in subfolders:
>>> print('SUBFOLDER OF {}: {}'.format(folder_name, subfolder))
>>> for filename in filenames:
>>> print('FILE INSIDE {}: {}'.format(folder_name, filename))
>>>
>>> print('')
The current folder is C:\delicious
SUBFOLDER OF C:\delicious: cats
SUBFOLDER OF C:\delicious: walnut
FILE INSIDE C:\delicious: spam.txt
74 / 96
README.md 11/27/2018
pathlib provides a lot more functionality than the ones listed above, like getting file name, getting file
extension, reading/writing a file without manually opening it, etc. Check out the official documentation if you
want to know more!
To read/write to a file in Python, you will want to use the with statement, which will close the file for you after
you are done.
>>> # Alternatively, you can use the *readlines()* method to get a list of string
values from the file, one string for each line of text:
>>> # You can also iterate through the file line by line:
>>> with open('sonnet29.txt') as sonnet_file:
... for line in sonnet_file: # note the new line character will be included in
the line
... print(line, end='')
75 / 96
README.md 11/27/2018
Writing to Files
>>> print(content)
Hello world!
Bacon is not a vegetable.
To save variables:
Just like dictionaries, shelf values have keys() and values() methods that will return list-like values of the keys
and values in the shelf. Since these methods return list-like values instead of true lists, you should pass them
to the list() function to get them in list form.
>>> pprint.pformat(cats)
"[{'desc': 'chubby', 'name': 'Zophie'}, {'desc': 'fluffy', 'name': 'Pooka'}]"
The extractall() method for ZipFile objects extracts all the files and folders from a ZIP file into the current
working directory.
The extract() method for ZipFile objects will extract a single file from the ZIP file. Continue the interactive shell
example:
This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt.
import json
with open("filename.json", "r") as f:
content = json.loads(f.read())
import json
78 / 96
README.md 11/27/2018
YAML
Compared to JSON, YAML allows a much better humain maintainance and gives ability to add comments. It is
a convinient choice for configuration files where human will have to edit.
PyYaml
Ruamel.yaml
The first one it easier to use but the second one, Ruamel, implements much better the YAML specification, and
allow for example to modify a YAML content without altering comments.
with open("filename.yaml") as f:
yaml=YAML()
yaml.load(f)
Anyconfig
Anyconfig is a very handy package allowing to abstract completly the underlying configuration file format. It
allows to load a Python dictionary from JSON, YAML, TOML, and so on.
Install it with:
Usage:
import anyconfig
conf1 = anyconfig.load("/path/to/foo/conf.d/a.yml")
Debugging
Raising Exceptions
79 / 96
README.md 11/27/2018
Exceptions are raised with a raise statement. In code, a raise statement consists of the following:
Often it’s the code that calls the function, not the function itself, that knows how to handle an expection. So
you will commonly see a raise statement inside a function and the try and except statements in the code
calling the function.
The traceback is displayed by Python whenever a raised exception goes unhandled. But can also obtain it as a
string by calling traceback.format_exc(). This function is useful if you want the information from an exception’s
traceback but also want an except statement to gracefully handle the exception. You will need to import
Python’s traceback module before calling this function.
>>> try:
>>> raise Exception('This is the error message.')
>>> except:
80 / 96
README.md 11/27/2018
The 116 is the return value from the write() method, since 116 characters were written to the file. The
traceback text was written to errorInfo.txt.
Assertions
An assertion is a sanity check to make sure your code isn’t doing something obviously wrong. These sanity
checks are performed by assert statements. If the sanity check fails, then an AssertionError exception is raised.
In code, an assert statement consists of the following:
>>> assert pod_bay_door_status == 'open', 'The pod bay doors need to be "open".'
>>> assert pod_bay_door_status == 'open', 'The pod bay doors need to be "open".'
In plain English, an assert statement says, “I assert that this condition holds true, and if not, there is a bug
somewhere in the program.” Unlike exceptions, your code should not handle assert statements with try and
except; if an assert fails, your program should crash. By failing fast like this, you shorten the time between the
original cause of the bug and when you first notice the bug. This will reduce the amount of code you will have
to check before finding the code that’s causing the bug.
Disabling Assertions
81 / 96
README.md 11/27/2018
Logging
To enable the logging module to display log messages on your screen as your program runs, copy the
following to the top of your program (but under the #! python shebang line):
import logging
Say you wrote a function to calculate the factorial of a number. In mathematics, factorial 4 is 1 × 2 × 3 × 4, or
24. Factorial 7 is 1 × 2 × 3 × 4 × 5 × 6 × 7, or 5,040. Open a new file editor window and enter the following
code. It has a bug in it, but you will also enter several log messages to help yourself figure out what is going
wrong. Save the program as factorialLog.py.
0
2015-05-23 16:20:12,684 - DEBUG - End of program
Logging Levels
Logging levels provide a way to categorize your log messages by importance. There are five logging levels,
described in Table 10-1 from least to most important. Messages can be logged at each level using a different
logging function.
The lowest level. Used for small details. Usually you care about
DEBUG logging.debug()
these messages only when diagnosing problems.
The highest level. Used to indicate a fatal error that has caused or is
CRITICAL logging.critical()
about to cause the program to stop running entirely.
Disabling Logging
After you’ve debugged your program, you probably don’t want all these log messages cluttering the screen.
The logging.disable() function disables these so that you don’t have to go into your program and remove all
the logging calls by hand.
>>> logging.disable(logging.CRITICAL)
83 / 96
README.md 11/27/2018
Logging to a File
Instead of displaying the log messages to the screen, you can write them to a text file. The
logging.basicConfig() function takes a filename keyword argument, like so:
import logging
Lambda Functions
This function:
>>> add(5, 3)
8
>>> plus_3(4)
84 / 96
README.md 11/27/2018
7
>>> plus_5(4)
9
Note: lambda can only evaluate an expression, like a single line of code.
Example:
>>> age = 15
>>> age = 15
>>> print('kid' if age < 13 else 'teenager' if age < 18 else 'adult')
teenager
The names args and kwargs are arbitrary - the important thing are the * and ** operators. They can mean:
1. In a function declaration, * means “pack all remaining positional arguments into a tuple named
<name>”, while ** is the same for keyword arguments (except it uses a dictionary, not a tuple).
2. In a function call, * means “unpack tuple or list named <name> to positional arguments at this position”,
while ** is the same for keyword arguments.
For example you can make a function that you can use to call any other function, no matter what parameters
it has:
Inside forward, args is a tuple (of all positional arguments except the first one, because we specified it - the f),
kwargs is a dict. Then we call f and unpack them so they become normal arguments to f.
You use *args when you have an indefinite amount of positional arguments.
"apples"
"bananas"
"grapes"
Similarly, you use **kwargs when you have an indefinite number of keyword arguments.
name: apple
color: red
>>> print(kwargs)
Thinks to Remember(args)
1. Functions can accept a variable number of positional arguments by using *args in the def statement.
2. You can use the items from a sequence as the positional arguments for a function with the * operator.
3. Using the * operator with a generator may cause your program to run out of memory and crash.
4. Adding new positional parameters to functions that accept *args can introduce hard-to-find bugs.
Thinks to remember(kwargs)
Context Manager
87 / 96
README.md 11/27/2018
While Python's context managers are widely used, few understand the purpose behind their use. These
statements, commonly used with reading and writing files, assist the application in conserving system memory
and improve resource management by ensuring specific resources are only in use for certain processes.
with statement
A context manager is an object that is notified when a context (a block of code) starts and ends. You
commonly use one with the with statement. It takes care of the notifying.
For example, file objects are context managers. When a context ends, the file object is closed automatically:
Anything that ends execution of the block causes the context manager's exit method to be called. This
includes exceptions, and can be useful when an error causes you to prematurely exit from an open file or
connection. Exiting a script without properly closing files/connections is a bad idea, that may cause data loss
or other problems. By using a context manager you can ensure that precautions are always taken to prevent
damage or loss in this way.
It is also possible to write a context manager using generator syntax thanks to the
contextlib.contextmanager decorator:
>>>
88 / 96
README.md 11/27/2018
__main__ is the name of the scope in which top-level code executes. A module’s name is set equal to
__main__ when read from standard input, a script, or from an interactive prompt.
A module can discover whether or not it is running in the main scope by checking its own __name__, which
allows a common idiom for conditionally executing code in a module when it is run as a script or with python
-m but not when it is imported:
For a package, the same effect can be achieved by including a main.py module, the contents of which will be
executed when the module is run with -m
For example we are developing script which is designed to be used as module, we should do:
Advantages
1. Every Python module has it’s __name__ defined and if this is __main__, it implies that the module is
being run standalone by the user and we can do corresponding appropriate actions.
2. If you import this script as a module in another script, the name is set to the name of the
script/module.
3. Python files can act as either reusable modules, or as standalone programs.
4. if __name__ == “main”: is used to execute some code only if the file was run directly, and not
imported.
setup.py
89 / 96
README.md 11/27/2018
The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils.
The main purpose of the setup script is to describe your module distribution to the Distutils, so that the
various commands that operate on your modules do the right thing.
The setup.py file is at the heart of a Python project. It describes all of the metadata about your project. There
a quite a few fields you can add to a project to give it a rich set of metadata describing the project. However,
there are only three required fields: name, version, and packages. The name field must be unique if you wish
to publish your package on the Python Package Index (PyPI). The version field keeps track of different releases
of the project. The packages field describes where you’ve put the Python source code within your project.
This allows you to easily install Python packages. Often it's enough to write:
Our initial setup.py will also include information about the license and will re-use the README.txt file for the
long_description field. This will look like:
Dataclasses
Dataclasses are python classes but are suited for storing data objects. This module provides a decorator and
functions for automatically adding generated special methods such as __init__() and __repr__() to user-
defined classes.
Features
1. They store data and represent a certain data type. Ex: A number. For people familiar with ORMs, a
model instance is a data object. It represents a specific kind of entity. It holds attributes that define or
represent the entity.
2. They can be compared to other objects of the same type. Ex: A number can be greater than, less than,
or equal to another number.
Python 3.7 provides a decorator dataclass that is used to convert a class into a dataclass.
90 / 96
README.md 11/27/2018
python 2.7
with dataclass
>>> @dataclass
... class Number:
... val: int
...
>>> obj = Number(2)
>>> obj.val
2
Default values
>>> @dataclass
... class Product:
... name: str
... count: int = 0
... price: float = 0.0
...
>>> obj = Product("Python")
>>> obj.name
Python
>>> obj.count
0
>>> obj.price
0.0
Type hints
It is mandatory to define the data type in dataclass. However, If you don't want specify the datatype then, use
typing.Any.
91 / 96
README.md 11/27/2018
>>> @dataclass
... class WithoutExplicitTypes:
... name: Any
... value: Any = 42
...
Virtual Environment
The use of a Virtual Environment is to test python code in encapsulated environments and to also avoid filling
the base Python installation with libraries we might use for only one project.
virtualenv
1. Install virtualenv
Usage:
mkvirtualenv HelloWold
Anything we install now will be specific to this project. And available to the projects we connect to this
environment.
To bind our virtualenv with our current working directory we simply enter:
setprojectdir .
92 / 96
README.md 11/27/2018
3. Deactivate
To move onto something else in the command line type ‘deactivate’ to deactivate your environment.
deactivate
4. Workon
Open up the command prompt and type ‘workon HelloWold’ to activate the environment and move
into your root project folder
workon HelloWold
poetry
Poetry is a tool for dependency management and packaging in Python. It allows you to declare the
libraries your project depends on and it will manage (install/update) them for you.
1. Install Poetry
my-project
├── pyproject.toml
├── README.rst
├── poetry_demo
│ └── __init__.py
└── tests
├── __init__.py
└── test_poetry_demo.py
The pyproject.toml file will orchestrate your project and its dependencies:
93 / 96
README.md 11/27/2018
[tool.poetry]
name = "my-project"
version = "0.1.0"
description = ""
authors = ["your name <your@mail.com>"]
[tool.poetry.dependencies]
python = "*"
[tool.poetry.dev-dependencies]
pytest = "^3.4"
3. Packages
To add dependencies to your project, you can specify them in the tool.poetry.dependencies section:
[tool.poetry.dependencies]
pendulum = "^1.4"
Also, instead of modifying the pyproject.toml file by hand, you can use the add command and it will
automatically find a suitable version constraint.
poetry install
To remove dependencies:
pipenv
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo,
yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.
1. Install pipenv
94 / 96
README.md 11/27/2018
2. Enter your Project directory and install the Packages for your project
cd my_project
pipenv install <package>
Pipenv will install your package and create a Pipfile for you in your project’s directory. The Pipfile is
used to track which dependencies your project needs in case you need to re-install them.
3. Uninstall Packages
pipenv shell
exit
anaconda
Where packages, notebooks, projects and environments are shared. Your place for free public conda
package hosting.
Usage:
95 / 96
README.md 11/27/2018
conda deactivate
96 / 96