Python Cheat Sheet
by Sonnylnw MUIDS via cheatography.com/25876/cs/6997/
Function Name strip (cont) Import random (cont)
str() make the number or symbol if (mynumber) > len(fullname): else:
input() to get infomation from the print ("invalid letter number, print ("Sorry,that is not
user try again") even in the list!")
print() to show some infomation else: if chance == 0:
that in the code print (fullname[mynumber]) print ("Game Over! The word
int() the integers repeat = input("how many times was",random_item)
you want to print the letter? ") print ("Final score:
len() length of the string
myrepeat = int(repeat) ",score)
# comment
if (myrepeat) > 99:
"""........................ multiple line comment
print ("too many letter! ") write alltheoutcome
"""
else:
mystring = ""
List2 print(fullname[mynumber]*
count = 0
(myrepeat))
while count < 5:
mylist = [1,2,3,4,5]
mulist2 = ['hi', 'hello', mystring = mystring +
str(count)
'anything']
Import random
mylist3 = [1, 'hello', 2.5 print (mystring)
import random count = count + 1
print (mylist)
print (mylist2) chance = 3 OUTCOMEIS
score = 0 0
print (mylist3)
number = range(100) mylist = ['Hack', 'ToeyD.', 01
for nm in number: 'Patter','Tim','Lily'] 012
random_item = 0123
print (num)
random.choice(mylist) 01234
while chance > 0:
Name strip
print (mylist) areaofEllipse
firstname = input("what is your
first name? ") def areaofEllipse(radius1,
print ("Chances Remaining
radius2):
lastname = input("what is your
=",chance)
pi = 3.142
lastname? ")
guess = input("Guess a word
area = pi radius1radius2
fullname = firstname + " " +
from the above :")
return area
lastname
print("Your fullname is ") ra1 = int(input("put in the radius
if guess == random_item:
1"))
print (fullname)
score = score + 100
ra2 = int(input("put in the radius
letternumber = input("what is
print ("That's
2"))
letter number? ")
correct!","The score is :",score)
mynumber = int(letternumber)-1 print (areaofEllipse(ra1, ra2))
random_item =
OUTPUT
random.choice(mylist)
put in the radius 14
else:
put in the radius 25
print ("Sorry, wrong
choice!")
chance = chance - 1
if guess in mylist:
print ("")
By Sonnylnw MUIDS Published 5th February, 2016. Sponsored by CrosswordCheats.com
cheatography.com/sonnylnw- Last updated 18th March, 2016. Learn to solve cryptic crosswords!
muids/ Page 1 of 4. http://crosswordcheats.com
Python Cheat Sheet
by Sonnylnw MUIDS via cheatography.com/25876/cs/6997/
areaofEllipse (cont) DEFINATION (cont) Reverse2
62.839999999999996 printDefinition(user_input) hello = "hello there!"
OUTPUT out = ""
DEFINATION Please type the word :variable for letter in hello:
A variable is the the thing out = letter+out
def printDefinition(word):
that can be changed. print(out)
if word == "variable":
print ("""
Number to Binary
A variable is the the thing
Vocab
that can be changed. user_number = input("Enter number
""") Variable The unknow that can be change to convert to binary : ")
elif word == "parameter": Float Decimal number number = int(user_number)
print (""" binary_string = ''
String Number, Letter, and symble
A parameter is the limiting while (number > 0):
Syntax The programing language
factor remainder = number % 2
Boolean True or False
""") binary_string =
Paramiter some thing you give to function
elif word == "argument": str(remainder) +
print (""" str(binary_string)
Reverse
An argument is the number = number // 2
identifier that you give to reverse = "" print ("Binary string
function letter_num = 0 is",binary_string)
""") word = input('type in a word: ')
elif word == "string": """ Countdown
print (""" while letter_num < len(word):
user_number = input("Please enter
A string is something that reverse = word[letter_num] +
a number: ")
can be repeated by the number. reverse
number = int(user_number)
""") letter_num = letter_num + 1
countdown_string = ''
elif word == "function call": """
while number > 0:
print (""" for letter in word:
countdown_string =
A function call is the word reverse = letter + reverse
countdown_string + str(number)
you use to reuse the function. print ('reverse: ',reverse)
number = number - 1
""")
print (countdown_string)
else:
print ("unknown word")
while True:
user_input = input("Please type
the word :")
By Sonnylnw MUIDS Published 5th February, 2016. Sponsored by CrosswordCheats.com
cheatography.com/sonnylnw- Last updated 18th March, 2016. Learn to solve cryptic crosswords!
muids/ Page 2 of 4. http://crosswordcheats.com
Python Cheat Sheet
by Sonnylnw MUIDS via cheatography.com/25876/cs/6997/
Find Area of Circle Max value (cont) Symbol
def aofc(r): maxvalue = num2 == Equal to...
if r <= 0: != Not equal
return "Error: radius <=0" if num3 > maxvalue: < Less than
pi = 3.1415 maxvalue = num3
> More than
area = pi r * 2
<= Less than or equal
return area return maxvalue
>= More than or equal
user_radius = input("enter the print(max3(3,5,9))
radius: ") % Modulo/Find remainder
radius = float(user_radius) Decision Making/Conditional Statements:
List
print("The area of the circle is",
if 3 < 2: #if statement must
aofc(radius)) shoplist = ['son', 'goo', 'maaa',
compare two Booleans
'laaaa']
print ('3 is less than 2')
Max value print(shoplist[2])
elif 4 < 2: #can have 0 or more
"""
# write a function that returns the elif statements
item_number = 0
largest of two values print ('4 is less than 2')
while item_number <
# name: max2 elif 5 < 2:
len(shoplist):
# arguments: num1, num2 print ('5 is less than 2')
print ("list item:",
# return: the largest value else: #can have 0 or 1 else
shoplist[item_number])
def max2 (num1, num2): statement at the end
item_number = item_number + 1
maxvalue = num1 print ('none of the above are
"""
True')
out = 0
if num2 > num1:
for item in shoplist:
maxvalue = num2 MATH
out = out + 1
return maxvalue
string+string Combine
#print ('list item:',item)
print(max2(10,9))
string - number CRASH print (out)
print(max2(1,9))
number + number ADDING
# write a function that returns the
string*number combine Number to Hex
largest of three values
# name: max3 string*string CRASH user_number = input("please enter
# arguments: num1, num2, num3 number*number Multiply a number: ")
# return: the largest value string**string CRASH number = int(user_number)
def max3 (num1, num2, num3): hex_string = ' '
string**number CRASH
maxvalue = num1 while (number > 0):
number%number remainder
if num2 > maxvalue: remaider = number % 16
number**number Exponent
if remaider == 10:
= Equal
remaider = 'A'
== Compare
// Divide anser in Interger
/ Divide anser in Floot
By Sonnylnw MUIDS Published 5th February, 2016. Sponsored by CrosswordCheats.com
cheatography.com/sonnylnw- Last updated 18th March, 2016. Learn to solve cryptic crosswords!
muids/ Page 3 of 4. http://crosswordcheats.com
Python Cheat Sheet
by Sonnylnw MUIDS via cheatography.com/25876/cs/6997/
Number to Hex (cont) Import list (cont) Palindrome (cont)
elif remaider == 11: myvar2 = 2 print (word,"it is a
remaider = 'B' myvar3 = 3 palindrome")
elif remaider == 12: varlist = (myvar1, myvar2, myvar3) else:
remaider = 'C' random_var =random.choice(varlist) print (word,"it is not a
elif remaider == 13: print (varlist,random_var) palindrom")
remaider = 'D' """
elif remaider == 14: Palindrome output
remaider = 'E' Insert your word: klk
'''
elif remaider == 15: 3
Surawut Sartpant
remaider = 'F' klk it is a palindrome
Son 1002
Insert your word: quit
Python Assignment-Palindrome
hex_string = str(remaider) + """
'''
str(hex_string)
def isPalindrome(word):
number = number // 16
numlen = 0
print ("Hexadecimal string is 0x",
while numlen < long // 2 + 1:
hex_string)
if word[numlen] !=
word[-numlen-1]:
Import list
return False
import random elif word[numlen] == 1:
intlist = [1, 2, 3, 4, 5] return True
random_int = numlen += 1
random.choice(intlist) else:
print (intlist,random_int) return True
fplist = [float(1), float(2),
float(3), float(4), float(5)]
random_fp = random.choice(fplist) while True:
print (fplist,random_fp) word = input(" Insert your
strlist = [str('son'), word: ")
str('hack'), str('pom'), long = len(word)
str('phon')] if word == "quit":
random_str = break
random.choice(strlist) else:
print (strlist,random_str) print (long)
mylist = ['son', 'jan', 'feb',
'mar'] numlen = 0
random_my = random.choice(mylist) if isPalindrome(word) == True:
print (mylist,random_my)
myvar1 = 1
By Sonnylnw MUIDS Published 5th February, 2016. Sponsored by CrosswordCheats.com
cheatography.com/sonnylnw- Last updated 18th March, 2016. Learn to solve cryptic crosswords!
muids/ Page 4 of 4. http://crosswordcheats.com