Show Menu
Cheatography

python cheat sheet MUIDS Cheat Sheet by

Functions

print()
Show inform­ation that you want on the screen
int()
change number to be number integer
float()
change number to be decimal number
" " " .... " " "
commen­t(many lines)
str()
a list of number, letter and symbols
#
commen­t(one line)
input()
receive inform­ation from user
import random + random.ch­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']

#print(mylist[0])

# 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
 

Addition

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

Multip­lic­ation and Exponents

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

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)

code

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! " )
else:

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

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

    if numberletter >100:
       print ( " too many letters to print! " )
    else:
        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)
print(intlist,random_int)

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

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

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

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

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
    print(number)

convert input to a integer multiply by 10

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

palindrome

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

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

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

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

Math

==
equal to
!=
no equal to
<
less than
>
more than
<=
less than or equal to
>=
more than or equal to
%
modulo, Find the remainder

VOCABULARY

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

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
            else:
                print("Sorry, wrong choice! ")
                chances = int(chances) -1           
        else:
            print("Sorry, that is not even in the list")
            chances = int(chances) -1

        if(chances > 0):
            print("Chances remaining:",chances)
        else:
            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:
    print(word)

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!')
        else:
             # add this item to the end of the list
             mylist.append(item)

#function call
mylist = creatList("stop")
print(mylist)

Definition in each word

def printDefinitions(word):

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

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

    elif word == "return variable":
        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)
 

Comments

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