QTM 151 - Introduction to Statistical Computing II

Lecture 05 - Boolean Logic and Conditional Statements

Danilo Freire

Emory University

17 September, 2024

Nice to see you all again! 😊

Brief recap of last class 📚

Brief recap

In the last class, we did the following:

  • Introduced Matplotlib and NumPy libraries and loaded them with import matplotlib.pyplot as plt and import numpy as np
  • Created histograms and scatter plots with plt.hist() and plt.scatter()
  • Explored NumPy mathematical functions: np.log(), np.exp(), np.sin(), np.sqrt()
  • Created NumPy arrays and performed operations
    • Example: np.array([1,2,3]), array1 + array2
  • Accessed array elements using indexing (e.g., array[1:3])
  • Performed scalar operations on arrays, such as array * 2 or array + 5
  • Computed summary statistics with np.mean(), np.std(), np.min(), np.max()
  • Discussed importance of random numbers in simulations
  • Generated random numbers: e.g., np.random.normal(loc=0, scale=1, size=10)
  • Plotted histograms of random variables with plt.hist()

Are you more comfortable with Python now? 🐍

Today’s class 📚

Today’s class

Today we will:

Let’s get started! 🚀

Boolean logic 🧠

Boolean logic

A bit of history

  • Named after George Boole, a British mathematician and philosopher
  • Boole’s work on logic laid the foundation for modern computer science
  • Boolean logic is a branch of algebra that deals with true and false values
  • It is useful for computer programming because it is based on binary values
  • In Python, Boolean values are True and False
  • We use them to make decisions in our code

Testing expressions with text 🐍

True and False values

  • We can test whether two values are equal using the == operator (two equal signs)

  • For example, 5 == 5 returns True

  • We can also test whether two values are not equal using the != operator (exclamation mark followed by an equal sign)

  • Let’s see how this works in Python

  • First, let’s load the matplotlib and numpy libraries

import matplotlib.pyplot as plt
import numpy as np

# You can compare two strings by using a double "equal sign"
# This can be useful if you're trying to evaluate whether data was entered correctly

"Is this the real life?" == "is this just fantasy?" 
False

Note: the order of the strings matter!

"ab" == "ba" 
False

True and False values

  • Equality of strings is most useful when you’re comparing an unknown variable to a benchmark

  • Below, try switching the value of any_questions

any_questions = "no"
print(any_questions == "no")
True
any_questions = "yes" 
print(any_questions == "no")
False

Test for the presence of keywords in a sentence (in operator)

  • We can test whether a keyword is present in a sentence or a list using the in operator
  • For example, "apple" in "I like apples" returns True
  • Let’s see how this works in Python
"apple" in "I like apples"
True
keyword = "economic"
sentence = "The Federal Reserve makes forecasts about many economic outcomes"

keyword in sentence

# Try changing the keyword!
True
  • Now, let’s test whether a keyword is present in a list
current_month = "September"
list_summer_months = ["June","July","August"]

print(current_month in list_summer_months)
print('June' in list_summer_months)
False
True

Common pitfall when testing expressions

  • Be careful when testing expressions with text
  • Python is case-sensitive, so "apple" == "Apple" returns False
  • To avoid this, you can use the lower() method to convert all characters to lowercase
  • And please remember there is a difference between double equal signs == and single equal sign =
"apple" == "Apple"
False
"apple".lower() == "Apple".lower()
True
# A single vs double equality sign makes a BIG difference
# When you have a single equality (=) you are assignning the variable
# When you have a double equality (==) you are comparing two values
message_hello = "hello"

print(message_hello == "hello")
True

Try it out!

  • Now, let’s test whether you can identify the correct expression

  • Write code to check whether the string “red” is contained in the list: ["red","green","yellow","orange"] Appendix 01

So far so good? 🤔

Testing expressions with numbers 🐍

Testing expressions with numbers

  • Tests with numbers
    • Strictly less than (<), less than or equal (<=)
    • Equal (==)
    • Strictly more than (>), greater than or equal to (>=)
  • Let’s see how this works in Python
x = 5

print(x < 5)
print(x <= 5)
print(x == 5)
print(x >= 5)
print(x > 5)
False
True
True
True
False

Validate a data type

  • We can test whether a variable is of a certain data type using the isinstance() function
  • For example, isinstance(5, int) returns True
  • Other data types include float, str, list, tuple, dict, set, bool
y = 10

print(isinstance(y,int))
print(isinstance(y,float))
print(isinstance(y,str))
True
False
False

Equality of vectors

  • We can test whether two vectors are equal using the == operator
  • For example, [1,2,3] == [1,2,3] returns True
  • Please note that the equality of vectors is done element-wise
vec_a = np.array([1,2,3])
vec_b = np.array([1,2,4])

vec_a == vec_b
array([ True,  True, False])
  • Try it out! 🚀

  • Define \(x= -1\). Check whether \(x^2 + 2x + 1 = 0\) is true Appendix 02

    • Please remember that \(x^2\) is written as x**2 in Python
    • Please note the difference between == and =

Testing multiple conditions 🐍

Testing multiple conditions

  • We can test multiple conditions using the and and or operators
  • The and operator returns True if both conditions are True
  • The or operator returns True if at least one condition is True

not: the negation operator

  • We can negate a condition using the not operator
  • The not operator returns True if the condition is False and vice versa
    • Yes, it’s a bit confusing at first, but it’s intuitive once you see it in action
  • For instance, imagine that you want to know whether someone can vote in the US
    • Here let’s assume the person is a national and we only care about age
    • The person can vote if they are at least 18 years old
age  = 22

# Can this person legally vote in the US?
not (age < 18)
True
  • The not operator can be separated by a space and parentheses are not necessary
  • But parentheses can be helpful to organize your code logically
not age < 18
True

Condition A and B need to be satisfied: & operator

  • We can test multiple conditions using the & operator
  • The & operator returns True if both conditions are True
  • For example, 5 > 3 & 5 < 10 returns True
# We use the "&" symbol to separate "AND" conditions
age = 31

# Is this age between 20 and 30 (including these ages)?
( age >= 20 ) & (age <= 30)
False

Condition A or B needs to be satisfied: | operator

  • To test whether at least one condition is True, we use the | operator
  • For example, 5 > 3 | 5 > 10 returns True
# We use the "|" symbol to separate "OR" conditions.
age = 31

# Is this age higher than 20 or lower than 30?
( age >= 20 ) | (age <= 30)
True
# Another example
student_status = "freshman" 

# Is the student in the first two years of undergrad?
(student_status == "freshman") | (student_status == "sophomore")
True

Try it out! 🚀

  • Now, let’s test whether you can identify the correct expression
  • Write code that checks the following conditions:
    • Whether age is strictly less than 20, or greater than 30
    • Not in the age range 25-27 Appendix 03

Flow control: conditional statements 🐍

Flow control: conditional statements

If, elif, else

  • Conditional statements are used to make decisions in programming
  • The if statement is used to execute a block of code if a condition is True
  • The elif statement is used to execute a block of code if the first condition is False and the second condition is True
  • The else statement is used to execute a block of code if all other conditions are False
  • We use this a lot in data cleaning or data analysis to filter out data!

If statements

  • Test expression
    • We type “if” followed by a logical condition and the “:” symbol.
    • The “:” says: run the following command
  • Body of expression
    • The “body” of the “if” statement needs to indented with four spaces
    • You can indent text by pressing the “tab” button in your keyboard.
  • If the condition is true, a message will appear.
  • If the condition is false, then nothing happens
  • The if statement syntax is as follows:
if condition:
    Body
  • Another example:
# We start by defining a string
any_questions = "yes"

if any_questions == "yes":
    print("Need to give more explanations")
Need to give more explanations

If/else statements

When you have two possible outcomes

  • The else statement is used to execute a block of code if the condition is False
  • The else statement is always used in conjunction with the if statement
  • The else statement syntax is as follows:
if condition:
    Body
else:
    Body
  • For example, let’s say you want to know whether someone can vote in the US
    • First, the person needs to be a national
    • Second, the person needs to be at least 18 years old
age = 22
national = False

if national & (age >= 18):
    print("This person can vote in the US")
else:
    print("This person cannot vote in the US")
This person cannot vote in the US

Another example

# Here we want to change the colour of the histogram 
# based on a conditional statement

is_graph_red = False
how_many_classes = np.array([7,1,2,3,3,3,4,5,6])

if is_graph_red:
    plt.hist(x = how_many_classes, color="red")
    plt.title("Count of students in each category")
    plt.xlabel("How many classes are you taking?")
    plt.show() 
else:
    plt.hist(x = how_many_classes, color="purple")
    plt.title("Count of students in each category")
    plt.xlabel("How many classes are you taking?")
    plt.show()
  • The histogram will be red if is_graph_red is True, and purple otherwise

Try it out! 🚀

What happens if … ? Try the following:

  • Rerun the above code but change the value of is_graph_red.
  • What happens if you set a non-boolean value of is_graph_red?
  • Don’t include :
  • Don’t indent the body of the if Appendix 04

If/elif/else statements

  • The last conditional statement is the elif statement
  • The elif statement is used to execute a block of code if the first condition is False and the second condition is True
  • It is just like another if statement, but it is used when you have more than two possible outcomes
  • The elif statement syntax is as follows:
if condition:
    Body
elif condition:
    Body
else:
    Body

Example

years_in_program = 1

if years_in_program == 1:
    print("This student is a freshman")
elif years_in_program == 2:
    print("This student is a sophomore")
elif years_in_program == 3:
    print("This student is a junior")
else:
    print("This student is a senior")
This student is a freshman

And that’s it for today! 🎉

Questions? 🤔

Try this at home! 🏠

  • Practice writing conditional statements in Python
  • Create a variable called “points” with a value between 0 and 100
  • Write a flow with “if”, “elif” and “else” to assign the letter grade
  • In the body, store the output in a new variable called “letter_grade”
  • Print the letter grade at the end

Important

Check that it works by trying different values of “points”!

Thank you very much and see you next time! 🙏

Appendix 01

colors = ["red","green","yellow","orange"]

"red" in colors
True

Back to the main text

Appendix 02

x = -1

x**2 + 2*x + 1 == 0
True

Back to the main text

Appendix 03

  • Whether age (age = 31) is strictly less than 20, or greater than 30
  • Not in the age range 25-27
age = 31

(age < 20) | (age > 30) 
True
(age < 25) | (age > 27)
True

The second answer uses | because & evaluates both statements at the same time, and one cannot be less than 25 and greater than 27 at the same time. Therefore, it must be |.

Back to the main text

Appendix 04

What happens if is_graph_red is set to True?

is_graph_red = True
how_many_classes = np.array([7,1,2,3,3,3,4,5,6])

if is_graph_red:
    plt.hist(x = how_many_classes, color="red")
    plt.title("Count of students in each category")
    plt.xlabel("How many classes are you taking?")
    plt.show() 
else:
    plt.hist(x = how_many_classes, color="purple")
    plt.title("Count of students in each category")
    plt.xlabel("How many classes are you taking?")
    plt.show()

Back to the main text

Appendix 04 - Continued

What happens if you set a non-boolean value of is_graph_red?

It is considered True if it is not zero

is_graph_red = 39
how_many_classes = np.array([7,1,2,3,3,3,4,5,6])

if is_graph_red:
    plt.hist(x = how_many_classes, color="red")
    plt.title("Count of students in each category")
    plt.xlabel("How many classes are you taking?")
    plt.show() 
else:
    plt.hist(x = how_many_classes, color="purple")
    plt.title("Count of students in each category")
    plt.xlabel("How many classes are you taking?")
    plt.show()

Back to the main text

Appendix 04 - Continued

What happens if you don’t include :?

You will get a syntax error

is_graph_red = True
how_many_classes = np.array([7,1,2,3,3,3,4,5,6])

if is_graph_red
    plt.hist(x = how_many_classes, color="red")
    plt.title("Count of students in each category")
    plt.xlabel("How many classes are you taking?")
    plt.show() 
else:
    plt.hist(x = how_many_classes, color="purple")
    plt.title("Count of students in each category")
    plt.xlabel("How many classes are you taking?")
    plt.show()

Back to the main text

Appendix 04 - Continued

What happens if you don’t indent the body of the if?

You will get another syntax error

is_graph_red = True
how_many_classes = np.array([7,1,2,3,3,3,4,5,6])

if is_graph_red:
plt.hist(x = how_many_classes, color="red")
plt.title("Count of students in each category")
plt.xlabel("How many classes are you taking?")
plt.show() 
else:
plt.hist(x = how_many_classes, color="purple")
plt.title("Count of students in each category")
plt.xlabel("How many classes are you taking?")
plt.show()

Back to the main text