Show Menu
Cheatography

Python 3.x Quick Reference (DRAFT) by

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

Data Type Summary

Numeric (Integer)
Store whole number values
Numeric (Float)
Store decimal number values
Numeric (Complex)
Stores complex number values
Boolean
Stores true (or false) values (in an integer)
String
Stores an ordered sequence of alphan­umeric character values
Tuple
Stores an ordered sequence of immutable data values. ()
Lists
Stores an ordered sequence of mutable data values. []
Dictio­naries
Stores key value pairs (mappings) {}
Set
Stores an unordered collection of unique and immutable objects

Slices

[x:y]
Slices from index x up to index y (up to!)
[x:]
Slices from index x to last index in the set
[:y]
Slices from the first index up to index y
[:]
Slices the entire set
[-1]
Slices the last item from the set
[:-x]
Slices everything except the last x items from the set. (gener­alized)
[x:y:z]
Slices from index x up to index y (up to!), by step z

Arithmetic Operations

+
Addition (or string concat­ena­tion)
-
Subtra­ction or Negation
*
Multip­lic­ation (or string repeti­tion)
/
Division
%
Modulus
**
Expone­ntial
//
Floor Division (integer)

Logical Operators

and
Logical And
or
Logical Or
not
Logical Not

Simple Function

 
def MaxFun­c(x,y):
 ­ ­ ­  if x>y:
 ­ ­ ­ ­ ­ ­ ­  return x
 ­ ­ ­  else:
 ­ ­ ­ ­ ­ ­ ­  return y
 

Bitwise Operators

<<
Shift left
>>
Shift Right
&
Binary AND
|
Binary OR
~
Binary NOT
^
Binary XOR
Don't confuse binary not with logical not!

Membership Operators

x in y
True if the value x can be found in y.
x not in y
True if the value x can not be found in y.

Identity Operators

x is y
True if x and y are variables that reference the same data undern­eath!
x is not y
Only true if x and y are indepe­ndent. They may have the same value!
Identity operators verify that the variables are located on the same part of the memory.

Assignment Operators

x = y
Simple assignment x=y
x += y
x=x+y
x -= y
x=x-y
x *= y
x=x*y
x /= y
x=x/y
x %= y
x=x%y
x //= y
x=x//y
x **=y
x=x**y
x &=y
x=x & y
x|=y
x=x | y
x^=y
x=x^y
x>>=y
x=x >> y
x<<=y
x=x << y
 

Sample If Statement

if crew_age < 10:
 ­ ­ rank = junior
elif age < 18:
 ­ ­ rank = ensign
else:
 ­ ­ rank = commander

List Operations

Creating an list
crew = ['Spoc­k',­'Mc­Coy']
Getting first item from the list
first = crew[0]
Get the last item from the list
last = crew[-1]
Looping through the list
for person in crew:
    print(person)
Adding items to a list
crew.a­ppe­nd(­'Kirk')
Slicing a list
human=­cre­w[1:2]
Pointer to a list (same data)
crew2=crew
Duplicate of the list (copy of data)
duplic­ate­=cr­ew[:]

Dictio­naries

Creating a dictionary
exampl­e_d­ict­ionary = {1:"­Ora­nge­"­,2:­"­App­le",­3:"B­ana­na",­4:"P­eac­h",5­:"Pe­ar"}
Accessing a value
print (examp­le_­dic­tio­nar­y[2])
Adding a new key-value pair
exampl­e_d­ict­ion­ary­[15­]="P­lum­"
Looping through all key-value pairs
for key in example_dictionary:
   print(key,example_dictionary[key])
Looping through all keys
for key in example_dictionary.keys():
   print("Key:",key)
Looping through all values
for value in example_dictionary.values():
   print("Value:",value)

Simple while loop

i=0
while i<10:
 ­ ­ ­ ­i=i+1
 ­ ­ ­ ­pri­nt(­"loop - iteration #",i)
print(­"­Don­e")

Loop control

break
immediate exit of loop
continue
resumes at test condition of the loop