Module 1 - Tutorial - Basic Python
Setting values to variables
The value types that we will be using in this course are mainly int, float, and string values.
An int value is an integer (whole numbers)
A float value is a decimal number
A string value is alpha-numeric characters that cannot be mathematically computed; can use single or double quotes
The pound/hashtag (#) sign denotes comments, which is not read by python as code
x = 5 #inty = 7.23 #floatz = 'hello world' #string#use the "print" function to see the output of the values stored in the variablesprint(x)print(y)print(z)#Jupyter notebooks can display the output of a variable's value by typing the variable name but only will output the last "called on" variabley #this will not displayz #this will display b/c it's the last#the "type" function will tell you what data type a variable istype(z)#variables can be added togethera = x + yprint(a)#also works for stringsr = "hello"s = "world"print(r + s)If Statement
Checks if a condition is true
Python will auto-indent starting from the first line after the colon within the If statement
Indentation signifies the code that belongs to the If statement
Note: != is "not equal to"
#check to see if something is true#can also use other tests of equality >, <, !=if x == 5: print("It is five.")else: print("It is not five.")Lists and For loops
A list is a container for multiple values
A For loop goes through each item within a list and executes the code that should be done for each item
Python will auto-indent starting from the first line after the colon within the loop
#this is a list with 3 itemscolors = ['red', 'yellow', 'blue']#iterating through a listfor color in colors: print(color)The
lenfunction returns the length (number of) items
print(len(colors))print(len(z)) #also works on counting characters (including spaces and punctuation) in a stringPython position counting (called "index") starts from 0
To "slice" a list is to look at particular specified values
The first number is the starting index of the value to get and the 2nd number after the colon is the index to stop at (not inclusive)
#this list has 7 items#position index starts at 0 and ends at 6days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']days[2:6] #2nd number is up to but not including that index numberDictionaries
Similar to lists, dictionaries are a container type to store values
Items in dictionaries are stored with a key:value pair (like a physical dictionary - you look up a word (key) and find the definition (value))
Instead of getting values by their positions, values are called by their key name
#dictionaries use curly bracketspets = {'Goku': 'dog', 'Mika': 'dog', 'Brunson': 'cat', 'Lucas': 'cat', 'Genki': 'fish'}#to see what kind of pet Mika is, look up the key "Mika" for the dictionary "pets"print(pets['Mika'])#see the entire dictionaryprint(pets)#list of keyspets.keys()#list of valuespets.values()#add new values to a dictionary#type the dictionary name with the new key in square brackets#then set equal to the value for the keypets['Tiny'] = 'dog'print(pets)#iterating over a dictionary#items functions gets key,value pair in dictionaryfor name, animal in pets.items(): print(name + " is a " + animal)The def keyword
defis used to write code to create your own function (we call this code the "function definition")Python will auto-indent starting from the first line after the colon (this is called the "function body")
The blue letters are the name of your function (no spaces are allowed in function names)
Inside the parentheses go the placeholder variables for the arguments (what will be passed into the function to compute on)
The
returnstatement sends back a value after the function is finished (there are cases where some functions do not have a return statement too)A function definition itself does not produce any output - instead, it stores the function in-memory; the function must be called separately to "activate"
# example of making a function definitiondef addNum(num1, num2): return num1 + num2# example of using (or "calling") the above functionaddNum(x, y)