# Python 123 Cheat Sheet by kawkub1

### Vocabulary

 Variable Something that can be change String A list of characters Integer Number Whole number­/co­unting number Float Number The number in decimal Syntax Grammar Modulo Remainder Boolean True&­False

### Function

 print() display info. on the screen input() receives info from the user int() convert a value to an interger float() convert a value to a floating point str() convert a value to a string len() The length of the string # comment if then else if ... is True "­the­n" will work or if it False then "­els­e" will work While: True Forever Loop for Looping Forever

### Function largest number

 ```def max2(num1,num2):     if num1 > num2:         maxvalue = num1     elif num2 > num1:         maxvalue = num2     return maxvalue      #function call print(max2(92,3)) print(max2(4,5)) def max3(num1,num2,num3):     maxvalue = num1     if num2 > maxvalue:         maxvalue = num2     elif num3 > maxvalue:         maxvalue = num3     return maxvalue #function call print(max3(92,5,6)) print(max3(8,9,7)) print(max3(99,40,49)) def maxlist(list):     maxvalue = list[0]     for num in list:         if maxvalue < num:             maxvalue = num     return maxvalue mylist = 3,5476,134,8,451,87 print (maxlist(mylist))```

### Naming Convention

 Rules for giving name -letter -numbers -under­scores Valid Name - _myStr - my3 - Hello_­there Invalid Name - 3mt = "­hi" -- cannot start with number - first name = "­hi" - first-name

### Volume and Triangle

 ```def areaOfTriangle(base,height):     area=1/2 base height     return(area) user_base = float(input('Enter the base of the triangle: ')) user_height = float(input('Enter the height of the triangle: ')) #function call print('The area of the triangle is',areaOfTriangle(user_base,user_height)) def volumeOfPrism(base,height,prism_height):     volume = areaOfTriangle(base,height) * prism_height     return (volume) user_prism_height = float(input('Enter the prism height:')) print ('the volume of the prism is', volumeOfPrism(user_base, user_height, user_prism_height))```

### Palindrome

 ```def isPalindrome(word):     if reverse_item == user_word:         return "true"     else:         return "false" while True:     user_word = input('What is the word: ')     length = len(user_word)     if user_word == 'quit':         break     else:         print("Length of the word is: ",(length))              reverse = ""     for item in user_word:         reverse = item + reverse     reverse_item = reverse     s = 0     while s < length / 2 + 1 :         if reverse_item[s] == user_word[s]:             print(user_word, "is a palindrome")             break             s = s + 1         else:             print(user_word, "is not a palidrome")             break```

### Math

 = = equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to % Modulo, Find the remainder

 String + String combine together string + number CRASH number + number math

### Multip­lic­ation and Exponent

 string * number combine the string string * string CRASH number * number math string ** string CRASH number ** number Math string ** number CRASH

### Example

 Print (2) -interger Print (2.4) - Floating Point Print (myStr) - variable Print ("He­llo­") - string Print (myStr­,"Hi­"­,2,1,0) -- commas myStr = "­Hi" mystr - name "­Hi" - value can change print (int(1.5)) - 1 print (float(1)) - 1.0 print (4%2) - 0

### Randomizer

 ```""" Group member: Kim, Pop, and Earn Group: 10-03 """ import random mylist = ('lion' , 'tiger' , 'dog' , 'bee' , 'cat') print (mylist) random_item = random.choice(mylist) print (random_item) chance = 7 score = 0 while chance > 0:     user_guess = input("Guess a word: ")     if user_guess == random_item:         score = score + 100         print ("That's Correct!")         print ('score=',(score))         random_item = random.choice(mylist)              else:         if user_guess in mylist:             chance = chance - 1             print ("Sorry, wrong choice")             print ("chance =", (chance))             print ("The word is: ",random_item)             random_item = random.choice(mylist)             print ("score",(score))         else:             chance = chance - 1             print ("Sorry, that is not even in the list")             print ("chance =", (chance))             random_item = random.choice(mylist)             print ("score",(score))     if chance == 0:         print ("GAME OVER!")         print (' Answer =', (random_item))         print ("Final Score =", (score))```

### Area

 ```def areaOfCircle(r):     pi = 3.1415     area = 2pir     return area user_radius = float(input("Enter the radius: ")) print("The area of the circle is", areaOfCircle(user_radius))```

### Calcul­ating Program

 ```def calc(num1, num2, operation):     if operation == "sum":         return sum(num1, num2)     elif operation == "product":         return product(num1,num2)     elif operation == "diff":         return diff(num1,num2)     elif operation == "div":         return div(num1,num2)      def sum(a, b):     return (a + b) def product(a, b):     return (a * b) def diff(a, b):     return (a - b) def div(a, b):     return (a / b) print (calc(1,2,"sum")) print(calc (4,2, "diff")) print (calc (9, 3, "div")) print(calc (2, 12, "product"))```

### Palind­rome2

 ```''' Kim Jaroensattayatham(1003) ''' def isPalindrome(word):     index = 0     reverse = ""     while int(index) < len(word):         reverse = word[index] + reverse         index = int(index) + 1     if reverse == user_input:         return "true"     else:         return "false" while True:     user_input = input("please enter the word")     if user_input == "quit":         break     print(len(user_input))     print(isPalindrome(user_input))```

### Reverse Word

 while True: word = input(­"­enter your word") index = 0 reverse = ' ' while int(index) < len(word) reverse = word[i­ndex] + (reverse) index = int(index) + 1 print(­"­Rev­erse: ", reverse)

### Convert To BInary

 user_n­umber = ' ' while user_n­umber != ' 0 ' user_n­umber = input(­"­Enter a number to convert to binary­") number = int(us­er_­number) binary­_string = ' ' while(­number > 0) remainder = number %2 binary­_string = str(re­mai­nder) + binary­_string number = number//2 print(­"­Binary string is", binary­_st­ring)

### Countdown Machine

 user_n­umber = input(­"What number do you want to countd­own­") number = int (user_­number) countd­own­_string = ' ' while number > 0: countd­own­_number = countd­own­_string + str(nu­mber) + " " number = number - 1 #print­(nu­mber) print(­cou­ntd­own­_st­ring)

### IMPORT

 ```import random intlist = [1,2,3,4,5] random_int = random.choice(intlist) print(intlist, random_int) fplist = [1.0,1.2,1.3] random_fp = random.choice(fplist) print(fplist, random_fp) strlist = ['a','b','c','d','e'] random_str = random.choice(strlist) print(strlist, random_str) mylist = [1,1.0,'hello'] 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)```

### Asking Name Code

 firstname = input(­" what is your firstname? ") lastname = input(­" what is your lastna­me?­") fullname = firstname + " " + lastname print (fullname) letternum = int(in­put­(" what is the letter number? ")) if len(fu­llname) < letternum : print(­"­Invalid letter number, try again! ") else : print (fulln­ame­[le­tte­rnum]) letter­print = int(in­put­(" how many time to print letter ")) if letter­print > 100 : print (" too many letters to print ") else : print (fulln­ame­[le­tte­rnum] * letter­print)

### rdtfghjbkn

 ```''' theList = ('A' , 'B' , 'C' , 'D') for item in theList:     print (item) whileList = ('a' , 'b' , 'c') index = 0 while index < len(whileList):     print(whileList[index])     index = index + 1 while True:     user_input = input("Enter your word")     print (len(user_input))     if user_input == "exit":         break def theFunction():     while True:         user_input = input ("Enter the word:")         if user_input == "stop":             break #call the function theFunction() def computeThis(a1,b2):     theproduct = a1 * b2     print(theproduct)     return computeThis(1,2) ''' def finalFunction(string):     print("*",string,"*")     return finalFunction("Kim")```

## Download the Python 123 Cheat Sheet

4 Pages
//media.cheatography.com/storage/thumb/kawkub1_python-123.750.jpg

PDF (recommended)