Intro to Programming in Python¶

This notebook is an interactive tool to share/run code¶

"Programming languages allow us to formalize instructions and express logical rules, business rules, mathematics, processes, and automation instructions in one single notation." - Ryan Orsinger

Part I - Basic Buiding Blocks¶

  1. Orientation - How to run code in this notebook
  2. Printing values
  3. Using comments
  4. Variables, assignment, and re-assignment
  5. Data Types
  6. Mathematical operations in Python
  7. Comparison operators
  8. Logical operations in Python
  9. Conditionals

1. Printing with the print() function¶

In [ ]:
print("Good afternoon, ya'll!")
In [ ]:
2 + 2
In [ ]:
# assigning a value to a variable
today = "Today is a great day to learn"
print(today)
In [ ]:
print("Use the print command")
print("To print")
print("Prints each and every line")
In [ ]:
"Notebook cells automatically print the last line of code"
In [ ]:
# run this cell and the number 5 will be printed
5
In [ ]:
5 * 2
In [ ]:
"Multiple lines"
"do not print automatically"
"only the last line"
In [ ]:
"I'm writing Python with ya'll right now"
In [ ]:
print("That means")
print("The print command is important.")
print("When we want to print each line")
In [ ]:
# Exercise: Print your name in this cell. Be sure to use quotation marks around the letters.
In [ ]:
# Exercise: Print your favorite number in this cell
In [ ]:
# Exercise: Print the number 7 in this cell
In [ ]:
# Exercise: Print out 3 + 4 in this cell
# Make Python do the math rather than adding 3 + 4 in your head
In [ ]:
# Exercise: Print out 3 * 5 in this cell, * means multiply

2. Comments are how we notate our code with descriptive text¶

  • Python comments are created by the hashtag symbol.
  • The hashtag "comments out" everything to the right of the hashtag.
  • Comments exist in the code but do not execute
  • Commenting your code is an industry best practice
  • A hashtag at the beginning of a line "comments out
In [ ]:
# This is a comment. Run this cell. Do you see anything? Why or why not?
In [ ]:
# First, run this cell.
# Then delete the hashtag at the beginning of the last line.
# Last, run the cell again.
print("This is a 'commented out' print command")
In [ ]:
# In Python
# Comments are a best-practice when it comes to communicating to your future self and to fellow developers
print("Once the # at the beginning of this line is removed, this string will print.") # additional hashtags make additional comments

3. Variables point to values and give them a name¶

  • "Assigning a variable" means to bind value to a variable
  • Variables allow us to name values
  • Variables hold onto values for later.
  • Variables hold their assigned value(s) until
  • Applications use the same variable to hold different values for different users
In [ ]:
# Variables hold values that we use later in the program by name.
# "Assigning a variable"
# Single = sign is called the assignment operator
message = "Hello, Everybody!"
print(message)
In [ ]:
favorite_quote = "If you can't solve a problem, then there is an easier problem you can solve:  find it."
print(favorite_quote)
In [ ]:
# Variables are assigned to point to values.
favorite_food = "lasagna"
print("My favorite food is", favorite_food)
In [ ]:
# Variables can also be re-assigned to point to different values.
favorite_food = "pizza" 
print("I changed my mind. My favorite food is actually", favorite_food)
In [ ]:
# Exercise: Reassign the favorite_food variable to hold your own favorite food, then print the variable.
In [ ]:
# Exercise: Create and assign the variable favorite_number to hold your own favorite number.
In [ ]:
# Exercise: Reassign the favorite_food variable to hold the number 7. 
# What happens and why? Does this match your expectations?

4. Data Types¶

Each piece of data has two fundamental characteristics:

  • The value of the data itself.
  • Every value has a data type which explains what kind of data the value is.
  • For example, the integer 2 has the value of 2 and the data type is integer.
  • The first data-types to learn:
    • None
    • Boolean
    • Integer
    • Float
    • String
    • List
    • Function
    • Dictionary
In [ ]:
# The "None" data type represents the absence of a value. This is called "null" in some other programming languages
type(None)
In [ ]:
type(True)
In [ ]:
type(False)
In [ ]:
# Numbers without a decimal are integers. Integers can be negative or positive.
type(-99)
In [ ]:
type(2.3)
In [ ]:
.5 * .5
In [ ]:
type("Howdy!")
In [ ]:
# True and False are the only two Boolean values.
True
False

print(True)
print(False)

5. Mathematical Operations in Python¶

  • Computers are not intelligent. They do 2nd grade math as fast as possible.
  • Programming languages need to have the ability to perform mathematical operations.
  • Mathematical operators in Python evaluate and provide
  • Order of operations in Python follows PEMDAS:
    • Parentheses
    • Exponents
    • Multiplication
    • Division
    • Addition
    • Subtraction

Operators operate on values and produce values¶

Let's look at some math operators in Python¶

  • + adds two numbers together
  • - subtracts the second number from the first
  • * multiplies both numbers and returns the product
  • / divides the first number by the second
  • % provides the remainder of dividing the first number by the second. (called the modulo operator)
  • ** is the exponentiation operator. 2 raised to the 3rd power is 2**3
In [ ]:
# Exercise: Add 1 plus 2 plus 3 plus 4 plus 5 plus 6 plus 7 plus 8 plus 9 plus 10
In [ ]:
# Exercise: Subtract 23 from 55
In [ ]:
# Exercise: Multiply 2.3 by 4.4. What's the data-type that this returns?
In [ ]:
# Exercise: Multiply 3 by 5.
In [ ]:
# Exercise: Print out the data-type of the result of multiplying 11 * 7
In [ ]:
# Exercise: Divide 1 by 2. What happens? What data type is returned?
In [ ]:
# Exercise: Divide 5 by 0. What happens? Why do you think that is?
In [ ]:
# Exercise: Use the modulo operator to obtain the remainder of dividing 5 by 2
5 % 2
In [ ]:
# Exercise: Use the modulo operator to obtain the remainder of dividing 8 by 2
In [ ]:
# Exercise: Use the modulo operator to obtain the remainder of dividing 7 by 2
In [ ]:
# Exercise: Use the exponent operator to raise 2 to the 3rd power
In [ ]:
# Exercise: Use the exponent operator to raise 10 to the 10th power
In [ ]:
# Exercise: Use the exponent operator to raise 100 to the 100th power
In [ ]:
# Exercise: Use the exponent operator to raise 2 to the negative 1st power
In [ ]:
# Exercise: Use the exponent operator to raise 123 to the 0th power
In [ ]:
# Exercise: Use what you have learned to determine the average of the following variables

6. Comparison Operators¶

Python Comparison Operators compare two values and return a True or False¶

  • == compares two values to see if they are the same value.
  • != asks if the values on each side of the operator are not the same.]
  • < and <= are less than and less than or equal to
  • > and >= are greater than and greater than or equal to, respectively.

Let's look at some concrete examples of using comparison operators and values that return a True or a False.¶

  • 2 < 3 returns True because it's a true statement that two is less than three
  • 2 <= 2 returns True because two is less than or equal to two
  • 2 < 2 returns False because two is not less than two
  • 4 <= 2 returns False because four is not less than or equal to two
  • 10 < 5 returns False because 10 is not less than 5
  • 10 > 5 returns True because ten is greater than 5
  • 2 == 2 returns True because the number two is equal to the number two
  • 2 == 3 returns False because two is not three
  • 3 != 2 returns True because three is not equal to two.
  • 4 in [1, 2, 3, 4] returns True because 4 exists in the list [1, 2, 3, 4]
  • "b" in "banana" returns True because the string "b" exists inside the string "banana"

If we have x = 3 followed by result = x > 0 on the next line, the result variable will hold True because 3 is greater than 0.

Comparison Operators ask the following questions¶

  • Are these two things equal?
  • Are these two things not equal?
  • Is A greather than B?
  • Is A greater than or equal to B?
  • Is A less than B?
  • Is A less than or equal to B?
  • Does A exist in a list or collection of values named B
In [ ]:
print("5 > 4 is", 5 > 4)
In [ ]:
print("1 > 0 is", 1 > 0)
In [ ]:
# Comparison operators give us back a True or False

print(5 == 5)

print(-4 + 4 == 0)
In [ ]:
# False because they're not the same data type
5 == "5"
In [ ]:
print("Hello" == "Goodbye")
print("Hello" != "Goodbye") # != means "not equal to" 
# = assignment# False because they're not the same data type
5 == "5"
In [ ]:
# Exercise: assign the variable favorite_number to hold your favorite number
In [ ]:
# Exercise: Write the Python code necessary to answer if your favorite number is less than 100
In [ ]:
# Exercise: Write the code to determine if your favorite number is greater than 0
In [ ]:
# Exercise: Write the code to determine if your favorite number is five
In [ ]:
# Exercise: Write the code to determine if your favorite number is greater than or equal to 3
In [ ]:
# Exercise: Write the code to determine if your favorite number is greater than or equal to -100
In [ ]:
# Before running this code, consider if this will return True or False, given your favorite_number variable's value


favorite_number < 0 and favorite_number > 10
In [ ]:
# Before running this code, consider if this will return True or False, given your favorite_number variable's value

favorite_number < 10 or favorite_number > 20

7. Logical Operators¶

Evaluating what's True in Python¶

  • Most common logical operations are:
    • And
    • Or
    • Not
  • For a reminder, see Conjunction Junction from School House Rock
In [ ]:
not True
In [ ]:
not False
In [ ]:
not not True
In [ ]:
not note False
In [ ]:
# Truth table for AND
print("Truth Table for AND logic")
print("True and True is:", True and True)
print("True and False is:", True and False)
print("False and True is:", False and True)
print("False and False is:", False and False)
In [ ]:
# Truth table for OR
print("Truth Table for OR logic")
print("True or True is:", True or True)
print("True or False is:", True or False)
print("False or True is:", False or True)
print("False or False is:", False or False)
In [ ]:
# Boolean values assigned to variables 
right_here_now = True
learning_python = True
on_the_moon = False

# Ands need all values to be true for the entire expression to be True
print(right_here_now and on_the_moon)


# Read the line of code below and think about the result. 
# An OR statement only needs a single True for the entire expression to be True
print(learning_python or on_the_moon)

8. Conditional Logic with Conditionals¶

  • Using if
  • Using if, else
  • Using if, elseif, else

The "if" statement is how we write "If this then that"¶

  • If the code evalutates to True then Python runs the code that's indented after the "if" line
In [ ]:
# the "if" evaluates the code to the right to see if it returns True or False.
# If the code returns `True`, then we run the code that's indented below the if.

right_here_now = True

if right_here_now:
    print("So glad you made it today!")
    print("Wherever you go, there you are.")
else:
    print("You are not right here now.")
    
print("Unindented means the if/else is over, and we're back to our regularly scheduled programming.")
In [ ]:
# Before running this cell, think about and predict the outcome.
# the "if" evaluates the code to the right to see if it returns True or False.
# If true, then we run the code that's indented below the if.

on_the_moon = False

if on_the_moon:
    print("You are currently on a lunar base.")
else:
    print("You are not on the moon!")
    
In [ ]:
# Before running this cell, think about and predict the outcome.

if right_here_now and learning_python:
    print("We are right here now and learning python.")
    
In [ ]:
# Before running this cell, think about and predict the outcome.
if right_here_now and learning_python and on_the_moon:
    print("You must be magic since you're on the moon and here and learning Python all at the same time.")
    
In [ ]:
# Before running this cell, think about and predict the outcome.
if right_here_now or on_the_moon:
    print("How many True values does it take with an OR for the entirety to be True?")
 
In [ ]:
   
# Before running this cell, think about and predict the outcome.
# Carefully consider the parentheses...
if learning_python and (right_here_now or on_the_moon):
    print("In addition to learning python, you are either right here now or on the moon")
In [ ]:
# Exercise: Create a variable called is_raining and assign it True or False depending on if it's raining right now
In [ ]:
# Exercise: Create a variable named will_rain_later and assign a value of True or False if it may rain later today
In [ ]:
# Exercise: Create an "if" condition that checks if it's raining now or later. 
# If it is going to rain now or later, then print out "I'll bring an umbrella"

Part II - Lists, Dictionaries, and Functions¶

  • With programming and analysis, we work with lots of lists of numbers.
  • The primary data type in Python for holding lists of numbers is called a list. It's like an array in other programming languages.

Lists are ordered lists of values.¶

  • Created with square brackets
  • Lists can hold any value(s)
  • Lists can hold lists.
  • Think of a list as a column of values in a spreadsheet (1 dimensional list)
  • Think of a list of lists as an the entire spreadsheet (2 dimensional list)
  • We can have lists of lists of lists, and that's like having a workbook of spreadsheets (3 dimensions)
  • There are other types of sequences and collections in Python, but lists are a solid start!
In [ ]:
# Lists are created with square brackets. Each item on the list is separated by a comma. Lists can hold any data type
beatles = ["John", "Paul", "George"]
print(beatles)
In [ ]:
# Lists have built-in functions
beatles.append("Ringo") # Don't forget Ringo
In [ ]:
beatles
In [ ]:
# .append and .reverse are some of the built-in list functions
beatles.reverse()
print(beatles)
In [ ]:
beatles
In [ ]:
# We can also use lists to hold numbers
odds = [1, 3, 5, 7, 9, 11, 13, 17, 19, 21]
odds
In [ ]:
# for variable in list:
# For each number in the list of odds, do something
# thing we do each time through the loop IS the code indented within that loop
# for new_variable in list_variable
for number in odds:
    print(number)
    print("The loop is going")
print("The Loop is complete.")
In [ ]:
for number in odds:
    print(number + 10)

Functions¶

There are 2 kinds of functions in Python: built-in and user defined functions.

Some built in functions:

  • type, print, len, sum, abs, range, list, min, max, round, etc...
  • For a complete list of build in functions, see https://docs.python.org/3/library/functions.html
In [ ]:
# sum is a built in function
numbers = [2, 3, 5, 7]
sum(numbers)
In [ ]:
# len is a built-in function short for length
len(numbers)
In [ ]:
# calculate the average of a list of numbers
sum(numbers) / len(numbers)
In [ ]:
# The len function also works on strings
programming_language = "Python"
print("The number of letters in Python is", len(programming_language))

Let's Create Some Example Functions¶

Imagine the exercise asks us to write a function called is_five that takes in a variable and returns True/False if that variable is the number five. The function would look like this:

def is_five(x):
    result = x == 5
    return result

Or we could write this a little more succinctly (since returning the result is the same as returning the expression assigned to the result variable)

def is_five(x):
    return x == 5

The result of this function is entirely dependent on the input values we send into the function. For example:

  • is_five(10) will return False
  • is_five(-5) returns False because -5 is not 5.
  • is_five("hello") returns False because the string "hello" certainly isn't the number 5.
  • is_five(5) returns True because 5 == 5 evaluates to True.

Notice how this function is returning the result of a comparison operation and that the comparison operator will give us a True or a False, depending on the value contained in the input variable sent into the function.

If the exercise was to make a function named is_more_than_three, we could define the function in the following way:

def is_more_than_three(x):
    return x > 3

Notice that we're returning the result of x > 3, which can change with different inputs. For example:

  • is_more_than_three(10) returns True
  • is_more_than_three(30) returns True
  • is_more_than_three(3) returns False (because 3 is not greater than 3)
  • is_more_than_three(3.025) returns True because 3.023 > 3 returns True.
  • is_more_than_three(-23) returns False b/c negative 23 is less than 3.
In [ ]:
# Exercise: Use the examples above to write a function called is_greater_than_1 that takes in an input and returns True/False if that number is greater than 1
In [ ]:
# Exercise: Use the examples above to write a function called is_less_than_100 that takes in an input and returns True/False if that number is less than 100
In [ ]:
# how to define our own functions
# def function_name(variable_name_for_the_input):
#      output = guts of the function
#      return output
def is_even(number):
    remainder = number % 2 # the % operator returns the remainder of integer division of the number on the left by the number on the right
    if remainder == 0: # even numbers have no remainder
        return True
    else:
        return False

# calling a function means to run it
print(is_even(2))
print(is_even(4))
print(is_even(2.3))
print(is_even(3))
In [ ]:
# lots of built in functions work on sequences
names = ["John", "Paul", "George", "Ringo"]

print("The number of items on the list is", len(names))
In [ ]:
numbers = [2, 3, 5, 7]
print("The highest number on the list is", max(numbers))
print("The smallest number on the list is", min(numbers))
In [ ]:
# def keyword to define, name_of_function, parentheses for parameters
def square(number):
    return number * number # return is the output we hand back to the code that called the function (calling == execute == run)
In [ ]:
square(3)
In [ ]:
square(4)
In [ ]:
# How to average a list of numbers.
numbers = [2, 3, 5, 7]
total = sum(numbers)
average = total / len(numbers)
average
In [ ]:
def average(numbers):
    total = sum(numbers)
    average = total / len(numbers)
    return average
In [ ]:
print(average([1, 2, 3]))
print(average([1, 2]))
In [ ]:
# Max returns the longest string
print(max(["a", "aa", "aaa"]))
print(min(["a", "aa", "aaa"]))

Introducing Dictionaries¶

  • Dictionaries are like "labeled lists"
  • Lists hold values in order and have a numeric index.
  • Dictionaries hold values and call them by a string index.
  • Dictionaries in the real world are like menus or nutritional facts tables
In [14]:
workshop = {
    "name": "Introduction to Python",
    "instructor": "Ryan",
    "platform": "Kaggle"
}
In [7]:
# We use the variable that holds the dictionary followed by square brackets (like a list), with the string holding the key name.
workshop["name"]
Out[7]:
'Introduction to Python'
In [6]:
workshop["platform"]
Out[6]:
'Kaggle'
In [13]:
# To add new values to a dictionary, use a new key name
workshop["programming_language"] = "Python"
workshop["language_version"] = 3.6 

workshop
Out[13]:
{'name': 'Introduction to Python',
 'instructor': 'Ryan',
 'platform': 'Kaggle',
 'favorite_programming_language': 'Python',
 'programming_language': 'Python',
 'language_version': 3.6}
In [ ]:
# Exercise
# Add a new key=>value pair to the "workshop" dictionary variable.
# The key should be called "my_name" and the value should be a string holding your name
In [ ]:
# Exercise
# Make a new variable called preferences.
# Set up a key for "favorite_number" and assign a value
# Setup a key for "favorite_color" and assign a string with your favorite number