You are on page 1of 2

python Cheat Sheet

by mmildmilds via cheatography.com/25734/cs/6848/

Python For Loop and List (cont) List (cont)

string + string combine together print ("Li​st:​"​,sh​opp​ing​lis​t[l​ist​_num]) random_str = random.choice (strlist)

string + number crash list_num = list_num + 1 print (strli​st,​ran​dom​_str)


for item in shoppi​nglist: mylist =
number + number math-a​ddition
print (item) ["ad​am",​"​mil​d","l​ove​ada​m","l​evi​ne",​"​3","4.6",​424​,
number​-number math-s​ubt​raction numbers = range(120) 67​4,5.733]

number​*number math-m​ult​ipl​ication for num in numbers: random​_item = random.choice (mylist)


print (num) print (mylis​t,r​and​om_​item)
number​/number math-d​ivision
myvar1 = 1
number​**n​umber math-e​xponent myvar2 = 2
Vocabulary
number​%number finding a remainder myvar3 = 3
str string
varlist = (myvar​1,m​yva​r2,​myvar3)
bolean True/False
int integer random_var = random.choice (varlist)
# single​-line comment print (varli​st,​ran​dom​_var)
float decimal number
"​"​"​" multi-line comment
syntax the structure of a program
List
Countdown syntax error An error in a program that
makes it impossible to parse import random
while True: intlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
len Return the length of an object
user_n​umber = input(​" Enter ur number random_int = random.choice (intlist)
Here...") Modulus(%) Show the remainder of the print(​int​lis​t,r​and​om_int)
number = int(us​er_​number) division fplist = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
countd​own​_string = "​" Single assigns the value on the right to random_fp = random.choice (fplist)
while number > 0: Equal (=) a variable on the left print (fplis​t,r​and​om_fp)
countd​own​_string = countd​own​_string + strlist = ["1",​"​2","3​"​,"4",​"​5","6​"​,"7",​"​8","9​"]
Double test if the 2 value are the same
str(nu​mber) random_str = random.choice (strlist)
Equal (==)
number = number - 1 print (strli​st,​ran​dom​_str)
String a series of sentences that the mylist =
print (count​dow​n_s​tring)
user will use. Usually ["ad​am",​"​mil​d","l​ove​ada​m","l​evi​ne",​"​3","4.6",​424​,
surrounded by double quotes 67​4,5.733]
Letter
Variable A thing that can be changed random​_item = random.choice (mylist)
name = "tim GIRARD​" print (mylis​t,r​and​om_​item)
input convert things you enter
print (name. upper()) myvar1 = 1
print (name. lower()) myvar2 = 2
print (name. capita​lize()) List
myvar3 = 3
print (name. title()) varlist = (myvar​1,m​yva​r2,​myvar3)
import random
TIM GIRARD random_var = random.choice (varlist)
intlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
tim girard print (varli​st,​ran​dom​_var)
random_int = random.choice (intlist)
Tim girard
print(​int​lis​t,r​and​om_int)
Tim Girard
fplist = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
random_fp = random.choice (fplist)
For Loop and List print (fplis​t,r​and​om_fp)
strlist = ["1",​"​2","3​"​,"4",​"​5","6​"​,"7",​"​8","9​"]
shoppi​nglist = ['salmon', 'bacon', 'water', 'jelly',
'ham']
print (shopp​ing​list)
list_num = 0
while list_num < len(sh​opp​ing​list):

By mmildmilds Published 10th February, 2016. Sponsored by Readability-Score.com


cheatography.com/mmildmilds/ Last updated 10th February, 2016. Measure your website readability!
Page 1 of 2. https://readability-score.com
python Cheat Sheet
by mmildmilds via cheatography.com/25734/cs/6848/

Symbol List

if/eli​f/ condit​ionals import random


else intlist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
random_int = random.choice (intlist)
While loop
print(​int​lis​t,r​and​om_int)
for list all the thing
fplist = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
!= If values of two operands are not equal, random_fp = random.choice (fplist)
then condition becomes true. print (fplis​t,r​and​om_fp)

== test if the 2 value are the same strlist = ["1",​"​2","3​"​,"4",​"​5","6​"​,"7",​"​8","9​"]


random_str = random.choice (strlist)
< less than
print (strli​st,​ran​dom​_str)
<= If the value of left operand is less than mylist =
or equal to the value of right operand, ["ad​am",​"​mil​d","l​ove​ada​m","l​evi​ne",​"​3","4.6",​424​,
then condition becomes true. 67​4,5.733]
> greater than random​_item = random.choice (mylist)
print (mylis​t,r​and​om_​item)
>= If the value of left operand is greater
myvar1 = 1
than or equal to the value of right
myvar2 = 2
operand, then condition becomes true.
myvar3 = 3
varlist = (myvar​1,m​yva​r2,​myvar3)
Guess word game
random_var = random.choice (varlist)
import random print (varli​st,​ran​dom​_var)

#create a list
Math - circle
guesslist = ['adam', 'mild', 'levine']
while True:
chance = 3
pi = 3.1415
score = 0
user_r​adius = input( " Insert radius here... " )
print (guess​list) radius = float(​use​r_r​adius)

while chance != 0: area = pi radius*2


print ( " the area of the circle is",​area)
random​_item = random.ch​oic​e(g​ues​slist)
print ( " Allahu Akbar")
user_input = input(​"​please guess a word: ")

if user_input == random​_item:

print (that's correc​t!")

else:

if user_input not in guesslist:

print ("sorry, that's wrong")

By mmildmilds Published 10th February, 2016. Sponsored by Readability-Score.com


cheatography.com/mmildmilds/ Last updated 10th February, 2016. Measure your website readability!
Page 2 of 2. https://readability-score.com

You might also like