Cheatsheet
Cheatsheet
Commands/ Functions
Example (cont)
print()
print (int(1.5)) 1
number ** number
input()
print (int(2)) 2
float()
int()
str()
Exponent (Math)
Naming Convention
Rule for giving name
Math
- letter
==
equal to
- numbers
!=
no equal to
<
less than
>
more than
- my3
<=
- Hello_name
>=
Comment; no effect
- underscore_
Valid name
Vocabulary
variable
string
a list of characters
Integer
number
number
Float number
- _myStr
Invalid name
- 3my="hi" -- cannot start with number
- first name="hi"
- first-name
Addition
Syntax
Grammar/ Structure of
language
Modulo
Boolean
True/False
string + string
string + number
crash
word.")
number +
math (addition)
"""
number
Example
Reverse word
letter_num = 0
reverse = ' '
string*string
CRASH!
string*number
times
number*numb
mystr name
Multiply (Math)
reverse
letter_num = letter_num + 1
"""
CRASH!
CRASH!
number
string ** string
reverse = word[letter_num] +
er
By Milly
Sponsored by Readability-Score.com
cheatography.com/milly/
Page 1 of 5.
https://readability-score.com
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)
countdown_string = ""
bears']
num = 0
countdown_string =
for w in shoppinglist:
countdown_string + str(number)
number = number - 1
print (countdown_string)
num = num + 1
print (num)
List
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
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
if operation == "sum":
return sum (num1,num2)
print (num)
# a string is a list of
mystr = "hello"
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
what operation to do
# call the correct function and
#name: areaofTriangle
#parameters: base height
#return: area
bears']
def sum (a, b):
num = 0
return a+b
for w in shoppinglist:
By Milly
Sponsored by Readability-Score.com
cheatography.com/milly/
Page 2 of 5.
https://readability-score.com
Maximum (cont)
return maxvalue
1. Multiplying number
#Receive input from the user as a
user_height = float(input('Enter
print (max2(3,4))
# name: max3
#functioncall
number:")
number = float(user_input)
is', areaOfTriangle(user_base,
finalnumber = 0.5*number
user_height))
maxvalue = num1
prism
#name: volumeOfPrism
maxvalue = num2
prism_height
#return volume
maxvalue = num3
def volumeOfPrism
(base,height,prism_height):
return maxvalue
# area * prism_height
print (max3(3,4,8))
volume = areaOfTriangle
(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"
(base,height) * prism_height
# argument: list
user_prism_height =
list
the prism:'))
print(finalnumber)
else:
print ("Message is good")
line 3 has an error because it has
def maxlist (list):
no indent
maxvalue = list[0]
volumeOfPrism
(user_base,user_height,user_prism_h
4. Divisible by 3
eight))
maxvalue = item
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
print (maxlist(list))
# name: max2
number:")
number = int(user_input)
if number %3 == 0:
print(number,"is divisible by
3")
else:
print (number, "is not
divisible by 3")
By Milly
Sponsored by Readability-Score.com
cheatography.com/milly/
Page 3 of 5.
https://readability-score.com
5. Even Number
2) Output
mylist = [1,2,3,4,5]
following code?
num = 2
num = 0
x = False
print (num)
print (mylist[num])
num = num+2
num = num+1
6. Output 2
9. Multiplication Table
following code?
condition = True
def doubleValue(value):
number = 5
return value*2
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
number = number/2
print (number) # output = 1
indented
num = int(user_input)
count=1
while count <= 10:
print
else:
(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
enter a number:"))
1) Multiply 5
if user_input > 0:
print (user_input,"is
loop
mylist = ["Milly","Prim","Pizza"]
positive")
an integer,
elif user_input == 0:
print (item)
user_input = int(user_input)
product = user_input * 5
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
Sponsored by Readability-Score.com
cheatography.com/milly/
Page 4 of 5.
https://readability-score.com
9) Area of Ellipse
# Write a function called
area of an ellipse
#using the equation pir1r2
(radius1,radius2):
pi = 3.1415
area = pi radius1radius2
return area
#function call
area1 = areaOfEllipse(2,3)
8) 0......
user. When
# the user enters a negative
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
Sponsored by Readability-Score.com
cheatography.com/milly/
Page 5 of 5.
https://readability-score.com