Cheatography

# Python code Cheat Sheet by phon

### BasicCode

 print() write word on python print(­"­Hello There") input() let people input their inform­ation in python input(­"­insert number­") len() show the length of that word len(word) int() Number into number integer int(2.50) float() Change number into decimal float(5) str() a list of number and letter str(10) # It's comment print(­"­Hello There")#It prints Hello There

### Make a list

 shoppi­nglist = ['bags', 'shoes', 'boots', 'shirts'] print (shopp­ing­lis­t[2]) item_n­umber = 0 "­"­" 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 "­"­" out = 0 for muids in shoppi­nglist: out = out + 1 print(out) Result = boots 4

### Calculate Math

 string +\-*\/ string calculate string number +\-*\/ number calculate number number +\-\/ string CANT string +\-\/ number CANT string * number It will multiply amount of string ex: str(5) * 6 = 555555 number * string It will multiply amount of string ex: 6 * str(5X = 555555 number ** number Exponent number ** string CANT

### Math Code For python

 == equal 3 == 3 != not equal 4 != 3 < less than 3 < 4 > more than 4 > 3 <= less than or equal len(word) <= letternum >= more than or equal len(word) >= letternum % find the remainder 5%4

### isPali­ndrome

 def isPali­ndr­ome­(word): reverse = "­" for item in word: reverse = item + reverse if reverse == word: return True else: return False while True: word = input(­"­Insert Word Plz: ") length = len(word) if word == "­qui­t": break else: print (length) lengthnum = 0 while lengthnum < length / 2 + 1: if word[l­eng­thnum] != word[-­len­gth­num-1]: print ("It is not palind­rom­e") break lengthnum += 1 else: print ("It is palind­rom­e")

### find largest value

 def max2(num1, num2): if num1>num2: return (num1) if num2>num1: return (num2) def max3(num1, num2, num3): if num1 > num3 and num1 > num2: return (num1) if num2 > num1 and num2 > num3: return (num2) if num3 > num1 and num3 > num2: return (num3) num1 = input(­"­Insert num1") num2 = input(­"­Insert num2") num3 = input(­"­Insert num3") print(­"the largest number­", max3(num1, num2, num3))

### Basic Vocabulary

 Variable a value and can ne change String a list of character such as number and letter Integer Number whole number Floating Point decimal number Syntax Grammar Modulo Find remainder Boolean True/False

### Reverse Code

 word = input(­"­insert a word plz") Ex:phon reverse ="" letter_num = 0 while letter_num < len(word): reverse = word[l­ett­er_num] + reverse letter_num = letter_num + 1 print ("Re­verse: "­,re­verse) Result = nohp

### Loop List

 mylist = [1,2,3­,4,5,6] mylist2 = ['a', 'b', 'c'] mylist3 = [1, 'b', 2.5] print (mylist) print (mylist2) print (mylist3) mynumbers = range(11) for num in mynumbers: print (num) mylist­2.a­ppe­nd(­'an­other item') print (mylist2)

### Random: PYthon

 import random mylist = ['Red', 'Green', 'Blue', 'Yellow', 'Purple', 'Black'] 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(­"­Correct Guess") else: if user_guess in mylist: print ("Yes, in the list") else: print ("No, not in the list")