Cheatography

# Python cheat sheet Kim Cheat Sheet by Kim_m

### Vocabulary

 Variable Hold a value and can be change String A list of character such as number, letter andsymbols Integer Number Whole number­/co­unting number Float Number The number in decimal Syntax Grammar /structure Modulo Find the remainder Boolean true/False

 string­+string Combine together string­+number CRASH! Number­+number Additi­on(­Math)

### Reverse

 #Finish this program so that it gets a word from the user and prints #that word backwards reverse = "­" #do not change letter_num = 0 #do not change word = input(­"­Please enter a word: "­)#get a word from the user ''' while letter_num < len(wo­rd)­:#c­ompare the letter_num to the lenght of the word reverse = word[l­ett­er_­num­]+r­eve­rse­#kepp adding the letter to the front of reverse letter_num = letter­_nu­m+1#go to the next letter in the word ''' for lette in word : reverse = letter + revers print ("Re­verse: "­,re­verse) #creating list mylist = [1,2,3­,4,5,6] mylist2 = ['hi', 'hello­','­any­thing'] mylist3 = [1, 'hello', 2.5]

 while True: #Ask the user for a radius of a circle user_r­adius = input(­"­Please enter the radius of the circle­") #Convert the given radiusto a floating point radius = float(­use­r_r­adius) #make a variable called pi pi = 3.1415 #Calculate the area of the circle using exponents area = pi radius *2 #display the area of the circle to the user print(­"The area of the circle is", area)

### A multiple string

 # write defini­tions for the following words and print them using # a multi-line string def printD­efi­nit­ion­s(w­ord): # parameter word if word == "­var­iab­le": #variale print "­"­" A variable is ... "­"­" elif word == "­fun­cti­on": # function print (""" A function is ... "­"­") elif word == "­par­ame­ter­": print(­"­"­" A parameter is ... "­"­") elif word == "­arg­ume­nt": print(­"­"­" A argument is ... "­"­") elif word == "­str­ing­": print(­"­"­" A srting is ... "­"­") elif word == "­fun­ction call": print(­"­"­" A function call is .... "­"­")+ # parameter # argument # string # function call else: return "­unknown word" #ask the user for the name of the word define user_input = input (" printD­efi­nit­ions( user_input )

### How to make list in python

 #how to make list in python shoppi­nglist = ['bag', 'shoes', 'boots', 'shiryt'] print(­sho­ppi­ngl­ist) item_n­umber = 0 #while loop while item_n­umber < len(sh­opp­ing­list): print ("List item:",­sho­ppi­ngl­ist­[it­em_­num­ber]) item_n­umber = item_n­umber + 1 #for loop out = 0 for muids in shoppi­nglist: out = out + 1 #print­("list item:", muids) print (out)

### Palindrome

 def isPali­ndr­ome­(word): index = 0 reverse = '' for letter in word: reverse = letter + reverse if reverse == word: return True elif word != reverse: return False while True: user_input = input(­"­Please enter a word: ") if user_input == ("qu­it"): break print (len(u­ser­_in­put)) check = (isPal­ind­rom­e(u­ser­_in­put)) if check == True: print(­use­r_i­npu­t,"is a palind­rom­e") elif check == False: print (user_­inp­ut,­"is not a palind­rom­e")

### Function

 print() Show inform­ation that you want toscreen int() Change number to be number integer float() Change number to be decimal number input() Gain inform­ation from user str() A list of number­,letter and symbols len() The length of the string # Comment, no effect

### Multip­lic­ation and Exponent

 string­*number Combine the string string­*string CRASH! number­*number Multip­ly(­math) string­**s­tring CRASH! number­**n­umber Expone­nt(­math) string­**n­umber CRASH!

### Random

 import random # Create a list of integers inlist = [1,2,4­,5,7,9] random_int = random.ch­oic­e(i­ntlist) print (inlist, random­_int) #print the entire list andthe random item # Create a list of floating point numbers fplist = [1.5,2.2,­1.0­,10­0.999] random_fp = random.ch­oic­e(f­plist) print (fplist, random_fp) #print the entire list and the random item # Create a list of strings strlist = ['dog', "­cat­", 'match', "it's me", '"hi­"'] random_str = random.ch­oic­e(s­trlist) print (strlist, random­_str) #print the entire list and the random item # Create a list of integers and floating point numbers and string mylist = [1,2,2.2,3.2, 'string', "­hi"] random­_item = random.ch­oic­e(m­ylist) print (mylist, random­_item) #print the entire list and the random item # create alist of following variable myvar1 = 1 myvae2 = 2 myvar3 = 3 varlist = [myvar1, myvar2, myvar3] random_var = random.ch­oic­e(v­arlist) print (varlist, random­_var) #print the entire list and the random item

### Countdown

 # Create a program that receives a number from the user and counts down # from that number on the same line # receive the number from the user as a string user_n­umber = input(­"­7") #convert the user number to an integer number = int(us­er_­number) #setup the countdown string countd­own­_string = '7 6 5 4 3 2 1 0' while number > 0: # add the number to the string countd­owm­_string = something + str(so­met­hin­gelse) # subtract 1 from the number number = number - 1 print (count­dow­n_s­tring)

### How to create function

 # how to create a function def nameOf­Fun­cti­on(­myvar1, myvar2): #param­eters or argument #write a function # name : areaOf­Tri­angle # parameters : base height # return: area user_base = float(­inp­ut(­'Enter the base of the triangle: ')) user_h­eight = float(­inp­ut(­'Enter the height ofthe triangle: ')) print ('the area of the triangle is', areaOf­Tri­ang­le(­use­r_base, user_h­ight)) # name: volume­OfPrism # parame­ters: area height # return: volume def volume­OfPrism user_p­ris­m_h­eight = float(­inp­ut9­'Enter the height of prism: ')) print('the volume of the prism is', volume­OfP­ris­m(a­rea­OfT­ria­ngl­e(u­ser­_base, user_h­eight), user_p­ris­m_h­eight))

### Operation

 def calc(num1, num2, operat­ion): #user if/eli­f/else to check what operation if operation == "­sum­": return sum(num1, num2) elif operation == "­div­": return div(num1, num2) elif operation == "­pro­duc­t": return product (num1, num2) else: print ("un­known operat­ion­") def sum(a, b): #calculate the sum of a and b return a+b #return the answer def product(a, b): # calculate the productof a and b return a * b #return the answer def diff(a, b): # calculate the difference between a and b return a -b # return the answer def div(a, b): # calculate the division of a and b return a / b # return the answer3 print(calc ( 10, -2, "­div­")) print(­cal­c(1­,2,­"­sum­")) #output should be 3 print(calc (4, 2, "­dif­f")) # output should be 2 calc (9, 3, "­div­" )) #output should be 3 calc (2, 12, "­pro­duc­t")) #output shouldbe 24

### Math

 == equal to != no equal to < less than > more than <= less than or equal to >= more than or equal to % Modulo, find the remainder

### Convert Binary

 #write a program that convert a number to binary while True: #get a number from the user user_n­umber = input(­"­please enter the number­") #convert to integer number = int(us­er_­number) binary­_string = '' while (number > 0):#the number is greater than 0) remainder = number % 2#user Modulo % binary­_string = str(re­mai­nder) + binary­_string #remainder + binary string number = number // 2#must use // when you divide #after the loop print the binary string print ("Binary string is",­bin­ary­_st­ring) #expected output - 5 = 101 #expected output - 3 = 11 #expected output - 2 = 10

 #write a program that convert a number to binary while True: #get a number from the user user_n­umber = input(­"­please enter the number­") #convert to integer number = int(us­er_­number) hex_string = '' while (number > 0):#the number is greater than 0) remainder = number % 16#user Modulo % if remainder == 10: remainder = 'A' elif remainder == 11: remainder = 'B' elif remainder == 12: remainder = 'C' elif remainder == 13: remainder = 'D' elif remainder == 14: remainder = 'E' elif remainder == 15: remainder = 'F' hex_string = str(re­mai­nder) + hex_string #remainder + hexade­cimal string number = number // 16#must use // when you divide #after the loop print the Hexade­cimal string print ("He­xad­ecimal string is 0x" + hex_st­ring) #expected output - 5 = 101 #expected output - 3 = 11 #expected output - 2 = 10

### Return Max number

 def max2(num1, num2): if num1 > num2: return num1 else: return num2 def max3(num1, num2, num3): if num1 > num2 and num1 > num3: return num1 elif num2 > num1 and num2 > num3: return num2 else: return num3 print (max2(10, 15)) print (max2(20, 10)) print (max3(1, 2, 3)) print (max3(15, 20, 10)) print (max3(99, 15, 47))

### My list

 mylist = ['lion' , 'tiger', 'cheetah', 'cougar' ,'lynx'] print (mylis­t) print (mylist) user_guess = input(­"­Guess a word: ") random­_item = random.ch­oic­e(m­ylist) print (rando­m_item) if user_guess == random­_item: print ("Co­rrect guess") else: if user_guess in mylist: print ("yes, in the list") else: print ("No,not in the list")

### Multiple Parameter

 _var1 = 1 _var1 = 3 _var1 + 100 print(­_var1) def bacon () : print(­"­hello it'sba­con­") print(­"line 2") print(­"line 3") print(­"line 4") print(­"line 5") print(­"line 6") print(­"line 7") print(­"line 8") return def myprin­t(t­ext): #Single parameter print(­"" + str(text) + "") return myprint(1) myprin­t("h­ell­o") myprin­t(1+2) def myprin­t2(­text, decora­tion): #multiple parameters print (decor­ation + str(text) + decora­tion) return myprin­t2(­123­123­21312, "­+++­") myprin­t2(­"­hel­lo",­"­<<>­>") def double­It(­num­ber): return number * 2 myvar = 2 myvarD­ouble = double­It(­myvar) print(­myv­arD­ouble) print(­dou­ble­It(­"­hel­lo")) myvar = double­It(­dou­ble­It(3)) # same as double­It(6) print(­myvar) def sumIt(­num1, num2): return num1+num2 print(­sum­It(­"­a", "­b")) print (sumIt­(2,3)) def areaOf­Circle (r): pi = 3.1415 area = pi r * 2 return user_R­adius = input(­'Enter the radius:') radius = float(­use­r_r­adius) print(­"the area of the circle is", areaOf­Cir­cle­(ra­dius))