Sei sulla pagina 1di 9

Python Cheat Sheet

by Pear Tan via cheatography.com/25842/cs/6964/

Symbol Rule for naming the variable (cont) Define Function (cont)

float() number with decimal point # last name = no spaces allowed myprintnew (1,"​@@@​@@@​@")

str() string # last​-​name = dashes are not accepted def double​it(​num​ber):


​ ​ ​ ​return number * 2
int() integer
Define Function
print (doubl​eit(3))
len() the length of the word or string
varl = 1 print (doubl​eit​(do​ubl​eit​(4)))

_varl = 3 myvar = 12
Multiply
_varl + 100 myvar = double​it(​myvar)
string * Repeat those thing for the
print(​_varl) myvar = double​it(​myvar)
number number of time
def bacon(): # defines a functio print(​myvar)
string * Crash!
named bacon Result
string
​ ​ ​ ​print ("hello it's bacon") 3
number * Multiply like in math
​ ​ ​ ​print ("li​ne2​") hello it's bacon
number
​ ​ ​ ​print ("li​ne3​") line2
​ ​ ​ ​print ("li​ne4​") line3
Exponents
​ ​ ​ ​print ("li​ne5​") line4
string ** number Crash!
​ ​ ​ ​print ("li​ne6​") line5
number ** number Exponent in Math line6
​ ​ ​ ​print ("li​ne7​")
number ** string Crash! ​ ​ ​ ​return #exit the fuction line7

bacon() hello it's bacon


Rule for naming the variable bacon() line2

Rule for naming variab​​les bacon() line3

# letters def myprint (text): line4


# numbers ​ ​ ​ ​print ("*​*​*"+ str(text) line5
# underscore (_) line6
+"*​*​*")
# can either start with letter or under​​scores
​ ​ ​ ​return line7
ONLY
myprint(1) hello it's bacon
# no space
Exam​​ple myprin​t("h​ell​o") line2

Hello_​​there myprin​t(2.5) line3

me2 def myprintnew (text, decora​tion): line4


_mynumber ​ ​ ​ ​print (decor​ation + str(text) line5
Invalid names + decora​tion)
# 3my =cannot start with number
​ ​ ​ ​return
myprintnew (1,"​+++​")
myprintnew ('hell​o',​'-=​-=-​=-=​-
=-​=-=-=')

By Pear Tan Published 11th February, 2016. Sponsored by Readability-Score.com


cheatography.com/pear-tan/ Last updated 21st March, 2016. Measure your website readability!
Page 1 of 9. https://readability-score.com
Python Cheat Sheet
by Pear Tan via cheatography.com/25842/cs/6964/

Define Function (cont) Example (cont) Example (cont)

line6 Result [1, 'Hello', 2.5, True, False]


line7 What is your fisrt name? Pear
1 What is your last name? Tan word per line
hel​lo Pear Tan
mystr = input(" Please enter your
2.5 What is the letter of number? 4
word")
+++1+++ r
letter_num = 0
-=-=-=​-=-​=-=​-=-​=he​llo​-=-​=-=​-=-​=-=- How many times to print the letter?
while letter_num < len(my​str):
=-= 12
​ ​ ​ ​print (mystr​[le​tte​r_num])
@@@@@@​@1@​@@@@@@ rrrrrr​rrrrrr
​ ​ ​ ​let​ter_num = letter_num + 1
6
Result
16 Example
Please enter your word1,2,3,
48
mystr = "hello123" 1
numbers = [1,2,3​,4,5,6] ,
Example
print (numbers) 2
firstname = input("What is your shoppi​nglist = ,
fisrt name?") ['shoe​s',​'ba​gs'​,'p​ant​s',​'sh​irts'] 3
lastname = input(​"What is your last print (shopp​ing​list) ,
name?") mixed = [1, 'hello', 2.5, True,
fullname = firstname + " " + False] The area of circle
lastname print (mixed)
while True:
print(​ful​lname) letter_num = 0
​ ​ ​ ​use​r_r​adius = input(​"​Enter the
letter​number = input(​"What is the while letter_num < len(my​str):
radius of the circle​s")
letter of number​?") ​ ​ ​ ​print (mystr​[le​tte​r_num])
​ ​ ​ ​radius = float(​use​r_r​adius)
letter​number = int(le​tte​rnu​mber) ​ ​ ​ ​let​ter_num = letter_num + 1
​ ​ ​ pi = 3.1415
if letter​num​ber​>le​n(f​ull​name): for mylett​eri​saw​esome in mystr:
​ ​ ​ #
​ ​ ​ ​pri​nt(​"​Invalid letter number, ​ ​ ​ ​pri​nt(​myl​ett​eri​saw​esome)
​ ​ ​ ​answer = pi * (radiu​s**2)
try again") for tientien in shoppi​nglist:
​ ​ ​ ​pri​nt(​"The area of the circle
else: ​ ​ ​ ​pri​nt(​tie​mtiem)
is",​answer)
​ ​ ​ ​pri​nt(​ful​lna​me[​let​ter​num​ber]) out = 0
Result
​ ​ ​ ​times = input(​"How many times for mrtim in shoppi​nglist:
Enter the radius of the circles3
to print the letter​?") ​ ​ ​ out = out + 1
The area of the circle is
​ ​ ​ ​times = int(times) Result
28.273​500​000​000002
​ ​ ​ if times>100: [1, 2, 3, 4, 5, 6]
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"Too many letters to ['shoe', 'bags', 'pants', 'shirt']
print")
​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​ful​lna​me[​let​ter​num​be
r​]*t​imes)

By Pear Tan Published 11th February, 2016. Sponsored by Readability-Score.com


cheatography.com/pear-tan/ Last updated 21st March, 2016. Measure your website readability!
Page 2 of 9. https://readability-score.com
Python Cheat Sheet
by Pear Tan via cheatography.com/25842/cs/6964/

Palindrome Group work (cont) Group work (cont)

word = input("Please enter a ​ ​ ​ ​else: pear


string: ") ​ ​ ​ ​ ​ ​ ​ ​chance = chance - 1 Final score: 100
letter_num = 0 ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Chances
reverse = "​" Remain​ing​:",c​hance) Function
while letter_num <le​n(w​ord): ​ ​ ​ ​ ​ ​
input() inform​ation that receive from user
​ ​ ​ ​reverse = word[l​ett​er_num] + ​ ​ ​ ​ ​ ​ ​ ​#check if that word is in
print() show inform​ation in the screen
reverse the list
​ ​ ​ ​let​ter_num = letter_num + 1 ​ ​ ​ ​ ​ ​ ​ if user_guess in mylist:
Addition or Plus
if word == reverse: ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​print ("Sorry, wrong
string + string combine those strings
​ ​ ​ ​pri​nt(​"It is palind​rom​e") choice​")
together
else: ​ ​ ​ ​ ​ ​ ​ ​else:
string + number program will be crash
​ ​ ​ ​pri​nt(​"It is not palind​rom​e") ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Sorry, that is
not even in the list!") number + add together like doing
number math
Group work if chance == 0:
​ ​ ​ ​pri​nt(​ran​dom​_item)
""" Symbol
​ ​ ​ ​pri​nt(​"​Final score:​"​,score)
Group Members: Earn, Pop, Pear
Result + plus or add
Class: 1005
mylist: ['earn', 'pear', 'pop', - subtract
"​"​"
import random
'jaja', 'roong'] * multiply
Guess a word: pear
# create a list ** exponent
Chances Remaining: 4
mylist = ['earn', 'pear', 'pop', / divide and quotient (result) is float
Sorry, wrong choice
'jaja', 'roong'] // divide and quotient (result) is integer
Guess a word: earn
random​_item =
% remainder (modulo)
Chances Remaining: 3
random.ch​oic​e(m​ylist)
Sorry, wrong choice == equal to
#print
Guess a word: pop != not equal to
print(​"​myl​ist​:",m​ylist)
Chances Remaining: 2 <= less than or eqaul to
# ask the user to input the word
Sorry, wrong choice < less than
chance = 5
Guess a word: jaja
score = 0 > more than
Chances Remaining: 1
while chance!= 0:
Sorry, wrong choice
​ ​ ​ ​use​r_guess = input(​"​Guess a
Guess a word: roong
word: ")
That's correct! Score 100
​ ​ ​
Guess a word: jaja
​ ​ ​ if user_guess == random​_item:
Chances Remaining: 0
​ ​ ​ ​ ​ ​ ​ ​score = score + 100
Sorry, wrong choice
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​That's
correc​t!",​"​Sco​re",​score)
​ ​ ​ ​ ​ ​ ​ ​ran​dom​_item =
random.ch​oic​e(m​ylist)
​ ​ ​ ​ ​ ​

By Pear Tan Published 11th February, 2016. Sponsored by Readability-Score.com


cheatography.com/pear-tan/ Last updated 21st March, 2016. Measure your website readability!
Page 3 of 9. https://readability-score.com
Python Cheat Sheet
by Pear Tan via cheatography.com/25842/cs/6964/

Symbol (cont) Meaning of the word (cont) Meaning of the word (cont)

>= more than or equal to ​ ​ ​ ​ ​ ​ ​ ​"​"​") ​ ​ ​ word = input ("Enter the word")

# one line comment that will not ​ ​ ​ elif word == "​fun​cti​on": ​ ​ ​ ​pri​ntD​efi​nit​ion​(word)
included in the code ​ ​ ​ ​ ​ ​ ​ ​#fu​nction Result

"​"​" Multi-line comment ​ ​ ​ ​ ​ ​ ​ ​print (""" Enter the wordva​riable


​ ​ ​ ​ ​ ​ ​ A function is a thing that ​ ​ ​ ​ ​ ​
True or Always true
anything reuse block or quote. A variable is thing that can be
​ ​ ​ ​ ​ ​ ​ ​"​"​") changed
False and False
anything ​ ​ ​ elif word == "​par​ame​ter​": ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​#pa​rameter Enter the wordfu​nction
The area of circle 2 ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​"​" A function is a thing that reuse
​ ​ ​ ​ ​ ​ ​ A parameter is thing inside block or quote.
def areaOfCircle (user_radius):
blacket of function ​ ​ ​ ​ ​ ​ ​
​ ​ ​ if user_r​adi​us<=0:
​ ​ ​ ​ ​ ​ ​ ​"​"​") Enter the wordag​ument
​ ​ ​ ​ ​ ​ ​ ​return "​Error: invalid
​ ​ ​ elif word == "​agu​men​t": A argument is the same thing as
radius​"
​ ​ ​ ​ ​ ​ ​ ​#ar​gument parameter. It is thinfg inside
​ ​ ​
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​"​" blacket f function
​ ​ ​ pi = 3.1415
​ ​ ​ ​ ​ ​ ​ A argument is the same ​ ​ ​ ​ ​ ​
​ ​ ​ area = pi*(u​ser​_ra​diu​s**2)
thing as parameter. It is thinfg Enter the wordfu​nction call
​ ​ ​ ​return area
inside blacket f function Function is the thing make fuction
user_r​adius = float(​inp​ut(​"​Enter
​ ​ ​ ​ ​ ​ ​ ​"​"​") run.
the radius: "))
​ ​ ​ elif word == "​fun​ction call": ​ ​ ​ ​ ​ ​ ​
print('The area of the circle is',
​ ​ ​ ​ ​ ​ ​ ​#fu​nction call Enter the wordstring
areaOf​Cir​cle​(us​er_​radius)
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​"​" A string is a list of character
Enter the radius: 3
​ ​ ​ ​ ​ ​ ​ ​Fun​ction is the thing make ​ ​ ​ ​ ​ ​
The area of the circle is
fuction run. Enter the wordpear
28.273​500​000​000002
​ ​ ​ ​ ​ ​ ​ ​"​"​") unknown word
​ ​ ​ elif word == "​str​ing​":
Meaning of the word
​ ​ ​ ​ ​ ​ ​ ​#string Count down
def printDefinition(word):
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​"​"
user_number = input("What is the
​ ​ ​ # write a definition in your
​ ​ ​ ​ ​ ​ ​ A string is a list of
number?" )
own words for the folllowing words:
character
number = int(us​er_​number)
​ ​ ​ # use multi-line strings to
​ ​ ​ ​ ​ ​ ​ ​"​"​")
countd​own​_string =''
print the definition
​ ​ ​ ​else:
while number > 0:
​ ​ ​ ​#va​riable
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​unknown word")
​ ​ ​ ​cou​ntd​own​_string =
​ ​ ​ if word == "​var​iab​le":
while True:
countd​own​_string + str(nu​mber)
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​"​"
​ ​ ​ ​number = number-1
​ ​ ​ ​ ​ ​ ​ A variable is thing that
print(​cou​ntd​own​_st​ring)
can be changed
Result
What is the number? 5
54321

By Pear Tan Published 11th February, 2016. Sponsored by Readability-Score.com


cheatography.com/pear-tan/ Last updated 21st March, 2016. Measure your website readability!
Page 4 of 9. https://readability-score.com
Python Cheat Sheet
by Pear Tan via cheatography.com/25842/cs/6964/

List List (cont) List (cont)

import random [1, 2, 3, 4, 1.1, 2.2, 3.3, 4.4, print(​mixed)


# Create a list of integers 'phone', 'pencil', 'compu​ter'] letter_num = 0
intlist = [1,2,3,4] pencil while letter_num < len(my​str):
random_int = [1, 2, 3] 2 ​ ​ ​ ​print (mystr​[le​tte​r_num])
random.ch​oic​e(i​ntlist) ​ ​ ​ ​let​ter_num = letter_num + 1
print(​int​lis​t,r​and​om_​int​)#print the Guess game with random for mylett​eri​saw​esome in mystr:
entire list and the random item ​ ​ ​ ​pri​nt(​myl​ett​eri​saw​esome)
import random
# Create a list of floating point for tientien in shoppi​nglist:
# create a list
numbers ​ ​ ​ ​pri​nt(​opal)
mylist =
fplist​=[1.1,​2.2​,3.3​,4.4] shoppi​nglist. append​('t​ies')
['lion​','​che​eta​h',​'pa​nth​er'​,'c​oug​ar
random_fp = random.ch​oic​e(f​plist) print(​sho​ppi​nglist)
'​,'l​eop​ard']
print(​fpl​ist​,ra​ndo​m_fp) out = 0
random​_item =
# Create a list of strings for mrtim in shoppi​nglist:
random.ch​oic​e(m​ylist)
strlis​t= ​ ​ ​ ​out=out + 1
print(​ran​dom​_item)
[​'ph​one​','​pen​cil​','​com​puter'] ​ ​ ​ ​pri​nt(​mrtim)
#print
random_str = print (out)
print(​myl​ist[0])
random.ch​oic​e(s​trlist) largelist = range(100)
# ask the user to input the word
print(​str​lis​t,r​and​om_str) for num in largelist:
user_guess = input(​"​Guess a word:
mylist = ​ ​ ​ ​pri​nt(num)
")
[1,2,3​,4,​1.1​,2.2​,3.3,​4.4​,'p​hon​e',​'p
if user_guess == random​_item:
e​nci​l',​'co​mpu​ter'] Decision making
​ ​ ​ ​pri​nt(​"​Cor​rec​t")
random​_item =
else: f 3 < 2: #if statement must compare
random.ch​oic​e(m​ylist)
​ ​ ​ two Booleans
print(​myl​ist​,ra​ndo​m_item)
​ ​ ​ ​#check if that word is in the ​ ​ ​print ('3 is less than2 ')
#create a list of follwing
list elif 4 < 2: #can have 0 or more
veraibles
​ ​ ​ if user_guess in mylist: elif statements
myvar1 = 1
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"Yes, it is in the ​ ​ ​ ​print ('4 is less than 2')
myvar2 = 2
list") elif 5 < 2:
myvar3 = 3
​ ​ ​ ​else: ​ ​ ​ ​print ('5 is less than 2')
varlist = [myvar​1,m​yva​r2,​myvar3]
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"No, it is not in the else: #can have 0 or 1 else
random_var =
list") statement at the end
random.ch​oic​e(v​arlist)
​ ​ ​ ​print ('none of the above are
print(​var​lis​t,r​and​om_var)
List True')
Result
myself= "hello123"
[1, 2, 3, 4] 3
numbers = [1,2,3​,4,5,6]
[1.1, 2.2, 3.3, 4.4] 3.3
print(​num​bers)
['phone', 'pencil', 'compu​ter']
shoppi​nglist =
phone
['shoe​','​bag​s',​'pa​nts​','​shirt']
print(​sho​ppi​nglist)
mixed=​[1,​'He​llo​',2.5, True, False]

By Pear Tan Published 11th February, 2016. Sponsored by Readability-Score.com


cheatography.com/pear-tan/ Last updated 21st March, 2016. Measure your website readability!
Page 5 of 9. https://readability-score.com
Python Cheat Sheet
by Pear Tan via cheatography.com/25842/cs/6964/

Function Divisible by 3 Vocabulary

def nameOfFunction(): num = int(input(" Enter a word")) variable A value or thing that can be changed
​ ​ ​ ​ ​print (‘This function has no remainder = num%3 string A list of character such as letter or
parame​ters’) if remainder ==o: symbol
​ ​ ​ ​ ​print (‘This function has no ​ ​ ​pri​nt(​num​,"is divisible by 3") boolean True False
return value’) else:
modulo Find the remainder
​ ​ ​ ​ ​return # no value, just exits ​ ​ ​pri​nt(​num​,"is not divisible by
syntax There are error such as grammar or
the function 3")
structure of language
#function call
float A number with decimal point
nameOf​Fun​ction() Fibonacci fron o to 50
integer Rounded number which do not have
#function with 1
num1 = 0 decimal point
parame​ter​/ar​gument
num2 = 0
def testFu​nct​ion​(pa​ram):
fibonacci = num1+num2 Condition
print (‘This function has 1
myoutput = "​0,1​"
while... While this is true loop the command
parame​ter’)
while fibonacci < 50: under the condit​​ional
print (param)
​ ​ ​ ​ ​ ​ ​ ​ ​ ​myo​utput = myouput + "​,"
while Forever loop
#function call
+ str(fi​bon​acci) True
testFu​nction (“this is the
​ ​ ​ ​ ​ ​ ​ ​ ​ ​num​1=num2
for For every item in the list repeat the
parameter value”)
​ ​ ​ ​ ​ ​ ​ ​ ​ num2 = fibonacci each command under the loop that many
#function with 2 parameters and a
​ ​ ​ ​ ​ ​ ​ ​ ​ ​fib​onacci = num1+ num2 item in times. (a string is a list too)
return value name
print(my output)
def functi​on3​(pa​ram1, param2):
0,1,1​,2,​3,5​,8,​13,... of list
​ ​ ​ ​ ​ ​pri​nt(​‘This function has 2 If...: If the statement in 'if ' is true, it will
parame​ters’) then... follow the statement in 'then'. If it is
Sample
​ ​ ​ ​ ​ ​return param1 + param2 # else not, it will follow statement under
return value
def test() : 'else'.
​ ​ ​ ​while True:
#function call and store the result
​ ​ ​ ​ ​use​r_input = input(​"​Please
in a variable
enter a word: ")
return​Value = functi​on3(2, 3)
​ ​ ​ ​ if user_input == 'quit':
print (retur​nValue)
​ ​ ​ ​ ​ ​ ​ ​ ​break
​ ​ ​ ​return
Determine zero positive and negative
test()
num = int (input("Enter a number")
​keep asking word till input quit
if num>0:
​ ​
​ ​ ​ ​print (num,"is positi​ve")
elif num<0:
​ ​ ​ ​print (num,"is negati​ve")
else:
​ ​ ​ ​print (num,"is zero")

By Pear Tan Published 11th February, 2016. Sponsored by Readability-Score.com


cheatography.com/pear-tan/ Last updated 21st March, 2016. Measure your website readability!
Page 6 of 9. https://readability-score.com
Python Cheat Sheet
by Pear Tan via cheatography.com/25842/cs/6964/

The largest value The largest value (cont) Area of Triangle & Volume of prism (cont)

#write a function that returns the # name: maxlist #write a function that computes the
largest of two values #argument : list volume of a prism
#name : max2 #returns the largest value in the # name: volume​OfPrism
#agrum​ents: num1, num2 list # parameters :b,h,l
# return: largest value def maxlis​t(l​ist): # return : volume
# write a functrion that returns ​ ​ ​ ​max​value = list(0) def volume​OfP​ris​m(b​,h,l):
the largest of three values ​ ​ ​ for item in list: ​ ​ ​ ​volume = b hl
# name : max3 ​ ​ ​ ​ ​ ​ ​ if item > maxvalue: ​ ​ ​ ​return volume
#agrument: num1, num2, num3 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​value = item user_l​ength = float(​inp​ut(​'Enter
# return: largest value ​ ​ ​ ​ ​ ​ ​ the length of the prism:'))
def max2(n​um1​,num2): ​ ​ ​ ​return maxvalue print('The volume of the prism is',
​ ​ ​ if num1 >= num2: mylist = [1,2,3​,4,​55,​66,​777​,0,1] volume​OfP​ris​m(u​ser​_ba​se,​use​r_h​eig​ht
​ ​ ​ ​ ​ ​ ​ ​max​_value = (num1) print (maxli​st(​list)) ,​use​r_l​ength))
​ ​ ​ if num2 > num1: Result
​ ​ ​ ​ ​ ​ ​ ​max​_value = (num2) Area of Triangle & Volume of prism Enter the base of the triang​le:12
​ ​ ​ ​return max_value Enter the height of the triangle: 6
# write a function that computes
num1 = input(​'Enter the the first The area of the triangle is 36.0
the area of triangle
value') Enter the length of the prism:3
# name : areaOf​Tri​angle
num2 = input(​'Enter the the second The volume of the prism is 216.0
# parameters :b,h
value')
# return : area
print (max2(​num​1,n​um2)) Useful Function for Name
def areaOf​Tri​ang​le(​b,h):
def max3(n​um1​,nu​m2,​num3):
​ ​ ​ if user_b​ase​<=0: mystr = "hello THERE"
​ ​ ​ if num1 >= num2 and num1 >=
​ ​ ​ ​ ​ ​ ​ ​return "​Error: invalid print (mystr.up​per()) > HELLO
num3:
radius​" THERE
​ ​ ​ ​ ​ ​ ​ ​max​_value = (num1)
​ ​ ​ if user_h​eig​ht<=0: print (mystr.lo​wer()) > hello
​ ​ ​ if num2 > num1 and num2 >=
​ ​ ​ ​ ​ ​ ​ ​return "​Error: invalid there
num3:
radius​" print (mystr.ca​pit​ali​ze()) > Hello
​ ​ ​ ​ ​ ​ ​ ​max​_value = (num2)
​ ​ ​ area = 0.5 b h there
​ ​ ​ if num3 >= num2 and num3 >=
​ ​ ​ ​return area print (mystr.ti​tle()) > Hello
num1:
user_base =float​(in​put​('Enter the There
​ ​ ​ ​ ​ ​ ​ ​max​_value = (num3)
base of the triang​le:'))
​ ​ ​ ​ ​ ​ ​
user_h​eight = float(​inp​ut(​'Enter How to convert to binary
​ ​ ​ ​return max_value
the height of the triangle: '))
user_number = input("Please enter
num3 = input(​'Enter the the third
print ('The area of the triangle
a number")
value')
is',ar​eaO​fTr​ian​gle​(us​er_​bas​e,u​ser​_h
number = int(us​er_​number)
print (max3(​num​1,n​um2​,num3))
e​ight))
binary​_string =''
# write a function that returns the
while (number > 0):
largest value

By Pear Tan Published 11th February, 2016. Sponsored by Readability-Score.com


cheatography.com/pear-tan/ Last updated 21st March, 2016. Measure your website readability!
Page 7 of 9. https://readability-score.com
Python Cheat Sheet
by Pear Tan via cheatography.com/25842/cs/6964/

How to convert to binary (cont) Reverse While Loop with List:

​ ​ ​ ​rem​ainder= number%2 word = input("Please enter a word thelist = [4, 3, 2, 1, 0]


​ ​ ​ ​bin​ary​_string = str(re​mai​nder) to reverse: ") index = 0 # start at the first
+ binary​_string letter_num = 0 item
​ ​ ​ ​number= number//2 reverse = "​" while index < len(th​elist):
​ ​ ​ while letter_num <le​n(w​ord): ​ ​ ​ ​ ​ ​ ​ ​ ​print (theli​st[​index])
print(​"​Binary string is", ​ ​ ​ ​ ​reverse = word[l​ett​er_num] + #prints each item
binary​_st​ring) reverse ​ ​ ​ ​ ​ ​ ​ ​ ​index = index + 1
Result ​ ​ ​ ​ ​let​ter_num = letter_num + 1
Please enter a number 36 print(​"​Rev​erse: "​,re​verse) Lists
Binary string is 100100 Result
mylist = [2,3,4,5] # create a list
Please enter a word to reverse:
#select an item from a list
Guess a word 0123456
print (mylis​t[0]) #selects first
Reverse: 6543210
# create a list item and displays 2
mylist = # len() determines the length of
Range
['lion​','​che​eta​h',​'pa​nth​er'​,'c​oug​ar the list
'​,'l​eop​ard'] #creates a list of numbers from 0 print (len(m​ylist)) # displays 4
#print to the specified mylist.ap​pend(5) # adds an item to
print(​myl​ist[0]) number the end of the list
# ask the user to input the word numberlist = range(5)
user_guess = input(​"​Guess a word: # is the same as creating the Condition while loop
") following list
count = 0 # start at zero
#check if that word is in the list number​list2 = [0, 1, 2, 3, 4]
while count < 10: # loop while
if user_guess in mylist: for num in range(​100):
count is less than 10
​ ​ ​ ​pri​nt(​"Yes, it is in the list") ​ ​ ​ ​ ​print (num) # prints all
​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​count) #will print
else: numbers from 0 – 99
numbers 0 - 9
​ ​ ​ ​pri​nt(​"No, it is not in the for num in range(5, 50):
​ ​ ​ ​ ​ ​ ​ ​ ​ ​count = count + 1 # must
list") ​ ​ ​ ​ ​ ​pri​nt(num) #prints all
increase count
numbers from 5 - 49
Boolean
palindrome
For loop with list
print(True)
def palindrome(word):
print (2<3) forlist = [3, 4, 5, 2, 1]
​ ​ ​ ​let​ter_num = 0
print (2 != 2) for item in forlist:
​ ​ ​ ​reverse = "​"
​ ​ ​ ​ ​ ​pri​nt(​item)
​ ​ ​ for letter_num in word:
print all items in the list
​ ​ ​ ​ ​ ​ ​ ​ ​reverse = letter_num +
reverse
​ ​ ​ if word == reverse:
​ ​ ​ ​ ​ ​ ​ ​return True
​ ​ ​ ​else:

By Pear Tan Published 11th February, 2016. Sponsored by Readability-Score.com


cheatography.com/pear-tan/ Last updated 21st March, 2016. Measure your website readability!
Page 8 of 9. https://readability-score.com
Python Cheat Sheet
by Pear Tan via cheatography.com/25842/cs/6964/

palindrome (cont) Example from sheet Positive integer count (cont)

​ ​ ​ ​ ​ ​ ​ ​return False mystring = " " program that repeatedly recieve


while True: count = 0 positive integers from the user.
​ ​ ​ ​use​r_word = input(​"​Please enter while count < 5: When the user enters a negative
a word: ") ​ ​ ​ ​ ​ ​mys​tring = mystring+ integer, exit the loop and print
​ ​ ​ if user_word != "​qui​t": str(count) how many of the numbers were odd
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"This word ​ ​ ​ ​ ​ ​print (mystring) and even
has"​,le​n(u​ser​_wo​rd)​,"le​tte​rs") ​ ​ ​ ​ ​ ​count = count + 1
​ ​ ​ if user_word == "​qui​t": mystring = " "
​ ​ ​ ​ ​ ​ ​ ​ ​break for num in range(5):
​ ​ ​ if palind​rom​e(u​ser​_word) == ​ ​ ​ ​ ​ ​mys​tring = mystring+
True: str(count)
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​use​r_w​ord​,"is ​ ​ ​ ​ ​ ​print (mystr​ing)g
palind​rom​e") Result
​ ​ ​ ​else: 0
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​use​r_w​ord​,"is not 01
palind​rom​e") 012
Result 0123
Please enter a word: 321 01234
This word has 3 letters
321 is not palindrome Positive integer count
Please enter a word: 212
evencount = 0
This word has 3 letters
oddcount = 0
212 is palindrome
while True:
Please enter a word: quit
​ ​ ​ ​ ​ ​ ​ ​ num = int (input​("Enter a
positive intege​r"))
while loop with counting number
​ ​ ​ ​ ​ ​ ​ ​ if num < 0:
num = -100 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​print ("Even
while num< -1: number​s:",​eve​ncount)
​ ​ ​ ​ ​pri​nt(num) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​print ("Odd
​ ​ ​ ​ num = num + 2 number​s:",odd count)
num = 0 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​break
while num< 100: ​ ​ ​ ​ ​ ​ ​ ​ ​else:
​ ​ ​ ​ num = num + 2 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ if (num%2) == 0:
​ ​ ​ ​ ​pri​nt(num) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​eve​ncount =
​ ​ ​ ​ evencount + 1
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​odd​count =
oddcount + 1

By Pear Tan Published 11th February, 2016. Sponsored by Readability-Score.com


cheatography.com/pear-tan/ Last updated 21st March, 2016. Measure your website readability!
Page 9 of 9. https://readability-score.com

Potrebbero piacerti anche