Show Menu
Cheatography

Python Module Companion Cheat Sheet (DRAFT) by

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

Data Types

Integer
-238, 39
Float
-253.23, 1.253e-10
String
"­­He­l­l­o", 'Goodbye', "­­Mu­l­t­il­­ine­­"­
Boolean
True, False
List
[ value, ... ]

Defining a variable

variab­le_name = value

Condit­ional Statement

if condition:
 ­ ­sta­tements
elif condition:
 ­ ­sta­tements
else:
 ­ ­sta­tements

Arithmetic Operators

x + y
add
x - y
substract
x * y
multiply
x / y
divide
x % y
modulus
x ** y
power

Comparison Operators

x < y
Less
x <= y
Less or eq
x > y
Greater
x >= y
Greater or eq
x == y
Equal
x != y
Different

Boolean Operators

not x
x or y
x and y

For loop

for i in range(start, end):
 ­ ­sta­tements

While loop

while condition:
 ­ ­sta­tements

Break and Continue

Break
Go out of the loop
Continue
Go back to the beginning of the loop

Function Definition

def name­(a­rg1, arg2, ...):
 ­ ­st­ate­ments
 ­ ­return expr

Fonction usage

name­(a­rg1, arg2, ...):

Useful functions

print(­"­som­eth­ing­")
variab­le_name = input(­"­som­eth­ing­")

Operation on strings

len("ex­amp­le")
7
"­exa­mpl­e1" + "­exa­mpl­e2"
"­exa­mpl­e1e­xam­ple­2"
str(36)
"­36"
"I love Python­".up­per()
"­ILO­VEP­YTH­ON"
"I love Python­".lo­wer()
"­ilo­vep­yth­on"

Lists Basics

Create an empty list
my_list = []
Create a list with values
my_list = [11, "­Pyt­hon­", 12.5]
Access an element
my_list[2]
Modify an element
my_list[2] = newvalue
Append an element
my_lis­t.a­ppe­nd(­ele­ment)

Iterate over a list

for element in my_list:
   statements

Create a Class

class class_name:
   statements

The init method

def __init__(self, params):
   statements

Import a class

from class_name import *

Objects

Creation
variab­le_name = class_­nam­e(p­arams)
Access of an attribute
variab­le_­nam­e.a­ttr­ibu­te_name
Use of a method
variab­le_­nam­e.m­eth­od_­nam­e(p­arams)