Python cheat sheet Cheat Sheet
by Milly via cheatography.com/25796/cs/6927/
Commands/ Functions
Example (cont)
Multiplication and Exponents (cont)
print()
displays information of the screen
print (int(1.5)) 1
number ** number
input()
receives info from the the user
print (int(2)) 2
float()
converts a value to decimal
int()
integer; coverts a value to an integer
str()
string; coverts a value to a string
print(float(1)) - 1.0 anything to a float
Exponent (Math)
Naming Convention
Rule for giving name
Math
- letter
==
equal to
- numbers
!=
no equal to
<
less than
>
more than
- my3
<=
less than or equal to
- Hello_name
>=
more than or equal to
Modulo, Find the remainder
Comment; no effect
- underscore_
Valid name
Vocabulary
variable
something that can change
string
a list of characters
Integer
Whole number/ counting
number
number
Float number
The number in decimal
- _myStr
Invalid name
- 3my="hi" -- cannot start with number
- first name="hi"
- first-name
Addition
Syntax
Grammar/ Structure of
language
Modulo
Find the remainder
Boolean
True/False
string + string
combines the strings
together
word = input("please enter a
string + number
crash
word.")
number +
math (addition)
"""
number
Example
Reverse word
letter_num = 0
reverse = ' '
Print (2) integer
Multiplication and Exponents
Print (2.5) float
Print (Hello) string
string*string
CRASH!
Print (mystr) variable
string*number
combines the string multiple
Print (str,Hi,2,1.0) commas
mystr = Hi
times
number*numb
mystr name
Multiply (Math)
reverse
letter_num = letter_num + 1
"""
CRASH!
for letter in word:
CRASH!
print ("Reverse: ",reverse)
reverse = letter + reverse
number
string ** string
reverse = word[letter_num] +
reverse = ' '
er
Hi value can change
string **
while letter_num < len(word) :
By Milly
Published 12th February, 2016.
Sponsored by Readability-Score.com
cheatography.com/milly/
Last updated 19th March, 2016.
Measure your website readability!
Page 1 of 5.
https://readability-score.com
Python cheat sheet Cheat Sheet
by Milly via cheatography.com/25796/cs/6927/
Countdown Machine
while True:
user_number = input("Please
enter a number")
List (cont)
num = num + 1
print (num)
shoppinglist = ['coke zero',
number = int(user_number)
'bacon', 'water', 'jelly', 'gummy
countdown_string = ""
bears']
while number > 0:
num = 0
countdown_string =
for w in shoppinglist:
countdown_string + str(number)
number = number - 1
print (countdown_string)
num = num + 1
print (num)
Calculator program (cont)
# calculate the sum of a and b
# return the answer
def product (a, b):
return a*b
# calcualate the product of a
and b
# return the answer
def diff (a, b):
return a-b
# calculate the difference
between a and b
List
Finding area of a circle
# return the answer
def div (a, b):
shoppinglist = ['coke zero',
'bacon', 'water', 'jelly', 'gummy
bears']
while True:
user_radius = input("Please
enter the radius of the circle:")
print (shoppinglist)
radius = float(user_radius)
print (shoppinglist[0])
pi = 3.1415
list_num =0
area = (pi radius*2)
while list_num <
print ("The area of the circle
len(shoppinglist):
is", area)
list_num = list_num + 1
return a/b
else:
return ("Error")
# calculate the division of a
and b
# return the answer
print (calc (10, 0, "div"))
print ("List:",
shoppinglist[list_num])
if b!=0:
Calculator program
print (calc (1, 2,"sum")) # output
should be 3
def calc (num1, num2, operation):
print (calc (4, 2, "diff")) #
for item in shoppinglist:
print (item)
if operation == "sum":
return sum (num1,num2)
numbers = range (1,6)
elif operation == "product":
for num in numbers:
return product (num1,num2)
print (num)
elif operation == "diff":
# a string is a list of
return diff (num1,num2)
characters, letters, numbers etc.
elif operation == "div":
mystr = "hello"
return div (num1,num2)
for letter in mystr:
# use if/elif/else to check
print (letter)
shoppinglist = ['coke zero',
output should be 2
print (calc (9, 3, "div" )) #
output should be 3
print (calc (2 ,12, "product" )) #
output should be 24
Finding the area of the triangle and its
prism
#write a function
what operation to do
# call the correct function and
#name: areaofTriangle
#parameters: base height
'bacon', 'water', 'jelly', 'gummy
return the answer
#return: area
bears']
def sum (a, b):
num = 0
return a+b
user_base = float(input ('Enter the
base of the triangle: '))
for w in shoppinglist:
By Milly
Published 12th February, 2016.
Sponsored by Readability-Score.com
cheatography.com/milly/
Last updated 19th March, 2016.
Measure your website readability!
Page 2 of 5.
https://readability-score.com
Python cheat sheet Cheat Sheet
by Milly via cheatography.com/25796/cs/6927/
Finding the area of the triangle and its prism
(cont)
Maximum (cont)
return maxvalue
1. Multiplying number
#Receive input from the user as a
user_height = float(input('Enter
print (max2(3,4))
float, and print out half of that
the height of the triangle:'))
# write a function that returns the
number. e.g. user enters 12.5,
def areaOfTriangle (base, height):
largest of three values
print out 6.25
return 0.5 base height #or 1/2
# name: max3
user_input = input("Please enter a
#functioncall
# arguments: num1, num2, num3
number:")
print ('The area of the triangle
# return: the largest value
number = float(user_input)
is', areaOfTriangle(user_base,
def max3 (num1,num2,num3):
finalnumber = 0.5*number
user_height))
maxvalue = num1
#write function compute volume of
prism
if num2 > maxvalue:
#name: volumeOfPrism
maxvalue = num2
#parameters: base, height,
prism_height
if num3 > maxvalue:
#return volume
maxvalue = num3
def volumeOfPrism
(base,height,prism_height):
return maxvalue
# area * prism_height
print (max3(3,4,8))
volume = areaOfTriangle
# write a function that returns the
(base,height) * prism_height
return areaOfTriangle
2. Output
#What is the output of the
following code:
y = True
print (not y or 2<3)
#output is True
3. Error
message = "hello"
largest number in a list
if (len(message) >5)
# name: maxlist
print ("Message too long")
(base,height) * prism_height
# argument: list
user_prism_height =
# returns the largest value in the
float(input('Enter the height of
list
the prism:'))
print(finalnumber)
else:
print ("Message is good")
line 3 has an error because it has
def maxlist (list):
no indent
print ('The area of the prism is',
maxvalue = list[0]
volumeOfPrism
for item in list:
(user_base,user_height,user_prism_h
if item > maxvalue:
4. Divisible by 3
eight))
maxvalue = item
#create a program to receive a
number from the user and determine
return maxvalue
if that number
Maximum
# write a function that returns the
list = [1,2,3,6,19,50,2,4,5]
#is divisible by 3
largest of two values
print (maxlist(list))
user_input = input("Please enter a
# name: max2
number:")
# arguments: num1, num2
number = int(user_input)
if number %3 == 0:
# return: the largest value
print(number,"is divisible by
def max2 (num1,num2):
maxvalue = num1
3")
else:
print (number, "is not
if num2 > maxvalue:
maxvalue = num2
divisible by 3")
By Milly
Published 12th February, 2016.
Sponsored by Readability-Score.com
cheatography.com/milly/
Last updated 19th March, 2016.
Measure your website readability!
Page 3 of 5.
https://readability-score.com
Python cheat sheet Cheat Sheet
by Milly via cheatography.com/25796/cs/6927/
5. Even Number
7. List: while loop (method 2)
2) Output
# print all the even numbers from 1
#while loop solution
#What is the output of the
to 100 using a while loop
mylist = [1,2,3,4,5]
following code?
num = 2
num = 0
x = False
while num <= 100:
while num < len(mylist):
print (x and True or 1 == 1)
print (num)
print (mylist[num])
#False and True = False
num = num+2
num = num+1
# False or True = True
# output is True
6. Output 2
9. Multiplication Table
#What is the output of the
#Write a function called
following code?
multiplicationTable that asks the
#condisder the following code
condition = True
user for a number and
def doubleValue(value):
number = 5
#computes its multiplication table.
return value*2
if condition == False: #False
def multiplicationTable ():
print (doubleValue(4))
3) Error 2
number = number ** 2
elif number <5: #False
number = number * 2
elif condition == True: #True
number = number%2 #5%2=1
user_input = input ("Enter a
number:")
number = number/2
print (number) # output = 1
indented
num = int(user_input)
count=1
while count <= 10:
print
else:
#line 2 has error because it is not
(num,"",count,"=",numcount)
count = count+1
4) Types of number
#write a program that receives a
number from the user and determines
if that
#number is negative,zero or
#function call
positive.
7. my list (method 1)
multiplicationTable()
user_input = int(input("Please
#Given a list called mylist, print
enter a number:"))
1) Multiply 5
all elements from the list using a
if user_input > 0:
print (user_input,"is
loop
#Write a program that receives
mylist = ["Milly","Prim","Pizza"]
input from the user, converts it to
positive")
for item in mylist:
an integer,
elif user_input == 0:
print (item)
#and print the product of the
integer and 5
user_input = input("Please enter a
number.")
print (user_input,"is zero")
else:
print (user_input,"is
negative")
user_input = int(user_input)
product = user_input * 5
5) Even numbers while loop
print (product)
# write a program that prints all
the even numbers from -100 to -1
using a
#while loop
num = -100
while num <= -2:
print (num)
By Milly
Published 12th February, 2016.
Sponsored by Readability-Score.com
cheatography.com/milly/
Last updated 19th March, 2016.
Measure your website readability!
Page 4 of 5.
https://readability-score.com
Python cheat sheet Cheat Sheet
by Milly via cheatography.com/25796/cs/6927/
5) Even numbers while loop (cont)
num = num + 2
9) Area of Ellipse
# Write a function called
11) Even and Odd (cont)
oddcount = oddcount + 1
areaOfEllipse () that computes the
6) Output 2
area of an ellipse
#using the equation pir1r2
#What is the output of the
# The function should be given 2
following code:
parameters (radius1 and radius 2)
condition = 2<3 #True
and should
number = 3
#return the area
if condition != True: #True != True
def areaOfEllipse
;False
number = number ** 2
elif number <= 0: #3 <= 0 ;False
number = number * 3
elif number > 3: # 3>3 ; False
number = number%10
(radius1,radius2):
pi = 3.1415
area = pi radius1radius2
return area
#function call
area1 = areaOfEllipse(2,3)
else: #everything is False do must
print(area1)
do else
number = number - 1 * 2 #
11) Even and Odd
number - 2; 3-2=1; 1
print (number) # 1
# Write a program that repeatly
receives positive integers from the
8) 0......
user. When
# the user enters a negative
#complete the program below by
integer, exit the loop and print
filling in the blank
how many of the
# Expected output of program
# numbers entered were even and
# 0
odd.
# 01
evencount = 0
# 012
oddcount = 0
# 0123
while True:
# 01234
mystring = ""
user_input = int(input("Enter
a number:"))
count = 0
while count <= 4:
mystring = mystring
if user_input < 0:
print ("Evencount=",
evencount)
+str(count)
print (mystring)
count = count + 1
print
("Oddcount=",oddcount)
break
elif user_input > 0:
if user_input % 2 == 0:
evencount = evencount
+ 1
else:
By Milly
Published 12th February, 2016.
Sponsored by Readability-Score.com
cheatography.com/milly/
Last updated 19th March, 2016.
Measure your website readability!
Page 5 of 5.
https://readability-score.com