Python Cheat Sheet
Lorem ipsum
Python Cheat Sheet
Python Basics
Math Operators
From Highest to Lowest precedence:
Operators | Operation | Example |
---|---|---|
** | Exponent |
|
% | Modulus/Remaider |
|
// | Integer division |
|
/ | Division |
|
* | Multiplication |
|
- | Subtraction |
|
+ | Addition |
|
Examples of expressions in the interactive shell:
Data Types
Data Type | Examples |
---|---|
Integers |
|
Floating-point numbers |
|
Strings |
|
String Concatenation and Replication
String concatenation:
Note: Avoid +
operator for string concatenation. Prefer string formatting.
String Replication:
Variables
You can name a variable anything as long as it obeys the following three 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
should not be used again in the code.
Comments
Inline comment:
Multiline comment:
Code with a comment:
Please note the two spaces in front of the comment.
Function docstring:
The print Function
The input Function
Example Code:
The len Function
Evaluates to the integer value of the number of characters in a string:
Note: test of emptiness of strings, lists, dictionary, etc, should not use len, but prefer direct boolean evaluation.
The str, int, and float Functions
Integer to String or Float:
Float to Integer:
Flow Control
Comparison Operators
Operator | Meaning |
---|---|
| 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:
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):
YES (even if they are valid Python):
These statements are equivalent:
And these as well:
Boolean Operators
There are three Boolean operators: and, or, and not.
The and Operator's Truth Table:
Expression | Evaluates to |
---|---|
True and True | True |
True and False | False |
False and True | False |
False and False | False |
The or Operator's Truth Table:
Expression | Evaluates to |
---|---|
True or True | True |
True or False | True |
False or True | True |
False or False | False |
The not Operator's Truth Table:
Expression | Evaluates to |
---|---|
not True | False |
not False | True |
Mixing Boolean and Comparison Operators
You can also use multiple Boolean operators in an expression, along with the comparison operators:
if Statements
else Statements
elif Statements
while Loop Statements
break Statements
If the execution reaches a break statement, it immediately exits the while loop's clause:
continue Statements
When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop.
for Loops and the range() Function
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.
You can even use a negative number for the step argument to make the for loop count down instead of up.
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:
Importing Modules
Ending a Program with sys.exit
Functions
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.
The None Value
Note: never compare to None
with the ==
operator. Always use is
.
print Keyword Arguments
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.
The global Statement
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:
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.
Exception Handling
Basic exception handling
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.
Lists
Getting Individual Values in a List with Indexes
Negative Indexes
Getting Sublists with Slices
Getting a list 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
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:
You could type this line of code:
The multiple assignment trick can also be used to swap the values in two variables:
Augmented Assignment Operators
Operator | Equivalent |
---|---|
|
|
|
|
|
|
|
|
|
|
Examples:
Finding a Value in a List with the index Method
Adding Values to Lists with append and insert
append():
insert():
Removing Values from Lists with remove
If the value appears multiple times in the list, only the first instance of the value will be removed.
Sorting the Values in a List with sort
You can also pass True for the reverse keyword argument to have sort() sort the values in reverse order:
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:
Tuple Data Type
The main way that tuples are different from lists is that tuples, like strings, are immutable.
Converting Types with the list and tuple Functions
Dictionaries and Structuring Data
Example Dictionary:
The keys, values, and items Methods
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.
Checking if a Key or Value Exists in a Dictionary
The get Method
The setdefault Method
Let's consider this code:
Using setdefault
we could make the same code more shortly:
Pretty Printing
Merge two dictionaries
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()
When creating an empty set, be sure to not use the curly braces {}
or you will get an empty dictionary instead.
sets: unordered collections of unique elements
A set automatically remove all the duplicate values.
And as an unordered data type, they can't be indexed.
set add and update
Using the add()
method we can add a single element to the set.
And with update()
, multiple ones .
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.
discard()
won't raise any errors.
set union
union()
or |
will create a new set that contains all the elements from the sets provided.
set intersection
intersection
or &
will return a set containing only the elements that are common to all of them.
set difference
difference
or -
will return only the elements that are in one of the sets.
set symetric_difference
symetric_difference
or ^
will return all the elements that are not common between them.
itertools Module
The itertools module is a collection of tools intented 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.
accumulate
Makes an iterator that returns the results of a function.
Example:
The operator.mul takes two numbers and multiplies them:
Passing a function is optional:
If no function is designated the items will be summed:
combinations
Takes an iterable and a integer. This will create all the unique combination that have r members.
Example:
combinations_with_replacement
Just like combinations(), but allows individual elements to be repeated more than once.
Example:
count
Makes an iterator that returns evenly spaced values starting with number start.
Example:
cycle
This function cycles through an iterator endlessly.
Example:
When reached the end of the iterable it start over again from the beginning.
chain
Take a series of iterables and return them as one long iterable.
Example:
compress
Filters one iterable with another.
Example:
dropwhile
Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element.
Example:
filterfalse
Makes an iterator that filters elements from iterable returning only those for which the predicate is False.
Example:
groupby
Simply put, this function groups things together.
Example:
islice
This function is very much like slices. This allows you to cut out a piece of an iterable.
Example:
permutations
Example:
product
Creates the cartesian products from a series of iterables.
repeat
This function will repeat an object over and over again. Unless, there is a times argument.
Example:
starmap
Makes an iterator that computes the function using arguments obtained from the iterable.
Example:
takewhile
The opposite of dropwhile(). Makes an iterator and returns elements from the iterable as long as the predicate is true.
Example:
tee
Return n independent iterators from a single iterable.
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.
Example:
Comprehensions
List comprehension
Set comprehension
Dict comprehension
A List comprehension can be generated from a dictionary:
Manipulating Strings
Escape Characters
Escape character | Prints as |
---|---|
| Single quote |
| Double quote |
Tab | |
Newline (line break) | |
| Backslash |
Example:
Raw Strings
A raw string completely ignores all escape characters and prints any backslash that appears in the string.
Note: mostly used for regular expression definition (see re
package)
Multiline Strings with Triple Quotes
To keep a nicer flow in your code, you can use the dedent
function from the textwrap
standard package.
This generates the same string than before.
Indexing and Slicing Strings
Slicing:
The in and not in Operators with Strings
The in and not in Operators with list
The upper, lower, isupper, and islower String Methods
upper()
and lower()
:
isupper() and islower():
The isX String Methods
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.
The startswith and endswith String Methods
The join and split String Methods
join():
split():
Justifying Text with rjust, ljust, and center
rjust() and ljust():
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:
center():
Removing Whitespace with strip, rstrip, and lstrip
Copying and Pasting Strings with the pyperclip Module
First, install pypeerclip
with pip:
String Formatting
% operator
We can use the %x
format specifier to convert an int value to a string:
Note: For new code, using str.format or f-strings is strongly recommended over the %
operator.
str.format
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.
Lazy string formatting
You would only use %s
string formatting on functions that can do lazy parameters evaluation, the most common being logging:
Prefer:
Over:
Or:
Formatted String Literals or f-strings
Python 3.6+
It is even possible to do inline arithmetic with it:
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.
Regular Expressions
Import the regex module with
import re
.Create a Regex object with the
re.compile()
function. (Remember to use a raw string.)Pass the string you want to search into the Regex object's
search()
method. This returns aMatch
object.Call the Match object's
group()
method to return a string of the actual matched text.
All the regex functions in Python are in the re module:
Matching Regex Objects
Grouping with Parentheses
To retrieve all the groups at once: use the groups() method—note the plural form for the name.
Matching Multiple Groups with the Pipe
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'.
You can also use the pipe to match one of several patterns as part of your regex:
Optional Matching with the Question Mark
The ? character flags the group that precedes it as an optional part of the pattern.
Matching Zero or More with the Star
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.
Matching One or More with the Plus
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:
Matching Specific Repetitions with Curly Brackets
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'.
Greedy and Nongreedy Matching
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.
The findall Method
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.
To summarize what the findall() method returns, remember the following:
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')].
Making Your Own Character Classes
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.
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:
The Caret and Dollar Sign Characters
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:
The Wildcard Character
The . (or dot) character in a regular expression is called a wildcard and will match any character except for a newline:
Matching Everything with Dot-Star
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:
Matching Newlines with the Dot Character
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:
Review of Regex Symbols
Symbol | Matches |
---|---|
| zero or one of the preceding group. |
| zero or more of the preceding group. |
| one or more of the preceding group. |
| exactly n of the preceding group. |
| n or more of the preceding group. |
| 0 to m of the preceding group. |
| at least n and at most m of the preceding p. |
| performs a nongreedy match of the preceding p. |
| means the string must begin with spam. |
| means the string must end with spam. |
| any character, except newline characters. |
| a digit, word, or space character, resectively. |
| anything except a digit, word, or space acter, respectively. |
| any character between the brackets (such as a, b, ). |
| any character that isn't between the brackets. |
Case-Insensitive Matching
To make your regex case-insensitive, you can pass re.IGNORECASE or re.I as a second argument to re.compile():
Substituting Strings with the sub() Method
The sub() method for Regex objects is passed two arguments:
The first argument is a string to replace any matches.
The second is the string for the regular expression.
The sub() method returns a string with the substitutions applied:
Another example:
Managing Complex Regexes
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().
Now instead of a hard-to-read regular expression like this:
you can spread the regular expression over multiple lines with comments like this:
Handling File and Directory Paths
There are two main modules in Python that deals with path manipulation. One is the os.path
module and the other is the pathlib
module. The pathlib
module was added in Python 3.4, offering an object-oriented way to handle file system paths.
Backslash on Windows and Forward Slash on OS X and Linux
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
Using os.path.join
on Windows:
And using pathlib
on *nix:
pathlib
also provides a shortcut to joinpath using the /
operator:
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.path.join
on Windows:
Using pathlib
on *nix:
The Current Working Directory
Using os
on Windows:
Using pathlib
on *nix:
Creating New Folders
Using os
on Windows:
Using pathlib
on *nix:
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:
And all is good :)
Absolute vs. Relative Paths
There are two ways to specify a file path.
An absolute path, which always begins with the root folder
A relative path, which is relative to the program's current working directory
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.”
Handling Absolute and Relative Paths
To see if a path is an absolute path:
Using os.path
on *nix:
Using pathlib
on *nix:
You can extract an absolute path with both os.path
and pathlib
Using os.path
on *nix:
Using pathlib
on *nix:
You can get a relative path from a starting path to another path.
Using os.path
on *nix:
Using pathlib
on *nix:
Checking Path Validity
Checking if a file/directory exists:
Using os.path
on *nix:
Using pathlib
on *nix:
Checking if a path is a file:
Using os.path
on *nix:
Using pathlib
on *nix:
Checking if a path is a directory:
Using os.path
on *nix:
Using pathlib
on *nix:
Finding File Sizes and Folder Contents
Getting a file's size in bytes:
Using os.path
on Windows:
Using pathlib
on *nix:
Listing directory contents using os.listdir
on Windows:
Listing directory contents using pathlib
on *nix:
To find the total size of all the files in this directory:
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!
Using os.path.getsize()
and os.listdir()
together on Windows:
Using pathlib
on *nix:
Copying Files and Folders
The shutil module provides functions for copying files, as well as entire folders.
While shutil.copy() will copy a single file, shutil.copytree() will copy an entire folder and every folder and file contained in it:
Moving and Renaming Files and Folders
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.
Permanently Deleting Files and Folders
Calling os.unlink(path) or Path.unlink() will delete the file at path.
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.
Safe Deletes with the send2trash Module
You can install this module by running pip install send2trash from a Terminal window.
Walking a Directory Tree
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!
Reading and Writing Files
The File Reading/Writing Process
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.
Opening and reading files with the open function
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:
Writing to Files
Saving Variables with the shelve Module
To save variables:
To open and read 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.
Saving Variables with pprint.pformat
Reading ZIP Files
Extracting from ZIP Files
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:
Creating and Adding to ZIP Files
This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt.
JSON, YAML and configuration files
JSON
Open a JSON file with:
Write a JSON file with:
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.
There are two main librairies allowing to access to YAML files:
Install them using pip install
in your virtual environment.
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.
Open a YAML file with:
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:
Debugging
Raising Exceptions
Exceptions are raised with a raise statement. In code, a raise statement consists of the following:
The raise keyword
A call to the Exception() function
A string with a helpful error message passed to the Exception() function
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.
Getting the Traceback as a String
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.
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:
The assert keyword
A condition (that is, an expression that evaluates to True or False)
A comma
A string to display when the condition is False
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
Assertions can be disabled by passing the -O option when running Python.
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):
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.
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.
Level | Logging Function | Description |
---|---|---|
|
| The lowest level. Used for small details. Usually you care about these messages only when diagnosing problems. |
|
| Used to record information on general events in your program or confirm that things are working at their point in the program. |
|
| Used to indicate a potential problem that doesn't prevent the program from working but might do so in the future. |
|
| Used to record an error that caused the program to fail to do something. |
|
| The highest level. Used to indicate a fatal error that has caused or is 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 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:
Lambda Functions
This function:
Is equivalent to the lambda function:
It's not even need to bind it to a name like add before:
Like regular nested functions, lambdas also work as lexical closures:
Note: lambda can only evaluate an expression, like a single line of code.
Ternary Conditional Operator
Many programming languages have a ternary operator, which define a conditional expression. The most common usage is to make a terse simple conditional assignment statement. In other words, it offers one-line code to evaluate the first expression if the condition is true, otherwise it evaluates the second expression.
Example:
Ternary operators can be changed:
The code above is equivalent to:
args and kwargs
The names args and kwargs
are arbitrary - the important thing are the *
and **
operators. They can mean:
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).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.
Similarly, you use **kwargs
when you have an indefinite number of keyword arguments.
If you do not specify ** for kwargs
Thinks to Remember(args)
Functions can accept a variable number of positional arguments by using
*args
in the def statement.You can use the items from a sequence as the positional arguments for a function with the
*
operator.Using the
*
operator with a generator may cause your program to run out of memory and crash.Adding new positional parameters to functions that accept
*args
can introduce hard-to-find bugs.
Thinks to remember(kwargs)
Function arguments can be specified by position or by keyword.
Keywords make it clear what the purpose of each argument is when it would be confusing with only positional arguments.
Keyword arguments with default values make it easy to add new behaviors to a function, especially when the function has existing callers.
Optional keyword arguments should always be passed by keyword instead of by position.
Context Manager
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.
Writing your own contextmanager using generator syntax
It is also possible to write a context manager using generator syntax thanks to the contextlib.contextmanager
decorator:
__main__
Top-level script environment
__main__
Top-level script environment__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
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.If you import this script as a module in another script, the name is set to the name of the script/module.
Python files can act as either reusable modules, or as standalone programs.
if
__name__ == “main”:
is used to execute some code only if the file was run directly, and not imported.
setup.py
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:
and module will install itself.
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:
Find more information visit http://docs.python.org/install/index.html.
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
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.
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.
python 2.7
with dataclass
Default values
It is easy to add default values to the fields of your data class.
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
.
PART 2:
Single line comments start with a number symbol.
""" Multiline strings can be written using three "s, and are often used as documentation. """
####################################################
1. Primitive Datatypes and Operators
####################################################
You have numbers
3 # => 3
Math is what you would expect
1 + 1 # => 2 8 - 1 # => 7 10 * 2 # => 20 35 / 5 # => 7.0
Integer division rounds down for both positive and negative numbers.
5 // 3 # => 1 -5 // 3 # => -2 5.0 // 3.0 # => 1.0 # works on floats too -5.0 // 3.0 # => -2.0
The result of division is always a float
10.0 / 3 # => 3.3333333333333335
Modulo operation
7 % 3 # => 1
Exponentiation (x**y, x to the yth power)
2**3 # => 8
Enforce precedence with parentheses
(1 + 3) * 2 # => 8
Boolean values are primitives (Note: the capitalization)
True False
negate with not
not True # => False not False # => True
Boolean Operators
Note "and" and "or" are case-sensitive
True and False # => False False or True # => True
True and False are actually 1 and 0 but with different keywords
True + True # => 2 True * 8 # => 8 False - 5 # => -5
Comparison operators look at the numerical value of True and False
0 == False # => True 1 == True # => True 2 == True # => False -5 != False # => True
Using boolean logical operators on ints casts them to booleans for evaluation, but their non-cast value is returned
Don't mix up with bool(ints) and bitwise and/or (&,|)
bool(0) # => False bool(4) # => True bool(-6) # => True 0 and 2 # => 0 -5 or 0 # => -5
Equality is ==
1 == 1 # => True 2 == 1 # => False
Inequality is !=
1 != 1 # => False 2 != 1 # => True
More comparisons
1 < 10 # => True 1 > 10 # => False 2 <= 2 # => True 2 >= 2 # => True
Seeing whether a value is in a range
1 < 2 and 2 < 3 # => True 2 < 3 and 3 < 2 # => False
Chaining makes this look nicer
1 < 2 < 3 # => True 2 < 3 < 2 # => False
(is vs. ==) is checks if two variables refer to the same object, but == checks
if the objects pointed to have the same values.
a = [1, 2, 3, 4] # Point a at a new list, [1, 2, 3, 4] b = a # Point b at what a is pointing to b is a # => True, a and b refer to the same object b == a # => True, a's and b's objects are equal b = [1, 2, 3, 4] # Point b at a new list, [1, 2, 3, 4] b is a # => False, a and b do not refer to the same object b == a # => True, a's and b's objects are equal
Strings are created with " or '
"This is a string." 'This is also a string.'
Strings can be added too! But try not to do this.
"Hello " + "world!" # => "Hello world!"
String literals (but not variables) can be concatenated without using '+'
"Hello " "world!" # => "Hello world!"
A string can be treated like a list of characters
"This is a string"[0] # => 'T'
You can find the length of a string
len("This is a string") # => 16
.format can be used to format strings, like this:
"{} can be {}".format("Strings", "interpolated") # => "Strings can be interpolated"
You can repeat the formatting arguments to save some typing.
"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick")
=> "Jack be nimble, Jack be quick, Jack jump over the candle stick"
You can use keywords if you don't want to count.
"{name} wants to eat {food}".format(name="Bob", food="lasagna") # => "Bob wants to eat lasagna"
If your Python 3 code also needs to run on Python 2.5 and below, you can also
still use the old style of formatting:
"%s can be %s the %s way" % ("Strings", "interpolated", "old") # => "Strings can be interpolated the old way"
You can also format using f-strings or formatted string literals (in Python 3.6+)
name = "Reiko" f"She said her name is {name}." # => "She said her name is Reiko"
You can basically put any Python statement inside the braces and it will be output in the string.
f"{name} is {len(name)} characters long." # => "Reiko is 5 characters long."
None is an object
None # => None
Don't use the equality "==" symbol to compare objects to None
Use "is" instead. This checks for equality of object identity.
"etc" is None # => False None is None # => True
None, 0, and empty strings/lists/dicts/tuples all evaluate to False.
All other values are True
bool(0) # => False bool("") # => False bool([]) # => False bool({}) # => False bool(()) # => False
####################################################
2. Variables and Collections
####################################################
Python has a print function
print("I'm Python. Nice to meet you!") # => I'm Python. Nice to meet you!
By default the print function also prints out a newline at the end.
Use the optional argument end to change the end string.
print("Hello, World", end="!") # => Hello, World!
Simple way to get input data from console
input_string_var = input("Enter some data: ") # Returns the data as a string
Note: In earlier versions of Python, input() method was named as raw_input()
There are no declarations, only assignments.
Convention is to use lower_case_with_underscores
some_var = 5 some_var # => 5
Accessing a previously unassigned variable is an exception.
See Control Flow to learn more about exception handling.
some_unknown_var # Raises a NameError
if can be used as an expression
Equivalent of C's '?:' ternary operator
"yahoo!" if 3 > 2 else 2 # => "yahoo!"
Lists store sequences
li = []
You can start with a prefilled list
other_li = [4, 5, 6]
Add stuff to the end of a list with append
li.append(1) # li is now [1] li.append(2) # li is now [1, 2] li.append(4) # li is now [1, 2, 4] li.append(3) # li is now [1, 2, 4, 3]
Remove from the end with pop
li.pop() # => 3 and li is now [1, 2, 4]
Let's put it back
li.append(3) # li is now [1, 2, 4, 3] again.
Access a list like you would any array
li[0] # => 1
Look at the last element
li[-1] # => 3
Looking out of bounds is an IndexError
li[4] # Raises an IndexError
You can look at ranges with slice syntax.
The start index is included, the end index is not
(It's a closed/open range for you mathy types.)
li[1:3] # Return list from index 1 to 3 => [2, 4] li[2:] # Return list starting from index 2 => [4, 3] li[:3] # Return list from beginning until index 3 => [1, 2, 4] li[::2] # Return list selecting every second entry => [1, 4] li[::-1] # Return list in reverse order => [3, 4, 2, 1]
Use any combination of these to make advanced slices
li[start🔚step]
Make a one layer deep copy using slices
li2 = li[:] # => li2 = [1, 2, 4, 3] but (li2 is li) will result in false.
Remove arbitrary elements from a list with "del"
del li[2] # li is now [1, 2, 3]
Remove first occurrence of a value
li.remove(2) # li is now [1, 3] li.remove(2) # Raises a ValueError as 2 is not in the list
Insert an element at a specific index
li.insert(1, 2) # li is now [1, 2, 3] again
Get the index of the first item found matching the argument
li.index(2) # => 1 li.index(4) # Raises a ValueError as 4 is not in the list
You can add lists
Note: values for li and for other_li are not modified.
li + other_li # => [1, 2, 3, 4, 5, 6]
Concatenate lists with "extend()"
li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6]
Check for existence in a list with "in"
1 in li # => True
Examine the length with "len()"
len(li) # => 6
Tuples are like lists but are immutable.
tup = (1, 2, 3) tup[0] # => 1 tup[0] = 3 # Raises a TypeError
Note that a tuple of length one has to have a comma after the last element but
tuples of other lengths, even zero, do not.
type((1)) # => <class 'int'> type((1,)) # => <class 'tuple'> type(()) # => <class 'tuple'>
You can do most of the list operations on tuples too
len(tup) # => 3 tup + (4, 5, 6) # => (1, 2, 3, 4, 5, 6) tup[:2] # => (1, 2) 2 in tup # => True
You can unpack tuples (or lists) into variables
a, b, c = (1, 2, 3) # a is now 1, b is now 2 and c is now 3
You can also do extended unpacking
a, *b, c = (1, 2, 3, 4) # a is now 1, b is now [2, 3] and c is now 4
Tuples are created by default if you leave out the parentheses
d, e, f = 4, 5, 6 # tuple 4, 5, 6 is unpacked into variables d, e and f
respectively such that d = 4, e = 5 and f = 6
Now look how easy it is to swap two values
e, d = d, e # d is now 5 and e is now 4
Dictionaries store mappings from keys to values
empty_dict = {}
Here is a prefilled dictionary
filled_dict = {"one": 1, "two": 2, "three": 3}
Note keys for dictionaries have to be immutable types. This is to ensure that
the key can be converted to a constant hash value for quick look-ups.
Immutable types include ints, floats, strings, tuples.
invalid_dict = {[1,2,3]: "123"} # => Raises a TypeError: unhashable type: 'list' valid_dict = {(1,2,3):[1,2,3]} # Values can be of any type, however.
Look up values with []
filled_dict["one"] # => 1
Get all keys as an iterable with "keys()". We need to wrap the call in list()
to turn it into a list. We'll talk about those later. Note - for Python
versions <3.7, dictionary key ordering is not guaranteed. Your results might
not match the example below exactly. However, as of Python 3.7, dictionary
items maintain the order at which they are inserted into the dictionary.
list(filled_dict.keys()) # => ["three", "two", "one"] in Python <3.7 list(filled_dict.keys()) # => ["one", "two", "three"] in Python 3.7+
Get all values as an iterable with "values()". Once again we need to wrap it
in list() to get it out of the iterable. Note - Same as above regarding key
ordering.
list(filled_dict.values()) # => [3, 2, 1] in Python <3.7 list(filled_dict.values()) # => [1, 2, 3] in Python 3.7+
Check for existence of keys in a dictionary with "in"
"one" in filled_dict # => True 1 in filled_dict # => False
Looking up a non-existing key is a KeyError
filled_dict["four"] # KeyError
Use "get()" method to avoid the KeyError
filled_dict.get("one") # => 1 filled_dict.get("four") # => None
The get method supports a default argument when the value is missing
filled_dict.get("one", 4) # => 1 filled_dict.get("four", 4) # => 4
"setdefault()" inserts into a dictionary only if the given key isn't present
filled_dict.setdefault("five", 5) # filled_dict["five"] is set to 5 filled_dict.setdefault("five", 6) # filled_dict["five"] is still 5
Adding to a dictionary
filled_dict.update({"four":4}) # => {"one": 1, "two": 2, "three": 3, "four": 4} filled_dict["four"] = 4 # another way to add to dict
Remove keys from a dictionary with del
del filled_dict["one"] # Removes the key "one" from filled dict
From Python 3.5 you can also use the additional unpacking options
{'a': 1, **{'b': 2}} # => {'a': 1, 'b': 2} {'a': 1, **{'a': 2}} # => {'a': 2}
Sets store ... well sets
empty_set = set()
Initialize a set with a bunch of values. Yeah, it looks a bit like a dict. Sorry.
some_set = {1, 1, 2, 2, 3, 4} # some_set is now {1, 2, 3, 4}
Similar to keys of a dictionary, elements of a set have to be immutable.
invalid_set = {[1], 1} # => Raises a TypeError: unhashable type: 'list' valid_set = {(1,), 1}
Add one more item to the set
filled_set = some_set filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5}
Sets do not have duplicate elements
filled_set.add(5) # it remains as before {1, 2, 3, 4, 5}
Do set intersection with &
other_set = {3, 4, 5, 6} filled_set & other_set # => {3, 4, 5}
Do set union with |
filled_set | other_set # => {1, 2, 3, 4, 5, 6}
Do set difference with -
{1, 2, 3, 4} - {2, 3, 5} # => {1, 4}
Do set symmetric difference with ^
{1, 2, 3, 4} ^ {2, 3, 5} # => {1, 4, 5}
Check if set on the left is a superset of set on the right
{1, 2} >= {1, 2, 3} # => False
Check if set on the left is a subset of set on the right
{1, 2} <= {1, 2, 3} # => True
Check for existence in a set with in
2 in filled_set # => True 10 in filled_set # => False
####################################################
3. Control Flow and Iterables
####################################################
Let's just make a variable
some_var = 5
Here is an if statement. Indentation is significant in Python!
Convention is to use four spaces, not tabs.
This prints "some_var is smaller than 10"
if some_var > 10: print("some_var is totally bigger than 10.") elif some_var < 10: # This elif clause is optional. print("some_var is smaller than 10.") else: # This is optional too. print("some_var is indeed 10.")
""" For loops iterate over lists prints: dog is a mammal cat is a mammal mouse is a mammal """ for animal in ["dog", "cat", "mouse"]: # You can use format() to interpolate formatted strings print("{} is a mammal".format(animal))
""" "range(number)" returns an iterable of numbers from zero to the given number prints: 0 1 2 3 """ for i in range(4): print(i)
""" "range(lower, upper)" returns an iterable of numbers from the lower number to the upper number prints: 4 5 6 7 """ for i in range(4, 8): print(i)
""" "range(lower, upper, step)" returns an iterable of numbers from the lower number to the upper number, while incrementing by step. If step is not indicated, the default value is 1. prints: 4 6 """ for i in range(4, 8, 2): print(i)
""" To loop over a list, and retrieve both the index and the value of each item in the list prints: 0 dog 1 cat 2 mouse """ list = ["dog", "cat", "mouse"] for i, value in enumerate(list): print(i, value)
""" While loops go until a condition is no longer met. prints: 0 1 2 3 """ x = 0 while x < 4: print(x) x += 1 # Shorthand for x = x + 1
Handle exceptions with a try/except block
try: # Use "raise" to raise an error raise IndexError("This is an index error") except IndexError as e: pass # Pass is just a no-op. Usually you would do recovery here. except (TypeError, NameError): pass # Multiple exceptions can be handled together, if required. else: # Optional clause to the try/except block. Must follow all except blocks print("All good!") # Runs only if the code in try raises no exceptions finally: # Execute under all circumstances print("We can clean up resources here")
Instead of try/finally to cleanup resources you can use a with statement
with open("myfile.txt") as f: for line in f: print(line)
Writing to a file
contents = {"aa": 12, "bb": 21} with open("myfile1.txt", "w+") as file: file.write(str(contents)) # writes a string to a file
with open("myfile2.txt", "w+") as file: file.write(json.dumps(contents)) # writes an object to a file
Reading from a file
with open('myfile1.txt', "r+") as file: contents = file.read() # reads a string from a file print(contents)
print: {"aa": 12, "bb": 21}
with open('myfile2.txt', "r+") as file: contents = json.load(file) # reads a json object from a file print(contents)
print: {"aa": 12, "bb": 21}
Python offers a fundamental abstraction called the Iterable.
An iterable is an object that can be treated as a sequence.
The object returned by the range function, is an iterable.
filled_dict = {"one": 1, "two": 2, "three": 3} our_iterable = filled_dict.keys() print(our_iterable) # => dict_keys(['one', 'two', 'three']). This is an object that implements our Iterable interface.
We can loop over it.
for i in our_iterable: print(i) # Prints one, two, three
However we cannot address elements by index.
our_iterable[1] # Raises a TypeError
An iterable is an object that knows how to create an iterator.
our_iterator = iter(our_iterable)
Our iterator is an object that can remember the state as we traverse through it.
We get the next object with "next()".
next(our_iterator) # => "one"
It maintains state as we iterate.
next(our_iterator) # => "two" next(our_iterator) # => "three"
After the iterator has returned all of its data, it raises a StopIteration exception
next(our_iterator) # Raises StopIteration
We can also loop over it, in fact, "for" does this implicitly!
our_iterator = iter(our_iterable) for i in our_iterator: print(i) # Prints one, two, three
You can grab all the elements of an iterable or iterator by calling list() on it.
list(our_iterable) # => Returns ["one", "two", "three"] list(our_iterator) # => Returns [] because state is saved
####################################################
4. Functions
####################################################
Use "def" to create new functions
def add(x, y): print("x is {} and y is {}".format(x, y)) return x + y # Return values with a return statement
Calling functions with parameters
add(5, 6) # => prints out "x is 5 and y is 6" and returns 11
Another way to call functions is with keyword arguments
add(y=6, x=5) # Keyword arguments can arrive in any order.
You can define functions that take a variable number of
positional arguments
def varargs(*args): return args
varargs(1, 2, 3) # => (1, 2, 3)
You can define functions that take a variable number of
keyword arguments, as well
def keyword_args(**kwargs): return kwargs
Let's call it to see what happens
keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"}
You can do both at once, if you like
def all_the_args(*args, **kwargs): print(args) print(kwargs) """ all_the_args(1, 2, a=3, b=4) prints: (1, 2) {"a": 3, "b": 4} """
When calling functions, you can do the opposite of args/kwargs!
Use * to expand tuples and use ** to expand kwargs.
args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} all_the_args(*args) # equivalent to all_the_args(1, 2, 3, 4) all_the_args(**kwargs) # equivalent to all_the_args(a=3, b=4) all_the_args(*args, **kwargs) # equivalent to all_the_args(1, 2, 3, 4, a=3, b=4)
Returning multiple values (with tuple assignments)
def swap(x, y): return y, x # Return multiple values as a tuple without the parenthesis. # (Note: parenthesis have been excluded but can be included)
x = 1 y = 2 x, y = swap(x, y) # => x = 2, y = 1
(x, y) = swap(x,y) # Again parenthesis have been excluded but can be included.
Function Scope
x = 5
def set_x(num): # Local var x not the same as global variable x x = num # => 43 print(x) # => 43
def set_global_x(num): global x print(x) # => 5 x = num # global var x is now set to 6 print(x) # => 6
set_x(43) set_global_x(6)
Python has first class functions
def create_adder(x): def adder(y): return x + y return adder
add_10 = create_adder(10) add_10(3) # => 13
There are also anonymous functions
(lambda x: x > 2)(3) # => True (lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5
There are built-in higher order functions
list(map(add_10, [1, 2, 3])) # => [11, 12, 13] list(map(max, [1, 2, 3], [4, 2, 1])) # => [4, 2, 3]
list(filter(lambda x: x > 5, [3, 4, 5, 6, 7])) # => [6, 7]
We can use list comprehensions for nice maps and filters
List comprehension stores the output as a list which can itself be a nested list
[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13] [x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7]
You can construct set and dict comprehensions as well.
{x for x in 'abcddeef' if x not in 'abc'} # => {'d', 'e', 'f'} {x: x**2 for x in range(5)} # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
####################################################
5. Modules
####################################################
You can import modules
import math print(math.sqrt(16)) # => 4.0
You can get specific functions from a module
from math import ceil, floor print(ceil(3.7)) # => 4.0 print(floor(3.7)) # => 3.0
You can import all functions from a module.
Warning: this is not recommended
from math import *
You can shorten module names
import math as m math.sqrt(16) == m.sqrt(16) # => True
Python modules are just ordinary Python files. You
can write your own, and import them. The name of the
module is the same as the name of the file.
You can find out which functions and attributes
are defined in a module.
import math dir(math)
If you have a Python script named math.py in the same
folder as your current script, the file math.py will
be loaded instead of the built-in Python module.
This happens because the local folder has priority
over Python's built-in libraries.
####################################################
6. Classes
####################################################
We use the "class" statement to create a class
class Human:
When a Python interpreter reads a source file it executes all its code.
This name check makes sure this code block is only executed when this
module is the main program.
if name == 'main': # Instantiate a class i = Human(name="Ian") i.say("hi") # "Ian: hi" j = Human("Joel") j.say("hello") # "Joel: hello" # i and j are instances of type Human, or in other words: they are Human objects
####################################################
6.1 Inheritance
####################################################
Inheritance allows new child classes to be defined that inherit methods and
variables from their parent class.
Using the Human class defined above as the base or parent class, we can
define a child class, Superhero, which inherits the class variables like
"species", "name", and "age", as well as methods, like "sing" and "grunt"
from the Human class, but can also have its own unique properties.
To take advantage of modularization by file you could place the classes above in their own files,
say, human.py
To import functions from other files use the following format
from "filename-without-extension" import "function-or-class"
from human import Human
Specify the parent class(es) as parameters to the class definition
class Superhero(Human):
if name == 'main': sup = Superhero(name="Tick")
####################################################
6.2 Multiple Inheritance
####################################################
Another class definition
bat.py
class Bat:
if name == 'main': b = Bat() print(b.say('hello')) print(b.fly)
And yet another class definition that inherits from Superhero and Bat
superhero.py
from superhero import Superhero from bat import Bat
Define Batman as a child that inherits from both Superhero and Bat
class Batman(Superhero, Bat):
if name == 'main': sup = Batman()
####################################################
7. Advanced
####################################################
Generators help you make lazy code.
def double_numbers(iterable): for i in iterable: yield i + i
Generators are memory-efficient because they only load the data needed to
process the next value in the iterable. This allows them to perform
operations on otherwise prohibitively large value ranges.
NOTE: range
replaces xrange
in Python 3.
range
replaces xrange
in Python 3.for i in double_numbers(range(1, 900000000)): # range
is a generator. print(i) if i >= 30: break
Just as you can create a list comprehension, you can create generator
comprehensions as well.
values = (-x for x in [1,2,3,4,5]) for x in values: print(x) # prints -1 -2 -3 -4 -5 to console/terminal
You can also cast a generator comprehension directly to a list.
values = (-x for x in [1,2,3,4,5]) gen_to_list = list(values) print(gen_to_list) # => [-1, -2, -3, -4, -5]
Decorators
In this example beg
wraps say
. If say_please is True then it
beg
wraps say
. If say_please is True then itwill change the returned message.
from functools import wraps
def beg(target_function): @wraps(target_function) def wrapper(*args, **kwargs): msg, say_please = target_function(*args, **kwargs) if say_please: return "{} {}".format(msg, "Please! I am poor :(") return msg
@beg def say(say_please=False): msg = "Can you buy me a beer?" return msg, say_please
print(say()) # Can you buy me a beer? print(say(say_please=True)) # Can you buy me a beer? Please! I am poor :(
Last updated