Get the Books

Enjoying these notebooks and want to support the work? Check out the practical books on Data Science, Visualisation, and Evolutionary Algorithms.

Get the books

Python Cheatsheet

Who is this for?

This crash-course makes the assumption that you already have some programming experience, but perhaps none with Python.

Comments

In [1]:
# You can write a comment using an octothorpe or 'hash' symbol

Printing to terminal

In [2]:
print("Hello World!")
Hello World!

Variables and assignment

In [3]:
my_number = 5
my_float = 0.2
my_string = "Hello"
also_my_string = 'Hello'
In [4]:
print("{} is of {}".format(my_number, type(my_number)))
print("{} is of {}".format(my_float, type(my_float)))
print("{} is of {}".format(my_string, type(my_string)))
print("{} is of {}".format(also_my_string, type(also_my_string)))
5 is of <class 'int'>
0.2 is of <class 'float'>
Hello is of <class 'str'>
Hello is of <class 'str'>

Terminal input

In [5]:
my_result = input('please enter a word: ')
print("result: {}".format(my_result))
please enter a word: Hello
result: Hello

Mathematical operations

In [6]:
my_result = 5 + 2 # add
print("result: {}".format(my_result))
result: 7
In [7]:
my_result = 5 - 2 # substract
print("result: {}".format(my_result))
result: 3
In [8]:
my_result = 5 * 2 # multiply
print("result: {}".format(my_result))
result: 10
In [9]:
my_result = 5 / 2 # divide
print("result: {}".format(my_result))
result: 2.5
In [10]:
my_result = 5 % 2 # modulo
print("result: {}".format(my_result))
result: 1
In [11]:
my_result = 5 ** 5 # exponent
print("result: {}".format(my_result))
result: 3125

Boolean operations

In [12]:
my_result = True
print("result: {}".format(my_result))

my_result = False
print("result: {}".format(my_result))
result: True
result: False
In [13]:
# and
my_result = False and False
print("result: {}".format(my_result))

my_result = False and True
print("result: {}".format(my_result))

my_result = True and False
print("result: {}".format(my_result))

my_result = True and True
print("result: {}".format(my_result))
result: False
result: False
result: False
result: True
In [14]:
# or
my_result = False or False
print("result: {}".format(my_result))

my_result = False or True
print("result: {}".format(my_result))

my_result = True or False
print("result: {}".format(my_result))

my_result = True or True
print("result: {}".format(my_result))
result: False
result: True
result: True
result: True
In [15]:
# not
my_result = not True
print("result: {}".format(my_result))

my_result = not False
print("result: {}".format(my_result))
result: False
result: True

Relational operations

In [16]:
my_result = 5 > 2 # greater than
print("result: {}".format(my_result))

my_result = 5 >= 2 # greater than or equal to
print("result: {}".format(my_result))

my_result = 5 < 2 # less than
print("result: {}".format(my_result))

my_result = 5 <= 2 # less than or equal to
print("result: {}".format(my_result))

my_result = 5 == 2 # equal to
print("result: {}".format(my_result))
result: True
result: True
result: False
result: False
result: False

Conditional statements

In [17]:
age = 17
uk_drinking_age = 18

if(age >= uk_drinking_age): # IF
    print("you can drinking!")
elif(age == 17):            # ELSE IF
    print("you can drink on your birthday!")
else:                       # ELSE
    print("you can't drink!")
you can drink on your birthday!

Data structures

In [18]:
# LIST
shopping_list = ["Carrots", "Onions", "Chicken", "Coconuts"]
print(shopping_list)
print(shopping_list [0]) # Python is zero-indexed

shopping_list.append("Ice cream")
print(shopping_list)
['Carrots', 'Onions', 'Chicken', 'Coconuts']
Carrots
['Carrots', 'Onions', 'Chicken', 'Coconuts', 'Ice cream']
In [19]:
# DICTIONARY
shopping_list = {"Carrots": 5, "Onions": 2, "Chicken": 1, "Coconuts": 1}
print(shopping_list)
print("Careful - dictionaries are not ordered in older versions of Python.")
print(shopping_list['Carrots'])
{'Carrots': 5, 'Onions': 2, 'Chicken': 1, 'Coconuts': 1}
Careful - dictionaries are not ordered in older versions of Python.
5
In [20]:
# TUPLE
my_result = (5, 6)
print(my_result)

my_result = (5, "hello", 5.5, True, 2)
print(my_result)
(5, 6)
(5, 'hello', 5.5, True, 2)

Loops

In [21]:
# FOR

shopping_list = ["Carrots", "Onions", "Chicken", "Coconuts"]

for item in shopping_list:
    print(item)
Carrots
Onions
Chicken
Coconuts
In [22]:
# FOR

for index in range(0,10):
    print(index)
0
1
2
3
4
5
6
7
8
9
In [23]:
# WHILE
number = 0

while(number < 18):
    print(number)
    number += 1
    
print("number was no longer below 18!")
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
number was no longer below 18!

Functions

In [24]:
def my_function(): # create a function
    print("No parameters passed in")
    
my_function()      # call a function
No parameters passed in
In [25]:
def another_function(name):
    print("Parameter passed in: {}".format(name))
    
another_function("Derek")
Parameter passed in: Derek
In [26]:
def greater_than(left_operand, right_operand): 
    my_result = left_operand > right_operand
    return my_result      # returning a value from the function

print(greater_than(5, 2)) # printing the value returned from the function
True

Packages

In [27]:
import datetime

print(datetime.datetime.now())
2019-07-12 19:37:47.117505

Support this work

You can support this work by getting the e-books. This notebook will always be available for free in its online format.