Python Cheat Sheet
Outputs and strings
print("Hello everyone") #outputting text
fname = "Bobby" #defining and assigning string
sname = "Jones"
print(fname + " " + sname) #joining strings
Inputting strings
name = input("Enter name: ") #string input from console
print("Hello " + name) #joining Hello with input
Integers (whole numbers)
num1 = 35 #defining and assigning integer
result = num1 * 2 #int variable times 2
num2 = int(input("Enter a number: ")) #int input from user
result = num1 - num2 #using subtraction
print("The result: " + str(result)) #converting to string
Floats (decimal places)
floaty = 2.5 #float variable assignment
num = float(input("Enter a number: ") #converting to float
answer = num / 2 #division will give a float answer
print(answer)
IF statements
num1 = 30
if num1 > 30: #if num1 bigger than 30
print("Num 1 than 30") #notice the indent (tab key)
else: #notice no indent
print("Num 1 less than 30")
ans = input("Capital of England: ")
if ans == "London": #if answer is london
print("Correct!")
else:
print("Sorry, wrong")
age = int(input("Enter age: "))
if age >= 17: #if 17 or greater
print("Full licence")
elif age == 16: #else if 16
print("Provisional licence")
else: #otherwise
print("Not old enough")
While loops
x = 0
while x < 10:
print(x)
x += 1 #increase x by 1
#below will keep looping until the user inputs 'exit'
response = "" #empty string
while response != "exit": #loop while response NOT exit
response = input("Continue? ") #ask user for input
For loops
for i in range(10): #looping from 0 to 9 (stopping at 10)
print(i) #will output 0 first loop, then 1...
#code below will output times table of input number
num = int(input("Enter a number: "))
for i in range(1,13): #i will start at 1, 12 on last loop
tot = i * num
print(str(num)+" times "+str(i)+" = "+str(tot))
Lists (arrays)
cars = ["Honda","Ford","Audi"] #list of strings
card.append("BMW")#add element to list
cars.pop(1) #remove at index 1 (Ford)
cars.pop() #remove last element (BMW)
bikes = [] #empty list
names = [""] * 100 #list/array with 100 blank string elements
numbers = [0] * 50 #list/array with 50 integer elements
bike.append(input("Enter a bike: ")) #user inputs new item
Looping through lists
#the code below will print all elements in the list
cars = ["Honda","Ford","Audi","BMW"] #list of strings
for x in cars: #x will be each item in turn
print(x) #print current value of x
#the code below uses an index to loop through the list
for i in range(0,len(cars)): #i 0 to end of the list
if cars[i] == "Ford": #if current item is Ford
print("Found at position "+i) #output index of item
Random
import random #needed to use random
rand = random.randint(0,10) #between 0 and 10
print(random.choice(cars)) #random item from a list
String manipulation
text = "Super coder"
chars = len(text) #length of string
print(chars) #output: 11
print(text[4]) #output: r
print(text[6:]) #output: coder
print(text[-5:-1]) #output: code
print(text[:-6]) #output: Super
print(text.count('e') #output: 2
if text.find("code") == -1: #find if contains "code"
print("Not found") #if -1, not found
else:
print("Found")
print(text.upper()) #output: SUPER CODER
Sleep (time library)
import time #needed to use time functions
time.sleep(2) #sleep for 2 seconds
#can be used in a loop to do a countdown 1 to 10
for x in range(1,11):
time.sleep(1)
print(x)
DIV and MOD
#int division ignores remainders e.g. below
answer = 9//2 #int division = 4 (4 twos in 9)
#MOD gives the remainder - amount left after in division
remain = 9 % 5 #remain = 4 (1 five in 9, 4 remainder)
Procedures (no return value)
def power2(num): #defining sub with 1 parameter
answer = num * num
print(answer)
power2(9) #calling the sub with argument (9)
Functions (returns a value)
#this function a number when raised to given power
def power(num,exp): #parameter for number and exponent
answer = num #variable to hold running result
#x = exp and decrease by 1 (-1) each loop until reaches 1
for x in range(exp,1,-1): #decrementing loop
answer = answer * num
return answer #return value to main code
print(power(5,3)) #call function and print result
result = power(6,4) #call and store result in variable