Show Menu

python cheat sheet MUIDS Cheat Sheet by


Show inform­ation that you want on the screen
change number to be number integer
change number to be decimal number
" " " .... " " "
commen­t(many lines)
a list of number, letter and symbols
commen­t(one line)
receive inform­ation from user
import random +­oic­e(list)
pick random item from the list

Random Code

import random

mylist = ['Dog','Fish', 'Cat', 'Bear']

counter = 0

while counter < 10:
      random_item = random.choice (mylist)
      print (random_item)
      counter = counter + 1

Print Code

name = "nae CHUTIMA"
print (name.upper())
print (name.lower())
print (name.capitalize())
print (name.title())

number to binary code

user_number = ""
while user_number != "0":
    user_number = input ( "enter a number" )
    number = int(user_number)

    binary_string = ""
while (number > 0 ):#the number is greater than 0
    remainder = number % 2
    binary_string = str( remainder )+ binary_string
    number = number//2
    print (number)
    print ( "binary string is ", binary_string )

Count down code

#create a program that receives a number from the user and count down from that number on the same line

#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 = ""

while number > 0:
    #add the number to the string
    #subtract 1 from the number
    countdown_string = countdown_string + str(number) + ""
    number = number-1

print (countdown_string)

#output should look like this
# if the user enter 5:
#5 4 3 2 1
#print (countdown_string)

The loop not go forever

gameover = 0

while(gameover == 0):
    print ("hello")
    gameover = 1

print number in separate line in list mylist

mylist = [1,2,3,4,5]

for number in mylist:
    print (number)

using a while loop to print each item in list

wlist = [2,4,5,6,7,8]
index = 0

while index < len(wlist):
    print (wlist[index])
    index = index +1

Definition Area

def areaOfTriangle (base,height):
    return base heigh 0.5

base = float(input('Enter the base of the triangle'))
height = float(input(input('Enter the height of the triangle: '))

print('The area of the triangle is',areaOfTriangle(base,height))

def volumeOfPrism (area,height)
    return areaOfPrism* height

base = float(input('Enter the area of the prism'))
height = float(input(input('Enter the height of the prism: '))

Import random

import random
#create list

mylist = ['nadia' , 'nat' , 'lily' , 'eye']


# select a random item from the list

counter = 0

while counter < 10:
    random_item = random.choice(mylist)
    print (random_item)
    counter = counter + 1

The loop not go forever (copy)

gameover = 0

while(gameover == 0):
    print ("hello")
    gameover = 1


string + string
combine together
string + number
number + number
math - addition

Multip­lic­ation and Exponents

string * number
combine that string
string* string
number * number
math - multiply
string ** string
number ** number
math - exponent
string ** number

Area of Circle Code

while True:
    user_radius = input("What is the radius?")
    radius = float(user_radius)
    pi = 3.1415
    area= pi radius * 2
    print ("The area of the circle is", area)


mystring = "hello"
print (mystring)
firstname = input( "what is your first name?")
lastname = input( "what is your last name?")
fullname = firstname + " " + lastname
print (fullname)

letternumber = int(input( " what is letter number? " ))
if letternumber >len(fullname):
    print ( " invalid letter number, try again! " )

    letter = ( fullname[letternumber] )
    print (letter)

    numberletter = int(input( "how many times to print letter " ))

    if numberletter >100:
       print ( " too many letters to print! " )
        print (letter * numberletter )

list code

shoppinglist = ['tshirt' , 'pants' , 'socks']

for myvariable in shoppinlist:
    print (myvariable)

print (shoppinglist[1])

for number in range(5):
    print (number)

Random code 2

import random

intlist = [1,2,3,4]
random_int = random.choice(intlist)

fplist = [1.0, 2.0, 3.0, 4.0]
random_fp = random.choice(fplist)

strlist =['book','pen','bag','pencil']
random_str = random.choice(strlist)
print (strlist,random_str)

mylist = [1, 1.0, 'beagle' ]
random_item = random.choice(mylist)

myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist =[myvar1, myvar2, myvar3]
random_var = random.choice(varlist)

print fifth character from the variable myword

myword = "hellothere"
print ( myword[4] )

convert input to a integer multiply by 10

while True:
    user_number = input("Enter the number")
    number = int(user_number)*10

convert input to a integer multiply by 10

while True:
    user_number = input("Enter the number")
    number = int(user_number)*10


while True:
    def palindrome(word):
        reverse = ""
        myresult = ""
        for letters in word:
            reverse = letters + reverse

        if word == reverse :
            return True
            return False
            reverse = ""

    word = input("please enter a word: ")
    if word == "quit":
    theresult = palindrome(word)
    print("This word has",len(word),"letter")

    if theresult == True:
       print(True,'',word + str('It is a palindrome'))
       print(False,'',word +str('It is not a palindrome'))


equal to
no equal to
less than
more than
less than or equal to
more than or equal to
modulo, Find the remainder


hold a value and can be change
a list of character such as number, letter and symbols
integer number
whole number or counting number
float number
the number in decimal
grammar or structure of lauguage
the number or string can be store in valuable
the text for storing for python code or find the remainder
gain inform­ation from user
to show inform­ation on the screen
syntax error
make impossible to the parse

Random Choice Code

import random
mylist = ['beagle','pomeranian','pug','golden','chihuahua']
score = 0
chances = 3
start_over = 0
random_item = random.choice(mylist)

while chances > 0:
    start_over = 0
    random_item = random.choice(mylist)
    while start_over < 1:
        print ("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
        print ("Guessing Game")
        print ("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
        print("words:", mylist)
        guess = input("Guess a word: ")

        if (guess in mylist):
            if(guess == random_item ):
                print("That's correct!")
                score = score + 100
                print("Score:", score)
                start_over = 2
                print("Sorry, wrong choice! ")
                chances = int(chances) -1
            print("Sorry, that is not even in the list")
            chances = int(chances) -1

        if(chances > 0):
            print("Chances remaining:",chances)
            start_over = 2
            print("Game Over! The word was ", random_item)
            print("Chance remaining:", chances)
            print("Final score:", score)

using loop to print out each item in list

forlist = ['hi','hello','bye']
for word in forlist:

create list

# create a function that allows a user to create a list

#function name: word
#paramater: word
#return the list

def createList (quitword):
    mylist = [] #create an empty list

    while True:
        #get the item from the user
        item = input('Please enter a list item')

        # when the user enters an item that is equal to quitword
        if item == quitword:
           return mylist
        # check if the list already in the list
        duplicateword = False
        # figure out if the word is already in the list

        for word in mylist:
            if item == word:
                duplicateword = True
        if duplicateword == True
            print ('Duplicate word!')
             # add this item to the end of the list

#function call
mylist = creatList("stop")

Definition in each word

def printDefinitions(word):

    if word == "variable":
     'A variable is things that able to change'

    elif word == "function":
      "A function is to help to use a code"
    elif word == "variable":
      'A variable is the things that help you to change'

    elif word == "return variable":
        'A return variable is something that return the function back to you'
    elif word == "argument":
       'A argument is something that give the function to you'
    elif word == "parameter":
       'A parameter is something that give function'
    elif word == "string":
       'A string is the text, number or anything that is list the characters'
      'unknown word'

user_word = input( "Enter a word to define: ")

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by Chutima Rakyu