0% found this document useful (0 votes)
65 views

Python Cheat Sheet: by Via

This Python cheat sheet provides: 1) Definitions and examples of common Python functions like str(), print(), len(), and import. 2) Examples of using lists, loops, user input, and random choice to generate outcomes. 3) Code snippets for stripping names, guessing games, calculating ellipse areas, and more. 4) A vocabulary section defining terms like variable, float, string, and syntax.

Uploaded by

pca_pca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Python Cheat Sheet: by Via

This Python cheat sheet provides: 1) Definitions and examples of common Python functions like str(), print(), len(), and import. 2) Examples of using lists, loops, user input, and random choice to generate outcomes. 3) Code snippets for stripping names, guessing games, calculating ellipse areas, and more. 4) A vocabulary section defining terms like variable, float, string, and syntax.

Uploaded by

pca_pca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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(fu​lln​ame): ​ ​ ​ ​else:

input() to get infomation from the ​ ​ ​ ​print ("in​valid letter number, ​ ​ ​ ​ ​ ​ ​ ​print ("So​rry​,that is not
user try again") even in the list!")

print() to show some infomation else: ​ ​ ​ if chance == 0:


that in the code ​ ​ ​ ​print (fulln​ame​[my​num​ber]) ​ ​ ​ ​ ​ ​ ​ ​print ("Game Over! The word

int() the integers ​ ​ ​ ​repeat = input(​"how many times was"​,ra​ndo​m_item)


you want to print the letter? ") ​ ​ ​ ​ ​ ​ ​ ​print ("Final score:
len() length of the string
​ ​ ​ ​myr​epeat = int(re​peat) "​,score)
# comment
​ ​ ​ if (myrepeat) > 99:
"​"​"........................ multiple line comment
​ ​ ​ ​ ​ ​ ​ ​print ("too many letter! ") write allthe​outcome
"​"​"​
​ ​ ​ ​else:
mystring = ""
List2 ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​ful​lna​me[​myn​umb​er]​*
count = 0
(m​yre​peat))
while count < 5:
mylist = [1,2,3,4,5]
​ ​ ​
mulist2 = ['hi', 'hello', ​ ​ ​ ​mys​tring = mystring +
str(count)
'anyth​ing']
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: 'Patte​r',​'Ti​m',​'Lily'] 012


random​_item = 0123
​ ​ ​ ​print (num)
random.ch​oic​e(m​ylist) 01234
while chance > 0:
Name strip
​ ​ ​ ​print (mylist) areaof​Ellipse
firstname = input("what is your
​ ​ ​
first name? ") def areaofEllipse(radius1,
​ ​ ​ ​print ("Ch​ances Remaining
radius2):
lastname = input(​"what is your
=",c​hance)
​ ​ ​ pi = 3.142
lastname? ")
​ ​ ​ ​guess = input(​"​Guess a word
​ ​ ​ area = pi rad​ius​1r​adius2
fullname = firstname + " " +
from the above :")
​ ​ ​ ​return area
lastname
​ ​ ​
print(​"Your fullname is ") ra1 = int(in​put​("put in the radius
​ ​ ​ if guess == random​_item:
1"))
print (fullname)
​ ​ ​ ​ ​ ​ ​ ​score = score + 100
ra2 = int(in​put​("put in the radius
letter​number = input(​"what is
​ ​ ​ ​ ​ ​ ​ ​print ("That's
2"))
letter number? ")
correc​t!",​"The score is :",s​core)
mynumber = int(le​tte​rnu​mber)-1 print (areao​fEl​lip​se(ra1, ra2))
​ ​ ​ ​ ​ ​ ​ ​ran​dom​_item =
OUTPUT
random.ch​oic​e(m​ylist)
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/

areaof​Ellipse (cont) DEFINATION (cont) Reverse2

62.839​999​999​999996 ​ ​ ​ ​pri​ntD​efi​nit​ion​(us​er_​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 == "​var​iab​le":
​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​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 == "​par​ame​ter​": Float Decimal number number = int(us​er_​number)
​ ​ ​ ​ ​ ​ ​ ​print (""" binary​_string = ''
String Number, Letter, and symble
​ ​ ​ ​ ​ ​ ​ A parameter is the limiting while (number > 0):
Syntax The programing language
factor ​ ​ ​ ​rem​ainder = number % 2
Boolean True or False
​ ​ ​ ​ ​ ​ ​ ​"​"​") ​ ​ ​ ​bin​ary​_string =
Paramiter some thing you give to function
​ ​ ​ elif word == "​arg​ume​nt": str(re​mai​nder) +
​ ​ ​ ​ ​ ​ ​ ​print (""" str(bi​nar​y_s​tring)
Reverse
​ ​ ​ ​ ​ ​ ​ An argument is the ​ ​ ​ ​number = number // 2
identifier that you give to reverse = "" print ("Binary string
function letter_num = 0 is",​bin​ary​_st​ring)
​ ​ ​ ​ ​ ​ ​ ​"​"​") word = input(​'type in a word: ')

​ ​ ​ elif word == "​str​ing​": "​"​" Countdown


​ ​ ​ ​ ​ ​ ​ ​print (""" while letter_num < len(word):
user_number = input("Please enter
​ ​ ​ ​ ​ ​ ​ A string is something that ​ ​ ​ ​reverse = word[l​ett​er_num] +
a number: ")
can be repeated by the number. reverse
number = int(us​er_​number)
​ ​ ​ ​ ​ ​ ​ ​"​"​") ​ ​ ​ ​let​ter_num = letter_num + 1
countd​own​_string = ''
​ ​ ​ elif word == "​fun​ction call": "​"​"
while number > 0:
​ ​ ​ ​ ​ ​ ​ ​print (""" for letter in word:
​ ​ ​ ​cou​ntd​own​_string =
​ ​ ​ ​ ​ ​ ​ A function call is the word ​ ​ ​ ​reverse = letter + reverse
countd​own​_string + str(nu​mber)
you use to reuse the function. print ('reverse: ',reverse)
​ ​ ​ ​number = number - 1
​ ​ ​ ​ ​ ​ ​ ​"​"​")
print (count​dow​n_s​tring)
​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​ ​print ("un​known word")
while True:
​ ​ ​ ​use​r_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): ​ ​ ​ ​ ​ ​ ​ ​max​value = num2 == Equal to...


​ ​ ​ if r <= 0: ​ ​ ​ ​ ​ ​ ​ != Not equal
​ ​ ​ ​ ​ ​ ​ ​return "​Error: radius <=0​" ​ ​ ​ if num3 > maxvalue: < Less than
​ ​ ​ pi = 3.1415 ​ ​ ​ ​ ​ ​ ​ ​max​value = num3
> More than
​ ​ ​ area = pi r * 2 ​ ​ ​ ​ ​ ​ ​
<= Less than or equal
​ ​ ​ ​return area ​ ​ ​ ​return maxvalue
>= More than or equal
user_r​adius = input(​"​enter the print(​max​3(3​,5,9))
radius: ") % Modulo​/Find remainder

radius = float(​use​r_r​adius) Decision Making​/Co​ndi​tional Statem​ents:


List
print(​"The area of the circle is",
if 3 < 2: #if statement must
aofc(r​adius)) shoplist = ['son', 'goo', 'maaa',
compare two Booleans
'laaaa']
​print ('3 is less than 2')
Max value print(​sho​pli​st[2])
elif 4 < 2: #can have 0 or more
"​"​"
# write a function that returns the elif statements
item_n​umber = 0
largest of two values ​print ('4 is less than 2')
while item_n​umber <
# name: max2 elif 5 < 2:
len(sh​opl​ist):
# arguments: num1, num2 ​print ('5 is less than 2')
​ ​ ​ ​print ("list item:",
# return: the largest value else: #can have 0 or 1 else
shopli​st[​ite​m_n​umber])
def max2 (num1, num2): statement at the end
​ ​ ​ ​ite​m_n​umber = item_n​umber + 1
​ ​ ​ ​max​value = num1 ​print ('none of the above are
"​"​"
​ ​ ​ True')
out = 0
​ ​ ​ if num2 > num1:
for item in shoplist:
​ ​ ​ ​ ​ ​ ​ ​max​value = num2 MATH
​ ​ ​ out = out + 1
​ ​ ​ ​return maxvalue
string​+string Combine
​ ​ ​ ​ ​ ​ ​ ​#print ('list item:'​,item)
print(​max​2(1​0,9))
string - number CRASH print (out)
print(​max​2(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​**s​tring CRASH number = int(us​er_​number)
def max3 (num1, num2, num3): hex_string = ' '
string​**n​umber CRASH
​ ​ ​ ​max​value = num1 while (number > 0):
number​%number remainder
​ ​ ​ if num2 > maxvalue: ​ ​ ​ ​rem​aider = number % 16
number​**n​umber Exponent
​ ​ ​ if remaider == 10:
= Equal
​ ​ ​ ​ ​ ​ ​ ​rem​aider = '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


​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​rem​aider = 'B' myvar3 = 3 palind​rom​e")
​ ​ ​ elif remaider == 12: varlist = (myvar1, myvar2, myvar3) ​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​rem​aider = 'C' random_var =rando​m.c​hoi​ce(​var​list) ​ ​ ​ ​ ​ ​ ​ ​print (word,​"it is not a
​ ​ ​ elif remaider == 13: print (varli​st,​ran​dom​_var) palind​rom​")
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​rem​aider = 'D' "​"​"
​ ​ ​ elif remaider == 14: Palindrome output
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​rem​aider = 'E' ​Insert your word: klk
'''
​ ​ ​ elif remaider == 15: 3
Surawut Sartpant
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​rem​aider = 'F' klk it is a palindrome
Son 1002
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​Insert your word: quit
Python Assign​men​t-P​ali​ndrome
​ ​ ​ ​hex​_string = str(re​maider) + ​"​"​"
'''
str(he​x_s​tring)
def isPali​ndr​ome​(word):
​ ​ ​ ​number = number // 16
​ ​ ​ ​numlen = 0
print ("He​xad​ecimal string is 0x",
​ ​ ​ ​while numlen < long // 2 + 1:
hex_st​ring)
​ ​ ​ ​ ​ ​ ​ if word[n​umlen] !=
word[-​num​len-1]:
Import list
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return False
import random ​ ​ ​ ​ ​ ​ ​ elif word[n​umlen] == 1:
intlist = [1, 2, 3, 4, 5] ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return True
random_int = ​ ​ ​ ​ ​ ​ ​ ​numlen += 1
random.ch​oic​e(i​ntlist) ​ ​ ​ ​else:
print (intli​st,​ran​dom​_int) ​ ​ ​ ​ ​ ​ ​ ​return True
fplist = [float(1), float(2), ​ ​ ​ ​ ​ ​ ​
float(3), float(4), float(5)] ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
random_fp = random.ch​oic​e(f​plist) while True:
print (fplis​t,r​and​om_fp) ​ ​ ​ word = input(​" Insert your
strlist = [str('​son'), word: ")
str('h​ack'), str('p​om'), ​ ​ ​ long = len(word)
str('p​hon')] ​ ​ ​ if word == "​qui​t":
random_str = ​ ​ ​ ​ ​ ​ ​ ​break
random.ch​oic​e(s​trlist) ​ ​ ​ ​else:
print (strli​st,​ran​dom​_str) ​ ​ ​ ​ ​ ​ ​ ​print (long)
mylist = ['son', 'jan', 'feb',
'mar'] ​ ​ ​ ​numlen = 0
random_my = random.ch​oic​e(m​ylist) ​ ​ ​ if isPali​ndr​ome​(word) == True:
print (mylis​t,r​and​om_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

You might also like