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 #int
y = 7.23 #float
z = 'hello world' #string
#use the "print" function to see the output of the values stored in the variables
print(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" variable
y #this will not display
z #this will display b/c it's the last
#the "type" function will tell you what data type a variable is
type(z)
#variables can be added together
a = x + y
print(a)
#also works for strings
r = "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 items
colors = ['red', 'yellow', 'blue']
#iterating through a list
for color in colors:
print(color)
The
len
function returns the length (number of) items
print(len(colors))
print(len(z)) #also works on counting characters (including spaces and punctuation) in a string
Python 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 6
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
days[2:6] #2nd number is up to but not including that index number
Dictionaries
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 brackets
pets = {'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 dictionary
print(pets)
#list of keys
pets.keys()
#list of values
pets.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 key
pets['Tiny'] = 'dog'
print(pets)
#iterating over a dictionary
#items functions gets key,value pair in dictionary
for name, animal in pets.items():
print(name + " is a " + animal)
The def
keyword
def
is 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
return
statement 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 definition
def addNum(num1, num2):
return num1 + num2
# example of using (or "calling") the above function
addNum(x, y)