Module 1 - Tutorial - Basic Python

Back to the course outline

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
0.0s
#use the "print" function to see the output of the values stored in the variables
print(x)
print(y)
print(z)
0.3s
#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
0.1s
#the "type" function will tell you what data type a variable is
type(z)
0.0s
#variables can be added together
a = x + y
print(a)
#also works for strings
r = "hello"
s = "world"
print(r + s)
0.3s

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.")
0.2s

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']
0.0s
#iterating through a list
for color in colors:
    print(color)
0.3s
  • 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
0.3s
  • 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
0.0s

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'}
0.0s
#to see what kind of pet Mika is, look up the key "Mika" for the dictionary "pets"
print(pets['Mika'])
0.3s
#see the entire dictionary
print(pets)
0.3s
#list of keys
pets.keys()
0.0s
#list of values
pets.values()
0.0s
#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)
0.3s
#iterating over a dictionary
#items functions gets key,value pair in dictionary
for name, animal in pets.items(): 
    print(name + " is a " + animal)
0.2s

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
0.0s
# example of using (or "calling") the above function
addNum(x, y)
0.0s

Back to the course outline

Appendix

This repo is mounted by: Python
Runtimes (1)