0% found this document useful (0 votes)
3 views5 pages

Chutima Rakyu Python Cheat Sheet Muids

Uploaded by

uthmanayaba
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)
3 views5 pages

Chutima Rakyu Python Cheat Sheet Muids

Uploaded by

uthmanayaba
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

python cheat sheet MUIDS Cheat Sheet

by Chutima Rakyu via [Link]/25741/cs/6857/

Functions Print Code Count down code (cont)

print() Show inform​ation that name = "nae CHUTIMA" while number > 0:
you want on the screen print (name.u​pp​er()) ​ ​ ​ #add the number to the string
int() change number to be print (name.l​ow​er()) ​ ​ ​ ​#su​btract 1 from the number
number integer print (name.c​ap​ita​lize()) ​ ​ ​ ​cou​ntd​own​_string =
float() change number to be print (name.t​it​le()) countd​own​_string + str(nu​mber) +
decimal number "​"
" " " .... " " " commen​t(many lines) number to binary code ​ ​ ​ ​number = number-1

str() a list of number, letter user_number = ""


print (count​dow​n_s​tring)
and symbols #output should look like this
while user_n​umber != "​0":
# commen​t(one line) ​ ​
# if the user enter 5:
#5 4 3 2 1
input() receive inform​ation from ​ ​ ​ ​use​r_n​umber = input ( "​enter a
user number​" )
#print (count​dow​n_s​tring)

import random + pick random item from ​ ​ ​


[Link]​oic​e(list) the list The loop not go forever
​ ​ ​ ​number = int(us​er_​number)
​ ​ ​ ​bin​ary​_string = "​" gameover = 0
Random Code
while (number > 0 ):#the number is while(​gam​eover == 0):
import random greater than 0 ​ ​ ​ ​print ("he​llo​")
mylist = ['Dog'​,'F​ish', 'Cat', ​ ​ ​ ​rem​ainder = number % 2 ​ ​ ​ ​gam​eover = 1
'Bear'] ​ ​ ​ ​bin​ary​_string = str( remainder
counter = 0 )+ binary​_string print number in separate line in list mylist
while counter < 10: ​ ​ ​ ​number = number//2
mylist = [1,2,3,4,5]
​ ​ ​ ​ ​ ​ran​dom​_item = [Link] ​ ​ ​ ​print (number)
for number in mylist:
(mylist) ​ ​ ​
​ ​ ​ ​print (number)
​ ​ ​ ​ ​ ​print (rando​m_item) ​ ​ ​ ​print ( "​binary string is ",
​ ​ ​ ​ ​ ​counter = counter + 1 binary​_string )
using a while loop to print each item in list

wlist = [2,4,5,6,7,8]
Count down code
index = 0
#create a program that receives a
while index < len(wl​ist):
number from the user and count down
​ ​ ​ ​print (wlist​[in​dex])
from that number on the same line
​ ​ ​ ​index = index +1
#recive the number from the user as
a string
user_n​umber= input(​"​enter number​")
#convert the user number to an
integer
number = int(us​er_​number)
#setup the countdown string
countd​own​_string = "​"

By Chutima Rakyu Published 3rd February, 2016. Sponsored by [Link]


[Link]/chutima- Last updated 20th March, 2016. Measure your website readability!
rakyu/ Page 1 of 5. [Link]
python cheat sheet MUIDS Cheat Sheet
by Chutima Rakyu via [Link]/25741/cs/6857/

Definition Area The loop not go forever (copy) code

def areaOfTriangle (base,height): gameover = 0 mystring = "hello"


​ ​ ​ ​return base heigh 0.5 while(​gam​eover == 0): print (mystring)
base = float(​inp​ut(​'Enter the base ​ ​ ​ ​print ("he​llo​") firstname = input( "what is your
of the triang​le')) ​ ​ ​ ​gam​eover = 1 first name?")
height = float(​inp​ut(​inp​ut(​'Enter lastname = input( "what is your
the height of the triangle: ')) Addition last name?")
print('The area of the triangle fullname = firstname + " " +
string + string combine together
is',ar​eaO​fTr​ian​gle​(ba​se,​hei​ght)) lastname
string + number crash
def volume​OfPrism (area,​height) print (fullname)
number + number math - addition
​ ​ ​ ​return areaOf​Prism* height letter​number = int(input( " what
base = float(​inp​ut(​'Enter the area is letter number? " ))
Multip​lic​ation and Exponents
of the prism')) if letter​number >le​n(f​ull​name):
height = float(​inp​ut(​inp​ut(​'Enter string * number combine that string ​ ​ ​ ​print ( " invalid letter
the height of the prism: ')) string* string crash number, try again! " )
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ number * number math - multiply else:

string ** string crash ​ ​ ​ ​letter = (


Import random fullna​me[​let​ter​number] )
number ** number math - exponent
​ ​ ​ ​print (letter)
import random string ** number crash
​ ​ ​ ​num​ber​letter = int(input( "how
#create list
many times to print letter " ))
mylist = ['nadia' , 'nat' , 'lily' Area of Circle Code
​ ​ ​ if number​letter >100:
, 'eye']
while True:
​ ​ ​ ​ ​ ​ ​print ( " too many letters
#print​(my​lis​t[0])
​ ​ ​
to print! " )
# select a random item from the
​ ​ ​ ​use​r_r​adius = input(​"What is
​ ​ ​ ​else:
list
the radius​?")
​ ​ ​ ​ ​ ​ ​ ​print (letter *
counter = 0
​ ​ ​ ​radius = float(​use​r_r​adius)
number​letter )
while counter < 10:
​ ​ ​ pi = 3.1415
​ ​
​ ​ ​ ​ran​dom​_item =
​ ​ ​ ​area= pi radius * 2
[Link]​oic​e(m​ylist)
​ ​ ​ ​print ("The area of the circle
list code
​ ​ ​ ​print (rando​m_item)
is", area)
​ ​ ​ ​counter = counter + 1 shoppinglist = ['tshirt' , 'pants'
, 'socks']
for myvariable in shoppi​nlist:
​ ​ ​ ​print (myvar​iable)
print (shopp​ing​lis​t[1])
for number in range(5):
​ ​ ​ ​print (number)

By Chutima Rakyu Published 3rd February, 2016. Sponsored by [Link]


[Link]/chutima- Last updated 20th March, 2016. Measure your website readability!
rakyu/ Page 2 of 5. [Link]
python cheat sheet MUIDS Cheat Sheet
by Chutima Rakyu via [Link]/25741/cs/6857/

Random code 2 convert input to a integer multiply by 10 Math

import random while True: == equal to


intlist = [1,2,3,4] ​ ​ ​ ​use​r_n​umber = input(​"​Enter the != no equal to
random_int = number​") < less than
[Link]​oic​e(i​ntlist) ​ ​ ​ ​number = int(us​er_​num​ber)*10
> more than
print(​int​lis​t,r​and​om_int) ​ ​ ​ ​pri​nt(​number)
<= less than or equal to
fplist = [1.0, 2.0, 3.0, 4.0]
>= more than or equal to
random_fp = [Link]​oic​e(f​plist) palindrome
print(​fpl​ist​,ra​ndo​m_fp) % modulo, Find the remainder
while True:
strlist =
​ ​ ​ def palind​rom​e(w​ord):
VOCABULARY
['boo​k',​'pe​n',​'ba​g',​'pe​ncil']
​ ​ ​ ​ ​ ​ ​ ​reverse = "​"
random_str = variable hold a value and can be change
​ ​ ​ ​ ​ ​ ​ ​myr​esult = "​"
[Link]​oic​e(s​trlist)
​ ​ ​ ​ ​ ​ ​ for letters in word: string a list of character such as number,
print (strli​st,​ran​dom​_str) letter and symbols
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​reverse = letters +
mylist = [1, 1.0, 'beagle' ]
reverse integer whole number or counting number
random​_item = number
​ ​ ​ ​ ​ ​ ​ if word == reverse :
[Link]​oic​e(m​ylist)
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return True float the number in decimal
print(​myl​ist​,ra​ndo​m_item) number
​ ​ ​ ​ ​ ​ ​ ​else:
myvar1 = 1
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return False syntax grammar or structure of lauguage
myvar2 = 2 value the number or string can be store in
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​reverse = "​"
myvar3 = 3
​ ​ ​ word = input(​"​please enter a valuable
varlist =[myvar1, myvar2, myvar3] module the text for storing for python code
word: ")
random_var =
​ ​ ​ if word == "​qui​t": or find the remainder
[Link]​oic​e(v​arlist) input gain inform​ation from user
​ ​ ​ ​ ​ ​ ​ ​break
print(​var​lis​t,r​and​om_var)
​ ​ ​ ​the​result = palind​rom​e(word) print to show inform​ation on the screen
​ ​ ​ ​pri​nt(​"This word syntax make impossible to the parse
print fifth character from the variable
has"​,le​n(w​ord​),"l​ett​er") error
myword
​ ​ ​ boolean true/false
myword = "hellothere" ​ ​ ​ if theresult == True:
print ( myword[4] ) ​ ​ ​ ​ ​ ​ ​pri​nt(​Tru​e,'​',word + str('It
is a palind​rome'))
convert input to a integer multiply by 10 ​ ​ ​ ​else:

while True: ​ ​ ​ ​ ​ ​ ​pri​nt(​Fal​se,​'',word +str('It

​ ​ ​ ​use​r_n​umber = input(​"​Enter the is not a palind​rome'))

number​") ​ ​ ​

​ ​ ​ ​number = int(us​er_​num​ber)*10 ​ ​ ​

​ ​ ​ ​pri​nt(​number)
​ ​ ​

By Chutima Rakyu Published 3rd February, 2016. Sponsored by [Link]


[Link]/chutima- Last updated 20th March, 2016. Measure your website readability!
rakyu/ Page 3 of 5. [Link]
python cheat sheet MUIDS Cheat Sheet
by Chutima Rakyu via [Link]/25741/cs/6857/

Random Choice Code Random Choice Code (cont) create list (cont)

import random ​ ​ ​ ​ ​ ​ ​ ​if(​chances > 0): ​ ​ ​ ​ ​ ​ ​ ​dup​lic​ateword = False


mylist = ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Chances ​ ​ ​ ​ ​ ​ ​ # figure out if the word is
['beag​le'​,'p​ome​ran​ian​','​pug​','​gol​de remain​ing​:",c​hances) already in the list
n​','​chi​hua​hua'] ​ ​ ​ ​ ​ ​ ​ ​else: ​ ​ ​ ​ ​ ​ ​ for word in mylist:
score = 0 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​sta​rt_over = 2 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ if item == word:
chances = 3 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"Game Over! The ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​dup​lic​ateword =
start_over = 0 word was ", random​_item) True
random​_item = ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Chance ​ ​ ​ ​ ​ ​ ​ if duplic​ateword == True
[Link]​oic​e(m​ylist) remain​ing​:", chances) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​print ('Dupl​icate
while chances > 0: ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Final score:​", word!')
​ ​ ​ ​sta​rt_over = 0 score) ​ ​ ​ ​ ​ ​ ​ ​else:
​ ​ ​ ​ran​dom​_item = ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ # add this item to the
[Link]​oic​e(m​ylist) using loop to print out each item in list end of the list
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​myl​[Link]​pen​d(item)
forlist = ['hi','hello','bye']
​ ​ ​ ​while start_over < 1: #function call
for word in forlist:
​ ​ ​ ​ ​ ​ ​ ​print ("-=​-=-​=-=​-=-​=-=​- mylist = creatL​ist​("st​op")
​ ​ ​ ​pri​nt(​word)
=-​=-=​-=-​=-=​-=-​=-=​-") print(​mylist)
​ ​ ​ ​ ​ ​ ​ ​print ("Gu​essing Game")
create list
​ ​ ​ ​ ​ ​ ​ ​print ("-=​-=-​=-=​-=-​=-=​- Definition in each word
# create a function that allows a
=-​=-=​-=-​=-=​-=-​=-=​-")
def printDefinitions(word):
user to create a list
​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​wor​ds:​", mylist)
​ ​ ​ if word == "​var​iab​le":
#function name: word
​ ​ ​ ​ ​ ​ ​ ​guess = input(​"​Guess a
​ ​ ​ ​ ​ ​pri​nt(​"​"​"
#param​ater: word
word: ")
​ ​ ​ ​ 'A variable is things that
#return the list
​ ​ ​ ​ ​ ​ ​ if (guess in mylist):
able to change'
def createList (quitw​ord):
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​
​ ​ ​ ​ ​ ​"​"​")
​ ​ ​ ​mylist = [] #create an empty
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​if(​guess ==
​ ​ ​ elif word == "​fun​cti​on":
list
random​_item ):
​ ​ ​ ​ ​ ​pri​nt(​"​"​"
​ ​ ​ ​while True:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​That's
​ ​ ​ ​ ​ ​"A function is to help to use
​ ​ ​ ​ ​ ​ ​ #get the item from the user
correc​t!")
a code"
​ ​ ​ ​ ​ ​ ​ item = input(​'Please enter
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​score = score + 100
​ ​ ​ ​ ​ ​"​"​")
a list item')
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Sco​re:​",
​ ​ ​ elif word == "​var​iab​le":
​ ​ ​ ​ ​ ​ ​ # when the user enters an
score)
​ ​ ​ ​ ​ ​pri​nt(​"​"​"
item that is equal to quitword
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​sta​rt_over = 2
​ ​ ​ ​ ​ 'A variable is the things
​ ​ ​ ​ ​ ​ ​ if item == quitword:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​else:
that help you to change'
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​return mylist
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Sorry, wrong
​ ​ ​ ​ ​ ​"​"​")
​ ​ ​ ​ ​ ​ ​ # check if the list already
choice! ")
​ ​ ​ elif word == "​return
in the list
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​chances =
variab​le":
int(ch​ances) -1
​ ​ ​ ​ ​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​Sorry, that is
not even in the list")
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​chances = int(ch​ances)
-1

By Chutima Rakyu Published 3rd February, 2016. Sponsored by [Link]


[Link]/chutima- Last updated 20th March, 2016. Measure your website readability!
rakyu/ Page 4 of 5. [Link]
python cheat sheet MUIDS Cheat Sheet
by Chutima Rakyu via [Link]/25741/cs/6857/

Definition in each word (cont)

​ ​ ​ ​ ​ ​ ​ ​pri​nt(​"​"​"
​ ​ ​ ​ ​ ​ ​ 'A return variable is something that
return the function back to you'
​ ​ ​ ​ ​ ​ ​"​"​")
​ ​ ​ elif word == "​arg​ume​nt":
​ ​ ​ ​ ​ ​ ​pri​nt(​"​"​"
​ ​ ​ ​ ​ ​ 'A argument is something that give the
function to you'
​ ​ ​ ​ ​ ​ ​"​"​")
​ ​ ​ elif word == "​par​ame​ter​":
​ ​ ​ ​ ​ ​ ​pri​nt(​"​"​"
​ ​ ​ ​ ​ ​ 'A parameter is something that give
function'
​ ​ ​ ​ ​ ​ ​"​"​")
​ ​ ​ elif word == "​str​ing​":
​ ​ ​ ​ ​ ​ ​pri​nt(​"​"​"
​ ​ ​ ​ ​ ​ 'A string is the text, number or
anything that is list the charac​ters'
​ ​ ​ ​ ​ ​ ​ ​"​"​")
​ ​ ​ ​else:
​ ​ ​ ​ ​ ​ ​pri​nt(​"​"​"
​ ​ ​ ​ ​ ​'un​known word'
​ ​ ​ ​ ​ ​ ​"​"​")
user_word = input( "​Enter a word to define:
")
printD​efi​nit​ion​s(u​ser​_word)

By Chutima Rakyu Published 3rd February, 2016. Sponsored by [Link]


[Link]/chutima- Last updated 20th March, 2016. Measure your website readability!
rakyu/ Page 5 of 5. [Link]

You might also like