Cheatography

# python cheat sheet - ToeyD Cheat Sheet by ttoeysd

### Vocabulary

 Variable Hold a value and be change String A list of character such as number, letter and symbols Integer number Whole number­/co­unting number Float number The number in decimal Syntax Gramma­r/S­tru­cture of language Modulo Find the remainder Boolean True/False

### Function

 print() Show the inform­ation that you want on the screen 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

### Forever While Loop

 ``````while True: # forever     user_input = input('Enter a number: ')     number = int(user_input)     print ('The number squared is', number ** 2)``````

### Condit­ional While Loop

 ``````count = 0 # start at zero while count < 10: # loop while count is less than 10     print(count) #will print numbers 0 - 9     count = count + 1 # must increase count``````

### Lists

 ``````mylist = [2,3,4,5] # create a list #select an item from a list print (mylist[0]) #selects first item and displays 2 # len() determines the length of the list print (len(mylist)) # displays 4 mylist.append(5) # adds an item to the end of the list``````

### Multip­lic­ation and Exponents

 string * number Combine that string string* string CRASH! number * number Multiply (Math) string ** string CRASH! number ** number Exponent (Math) string ** number CRASH!

### Math

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

 string + string Combine together string + number CRASH! number + number Addition (Math)

 ``````while True:      #get a number from the user     user_number = input("Please enter a number: ")     #convert to integer     number = int(user_number)     hex_string = ''     while (number > 0):#the number is greater than 0)         remainder = number % 16 #use 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(remainder) + hex_string #remainder + hex string         number = number // 16#must use // when you divide     #after the loop print the hex string     print ("Hexadecimal string is 0x" + hex_string)     #expected output - 5 = 101     #excepted output - 3 = 11     #excepted output - 2 = 10``````

### Reverse Word

 while True: word = input(­­­"­­P­lease enter a word") index = 0 reverse = ' ' while int(index) < len(word): reverse = word[i­­­ndex] + (reverse) index = int(index) + 1 print ("Re­­­v­erse: ", reverse)

### Create function

 ``````# how to create a function def nameOfFunction (myvar1, myvar2): # parameters or argu ments     print ("hello") #must indent each line that is part of the function     return myvar1 + myvar2 # function call nameOfFunction ('hi', 'there') # a value for each argument #write a function # name : areaOfTriangle # parameters : base height # return : area def areaOfTriangle (base, height):      area = 0.5*base*height      return area``````

### Decision Making­/Co­ndi­tional Statements

 ``````if 3 < 2: #if statement must compare two Booleans     print ('3 is less than 2') elif 4 < 2: #can have 0 or more elif statements     print ('4 is less than 2') elif 5 < 2:     print ('5 is less than 2') else: #can have 0 or 1 else statement at the end     print ('none of the above are True')``````