Skip to content

Latest commit

 

History

History
233 lines (185 loc) · 4.97 KB

cheat_sheet.md

File metadata and controls

233 lines (185 loc) · 4.97 KB

Cheat Sheet

Here are some commands for your reference that you will encounter throughout the sessions.

Session 0: Getting Started

Assigning variables

Remember starting variable names with numbers is not allowed

2var = 0 # invalid variable name
var2 = 0 # valid variable name
var_name = 0           # integer
var_name = 'a string'  # string
var_name = 2.3         # float

Converting Types

Remember to switch between types to perform operations

str(3) # '3'
int('2') # 2
float('3.2') # 3.2

Order of Operation

(3+2)*5 # 25
# remember BOMDAS?
# Brackets Over Multiplication, Division, Addition and Subtraction

Rounding Numbers

round(3.4564,2) # will round the number to 2 decimal places

Session 1: Basics Part A

Bools and Comparison operators

# comparison operators
== # equal to
!= # not equal to
< # less than
> # greater than
<= # less than or equal to
>= # greater than or equal to
# expressions that evaluate to a boolean value
42 <= 42 # True
'42' == 42 # False
my_age = 27 # assign 27 to my_age
my_age == 27 # True
# boolean operators
True and False # False
True or False # True
myAge > 25 or myAge < 20 # True
myAge > 25 and myAge < 20 # False
not True # False
not False # True

For loops

for i in range(10): # loops over the set -> 0,1,2,...,9
	print(i)

While loops

i = 0 
while i < 10:
	print(i)
	i = i+1  # alternatively short hand is i += 1

If Statements

# if statement
condition = True # condition is an expression that evaluates to True or False
if (condition):
	# execute the body of code. Indentation is important!

# if else statement
if (condition):
	# execute this body of code
else:
	# execute this body of code

# if elif else statement
if (condition1):
	# execute this body of code
elif (condition2):
	# execute this body of code
elif (condition3):
	# execute this body of code
else:
	# execute this body of code

# you can have as many elif's as you like

Session 2: Basics Part B

Defining a function

def add(a, b):
	return a+b

Calling a function

c = add(4, 5)
# c = 9

Session 3: Data Structures

Lists

# python list - a variable to store stuff
names = ["Jess","Tom","Laura","Will"]

# list splicing
names[0] # "Jess"
names[1] # "Tom"
names[-2] # "Laura"
# names[start:stop:step] grabs multiple elements from list (includes start, excludes stop)
names[1:3:1] # ["Tom", "Laura"]

# how long is the list
len(names) # 4
# adding to list
names.append("Jason") # add "Jason" to end
names.insert(2, "John") # insert "John" at index 2
names.extend(["Alex","Faye","Mia"]) # append "Alex", "Faye" and "Mia" to the list
# remove from the list
names.pop() # removes "Mia" and returns
names.remove("Tom") # removes first "Tom" found

# some useful things you can do with lists
"Sam" in names # False
"Sam" not in names # True

# loop through each element in an array
for name in names:
	print(name)

Dictionaries

person = {"name":"Alex", "age":27, "height":158, 42:"The ultimate question"}

person["name"] # "Alex"
person["age"] # 27

len(person) # 4
person["graduate"] = True # adds another key-value pair to the dictionary
person.pop(42) # returns "The ultimate Question" and deletes the key-value pair from the dictionary

# looping through dictionary
person.values() # ["Alex", 27, 158, True]
person.keys() # ["name", "age", "height", "graduate"]
person.items() # [("name", "Alex"), ("age", 27), ("height", 158), ("graduate", True)]

for k, v in person.items():
	print("Key: "+ str(k)+ "\tValue: "+ str(v)

Session 4: Reading/Writing Files

Navigation of directories in python can be done using the following commands

import os

# working director
os.getcwd() # will return a path to your current working directory

# check if file exists
os.path.exists(... your path)

#Joining Paths
path = '.\\Desktop\\'
file = 'example.txt'
full_path = os.path.join(path, file)

#changing directory
os.chdir()

Splitting a string

split_string = 'hello i am a string'.split() # returns a list split by whitespace
split_string # ['hello', 'i', 'am', 'a', 'string']

csv_string = '1,2,456,789,1566'.split(',') # returns a list split by ,
csv_string # ['1','2','456','789','1566'] note that these are still strings not ints

# you can use map and list to convert the list
csv_ints = list(map(int,'1,2,3,4,5'.split(','))) # [1,2,456,789,1566]

Opening a file

f = open('file.txt', 'r')    # read mode
f = open('file.txt', 'w')    # write mode
f = open('file.txt', 'a')    # append mode

Reading a file

f = open('file.txt', 'r')    # read mode
lines  = f.readlines()       # returns a list of strings representing your lines in your file
for line in lines:           # iterate over each line in your list of lines
  print(line)                # print out each line to the shell
f.close()

Writing a file

f = open('file.txt', 'w')    # read mode
f.write("To write or not to write\nthat is the question!\n")
f.close()