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!")
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)))
Terminal input¶
In [5]:
my_result = input('please enter a word: ')
print("result: {}".format(my_result))
Mathematical operations¶
In [6]:
my_result = 5 + 2 # add
print("result: {}".format(my_result))
In [7]:
my_result = 5 - 2 # substract
print("result: {}".format(my_result))
In [8]:
my_result = 5 * 2 # multiply
print("result: {}".format(my_result))
In [9]:
my_result = 5 / 2 # divide
print("result: {}".format(my_result))
In [10]:
my_result = 5 % 2 # modulo
print("result: {}".format(my_result))
In [11]:
my_result = 5 ** 5 # exponent
print("result: {}".format(my_result))
Boolean operations¶
In [12]:
my_result = True
print("result: {}".format(my_result))
my_result = False
print("result: {}".format(my_result))
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))
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))
In [15]:
# not
my_result = not True
print("result: {}".format(my_result))
my_result = not False
print("result: {}".format(my_result))
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))
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!")
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)
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'])
In [20]:
# TUPLE
my_result = (5, 6)
print(my_result)
my_result = (5, "hello", 5.5, True, 2)
print(my_result)
Loops¶
In [21]:
# FOR
shopping_list = ["Carrots", "Onions", "Chicken", "Coconuts"]
for item in shopping_list:
print(item)
In [22]:
# FOR
for index in range(0,10):
print(index)
In [23]:
# WHILE
number = 0
while(number < 18):
print(number)
number += 1
print("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
In [25]:
def another_function(name):
print("Parameter passed in: {}".format(name))
another_function("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
Packages¶
In [27]:
import datetime
print(datetime.datetime.now())
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.
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