| Vocabulary
                        
                                                                                    
                                                                                            | variable | holds a value and can be changed |  
                                                                                            | string | a list of characters such as numbers, letters, symbols |  
                                                                                            | integer | whole number/counting number. 100, -52 |  
                                                                                            | float | the number in decimal. 100.01, -52.5 |  
                                                                                            | boolean | true/false |  
                                                                                            | modulo | find the remainder |  
                                                                                            | syntax | grammar/structure of language |  Naming Conventions
                        
                                    
                        | Rules for naming variables:- letters
 - numbers
 - underscores (_)
 - can start with letters or underscores ONLY
 - NO SPACES
 
 Valid names:
 - _mystr
 - my3
 Hello_there
 
 Invalid names:
 - 3my= "hi" -- cannot start with number
 - first name = "hi" -- no spaces allowed
 - first-name -- dashes are not accepted
 |  Addition
                        
                                                                                    
                                                                                            | string + string | combine together |  
                                                                                            | string + number | CRASH! |  
                                                                                            | number + number | addition (math) |  Multiplication & Exponents
                        
                                                                                    
                                                                                            | string * string | CRASH!! |  
                                                                                            | string * number | combines the strings multiple time |  
                                                                                            | number * number | math (multiply) |  
                                                                                            | string ** number | CRASH!! |  
                                                                                            | number ** number | exponent (math) |  
                                                                                            | string ** number | CRASH!! |  Random Stuff
                        
                                    
                        | import random
intlist = [1,2,3,4,5]
random_int = random.choice(intlist)
print (intlist,random_int)
fplist = [3.5,4.02,5.55,9.65,7.02]
random_fp = random.choice(fplist)
print (fplist,random_fp)
strlist = ['dog','cat','monkey','elephant','squirrel']
random_list = random.choice(strlist)
print (strlist,random_list)
mylist = [1,3.2,'snow']
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)
 |  Largest number in the list
                        
                                    
                        | def maxlist(list):
    maxvalue = list[0]
    for item in list:
        if item > maxvalue:
            maxvalue = item
    return maxvalue
mylist = [1,2,3,4,55,66,777,0,1]
print(maxlist(mylist))
 |  Largest of two numbers
                        
                                    
                        | def max2(num1, num2):
    maxvalue = num1
    if num2 > maxvalue:
        maxvalue = num2
    return maxvalue
print(max2(4,5))
print(max2(33,5))
 |  |  | Function
                        
                                                                                    
                                                                                            | print() | displays information on the screen |  
                                                                                            | input() | receives information from the user |  
                                                                                            | int() | converts a value to an integer |  
                                                                                            | float() | change number to be decimal number |  
                                                                                            | str() | a list of number, letter and symbols |  
                                                                                            | len() | the length of the string |  
                                                                                            | # | comment, no effect |  
                                                                                            | """.................""" | multiple line comment |  Symbols
                        
                                                                                    
                                                                                            | = = | equal to |  
                                                                                            | != | no equal to |  
                                                                                            | < | less than |  
                                                                                            | > | more than |  
                                                                                            | <= | less than or equal to |  
                                                                                            | >= | more than or equal to |  
                                                                                            | + | add |  
                                                                                            | - | subtract |  
                                                                                            | * | multiply |  
                                                                                            | / | divide and quotient is float |  
                                                                                            | // | divide and quotient is integer |  
                                                                                            | ** | exponent |  
                                                                                            | % | modulo: the remainder |  Sort word per line
                        
                                    
                        | mystr = "Hello"
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
 |  Convert to binary
                        
                                    
                        | user_number = ' '
while True :
    binary_string = ' '
    user_number = input ("Enter a number to convert to binary")
    number = int (user_number)
    while (number > 0):
        remainder = number%2
        binary_string = str(remainder)+ binary_string
        number = number//2
    print ("Binary string is", binary_string)
 |  Print Name
                        
                                    
                        | name = "tim GIRARD"
print (name.upper()) → TIM GIRARD
print (name.lower()) → tim girard
print (name.capitalize()) → Tim girard
print (name.title()) → Tim Girard
 |  Area of circle code
                        
                                    
                        | def areaOfCircle (r):
    if r <= 0:
        return "Error: invalid radius"
    
    pi = 3.1415
    area = pi  r * 2
    return area
user_radius = float(input('Enter the radius:'))
print("The area of the circle is", areaOfCircle(user_radius))
 |  Largest of three values
                        
                                    
                        | def max3(num1, num2, num3):
    maxvalue = num1
    if (num1 > num2) and (num1 > num3):
        maxvalue = num1
    elif (num2 > num1) and (num2 > num3):
        maxvalue = num2
    else: maxvalue = num3
    return maxvalue
print (max3(1,2,50))
print (max3(51,2,50))
print (max3(1,255,50))
 |  |  | Using Boolean
                        
                                    
                        | print(True)
print (2<3)
print (2 != 2)
 |  Number to Hex
                        
                                    
                        | user_number = input("please enter a number: ")
number = int(user_number)
hex_string = ' '
while (number > 0):
    remaider = number % 16
    if remaider == 10:
        remaider = 'A'
    elif remaider == 11:
            remaider = 'B'
    elif remaider == 12:
            remaider = 'C'
    elif remaider == 13:
            remaider = 'D'
    elif remaider == 14:
            remaider = 'E'
    elif remaider == 15:
            remaider = 'F'
                            
    hex_string = str(remaider) + str(hex_string)
    number = number // 16
print ("Hexadecimal string is 0x", hex_string)
 |  Area of Circle
                        
                                    
                        | """
Python Intro Assignment #2
name
student number
"""
#Ask the user for a radius of a circle
user_radius = input("What is a radius of a circle?")
#Convert the given radius to a floating point
radius = float(user_radius)
#Make a variable called pi
pi = float(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)
 |  Sort fruit list
                        
                                    
                        | fruits = [] #an empty list
for number in range(5):
    user_fruit= input("Please enter a fruit")
    fruits.append(user_fruit)
print ("Size of fruit list is", len(fruits))
fruits.sort()
for fruit in fruits:
    print ("Fruit: ", fruit)
for fruit in fruits:
print ("Fruit: ", fruit)
 |  Bracket codes
                        
                                    
                        | def myprint(text):
    print ("" + str(text) + "")
    return
myprint("1)
myprint("hi")
myprint("2.5")
def myprintnew(text, decoration):
    print(decoration + str(text) + decoration)
    return
myprintnew(1, "+++")
myprintnew('hello','||||||||||||||||')
myprintnew(1, "-------")
 |  Print Definition (Loop)
                        
                                    
                        | 
def printDefinitions(word):
    if word == "variable":
        print ("""
        A variable is the vaule that can changes
        """)
    elif word == "function":
        print ("""
        A function is a block of code that can reuse
        """)
    elif word == "string":
        print ("""
        A String is a list of characters
        """)
    elif word == "parameter":
        print ("""
        A Parameter is something you give to the function
        """)
    elif word == "argument":
        print ("""
        An Argument is thing that give to function
        """)
    elif word == "function call":
        print ("""
        
        A Function call is to tell the function/code to run
        """) 
    else: 
        print("Unknown word")
while True:
    user_input = input("Enter words: ")
    printDefinitions(user_input)
 |  | 
            
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets