Show Menu
Cheatography

Python Tutorial Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Strings in python

String literals in python are surrounded by either single or double quotation marks.

my_name = "­Ang­us"

Arithmetic Operators

Arithmetic Operators are used with numeric values.

+ Addition
- Subtra­ction
* Multip­lic­ation
/ Division
% Modulus
** Expone­nti­ation
// Floor divison

Reserved words

These words are reserved and cannot be used as identi­fiers:

and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try

Line breaks

Line breaks should always occur BEFORE binary operators. This is to ensure it is readable to humans.

Identity operators

is Returns true if both variables are the same object
is not Returns true if both variables are not the same object.

Print

You can print by saying
print ("Hello World")

Lists in python

A list is a collection which is ordered and change­able.

my_list = ["ch­air­", "­tab­le", "­sof­a"]
print(­my_­list)

You can access specific items in a list by using its index numbers. (REMEMBER: Items in a list always start from 0!)

For example:
print(­my_­lis­t[1]) would return table
 

Membership operators

in Returns true if a sequence with the specified value is present in the object.
not in Returns true if a sequence with the specified value is not present in the object.

Comment rules

Comments should be kept up to date. If the code changes, the comment should be changed to ensure that it makes sense.

Comment rules

Comments should be kept up to date. If the code changes, the comment should be changed to ensure that it makes sense.

Comment rules

Comments should be kept up to date. If the code changes, the comment should be changed to ensure that it makes sense.

imports

Imports should always be put on the top of the file and should be placed on separate lines. For example:

import os
import sys

Maximum line length

Lines of good should be limited to 79 charac­ters. This is to ensure that the code can easily be read by humans.

Indend­ation

Code should be indented with 4 spaces. This is to ensure it is clean, readable and high quality.

Comparison Operators

Comparison operators are used to compare values.


> Greater than
< Less than
== Equal to
!= Not equal to
> = Greater than or equal to
<= Less than or equal to

Creating variables

A variable is created when a value is assigned to it:

x = 18
y = "­ang­us"
print (x)
print (y)
print (f"{y} is {x} years old")
 

Logical Operators

and Returns true if both statements are true
or Returns true if one of the statements is true
not Reverse the result, returns False if the result is true

Logical Operators

and Returns true if both statements are true
or Returns true if one of the statements is true
not Reverse the result, returns False if the result is true

Logical Operators

and Returns true if both statements are true
or Returns true if one of the statements is true
not Reverse the result, returns False if the result is true

String Quotes

With strings, if using single quotes stick to it, and if using double quotes, stick to it.

Numbers in Python

There are three numeric types in python. For example:
x = 1 This is an int
y = 3.5 This is a float
z = 1j This is a complex

You can see the type of data with the type function:
print(­typ­e(x))