python cheat sheet MUIDS Cheat Sheet
by Chutima Rakyu via [Link]/25741/cs/6857/
Functions Print Code Count down code (cont)
print() Show information that name = "nae CHUTIMA" while number > 0:
you want on the screen print (name.upper()) #add the number to the string
int() change number to be print (name.lower()) #subtract 1 from the number
number integer print (name.capitalize()) countdown_string =
float() change number to be print (name.title()) countdown_string + str(number) +
decimal number ""
" " " .... " " " comment(many lines) number to binary code number = number-1
str() a list of number, letter user_number = ""
print (countdown_string)
and symbols #output should look like this
while user_number != "0":
# comment(one line)
# if the user enter 5:
#5 4 3 2 1
input() receive information from user_number = input ( "enter a
user number" )
#print (countdown_string)
import random + pick random item from
[Link]oice(list) the list The loop not go forever
number = int(user_number)
binary_string = "" gameover = 0
Random Code
while (number > 0 ):#the number is while(gameover == 0):
import random greater than 0 print ("hello")
mylist = ['Dog','Fish', 'Cat', remainder = number % 2 gameover = 1
'Bear'] binary_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]
random_item = [Link] print (number)
for number in mylist:
(mylist)
print (number)
print (random_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(wlist):
number from the user and count down
print (wlist[index])
from that number on the same line
index = index +1
#recive the number from the user as
a string
user_number= input("enter number")
#convert the user number to an
integer
number = int(user_number)
#setup the countdown string
countdown_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(gameover == 0): print (mystring)
base = float(input('Enter the base print ("hello") firstname = input( "what is your
of the triangle')) gameover = 1 first name?")
height = float(input(input('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',areaOfTriangle(base,height)) lastname
string + number crash
def volumeOfPrism (area,height) print (fullname)
number + number math - addition
return areaOfPrism* height letternumber = int(input( " what
base = float(input('Enter the area is letter number? " ))
Multiplication and Exponents
of the prism')) if letternumber >len(fullname):
height = float(input(input('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 fullname[letternumber] )
number ** number math - exponent
print (letter)
import random string ** number crash
numberletter = int(input( "how
#create list
many times to print letter " ))
mylist = ['nadia' , 'nat' , 'lily' Area of Circle Code
if numberletter >100:
, 'eye']
while True:
print ( " too many letters
#print(mylist[0])
to print! " )
# select a random item from the
user_radius = input("What is
else:
list
the radius?")
print (letter *
counter = 0
radius = float(user_radius)
numberletter )
while counter < 10:
pi = 3.1415
random_item =
area= pi radius * 2
[Link]oice(mylist)
print ("The area of the circle
list code
print (random_item)
is", area)
counter = counter + 1 shoppinglist = ['tshirt' , 'pants'
, 'socks']
for myvariable in shoppinlist:
print (myvariable)
print (shoppinglist[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] user_number = input("Enter the != no equal to
random_int = number") < less than
[Link]oice(intlist) number = int(user_number)*10
> more than
print(intlist,random_int) print(number)
<= less than or equal to
fplist = [1.0, 2.0, 3.0, 4.0]
>= more than or equal to
random_fp = [Link]oice(fplist) palindrome
print(fplist,random_fp) % modulo, Find the remainder
while True:
strlist =
def palindrome(word):
VOCABULARY
['book','pen','bag','pencil']
reverse = ""
random_str = variable hold a value and can be change
myresult = ""
[Link]oice(strlist)
for letters in word: string a list of character such as number,
print (strlist,random_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]oice(mylist)
return True float the number in decimal
print(mylist,random_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 == "quit": or find the remainder
[Link]oice(varlist) input gain information from user
break
print(varlist,random_var)
theresult = palindrome(word) print to show information on the screen
print("This word syntax make impossible to the parse
print fifth character from the variable
has",len(word),"letter") error
myword
boolean true/false
myword = "hellothere" if theresult == True:
print ( myword[4] ) print(True,'',word + str('It
is a palindrome'))
convert input to a integer multiply by 10 else:
while True: print(False,'',word +str('It
user_number = input("Enter the is not a palindrome'))
number")
number = int(user_number)*10
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 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): duplicateword = False
mylist = print("Chances # figure out if the word is
['beagle','pomeranian','pug','golde remaining:",chances) already in the list
n','chihuahua'] else: for word in mylist:
score = 0 start_over = 2 if item == word:
chances = 3 print("Game Over! The duplicateword =
start_over = 0 word was ", random_item) True
random_item = print("Chance if duplicateword == True
[Link]oice(mylist) remaining:", chances) print ('Duplicate
while chances > 0: print("Final score:", word!')
start_over = 0 score) else:
random_item = # add this item to the
[Link]oice(mylist) using loop to print out each item in list end of the list
myl[Link]pend(item)
forlist = ['hi','hello','bye']
while start_over < 1: #function call
for word in forlist:
print ("-=-=-=-=-=-=-=- mylist = creatList("stop")
print(word)
=-=-=-=-=-=-=-=-=-") print(mylist)
print ("Guessing Game")
create list
print ("-=-=-=-=-=-=-=- Definition in each word
# create a function that allows a
=-=-=-=-=-=-=-=-=-")
def printDefinitions(word):
user to create a list
print("words:", mylist)
if word == "variable":
#function name: word
guess = input("Guess a
print("""
#paramater: word
word: ")
'A variable is things that
#return the list
if (guess in mylist):
able to change'
def createList (quitword):
""")
mylist = [] #create an empty
if(guess ==
elif word == "function":
list
random_item ):
print("""
while True:
print("That's
"A function is to help to use
#get the item from the user
correct!")
a code"
item = input('Please enter
score = score + 100
""")
a list item')
print("Score:",
elif word == "variable":
# when the user enters an
score)
print("""
item that is equal to quitword
start_over = 2
'A variable is the things
if item == quitword:
else:
that help you to change'
return mylist
print("Sorry, wrong
""")
# check if the list already
choice! ")
elif word == "return
in the list
chances =
variable":
int(chances) -1
else:
print("Sorry, that is
not even in the list")
chances = int(chances)
-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)
print("""
'A return variable is something that
return the function back to you'
""")
elif word == "argument":
print("""
'A argument is something that give the
function to you'
""")
elif word == "parameter":
print("""
'A parameter is something that give
function'
""")
elif word == "string":
print("""
'A string is the text, number or
anything that is list the characters'
""")
else:
print("""
'unknown word'
""")
user_word = input( "Enter a word to define:
")
printDefinitions(user_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]