Conditionals

CSCI 1012

Announcements

  • Your quizzes will reflect the code you turn in for your homework
  • If you use more advanced python than we’ve taught, you will be responsible for that material
  • So make sure you understand your solutions!

In-Class Exercises

  • Three in-class exercises today:
    • eligible_voter.py
    • discounted_admission.py
    • greater_number.py
  • Turn in on the submit server before the end of lecture for credit
  • Turn in one .zip with all the .py files
    • Unlimited attempts during lecture
  • If your version isn’t working, submit the provided solutions and come to office hours to debug

Today

Goal: Run different chunks of code depending on whether or not some condition holds:

  • Comparison Operators (==, !=, <, >, <=, >=)
  • Logical Operators (and, or, not)
  • If/Elif/Else Statements

Comparison Operators

Comparison operators

Operator Function Syntax
== Equal? 3 == 5
!= Not equal? 3 != 5
< Less than? 3 < 5
> Greater than? 3 > 5
<= Less than or equal to 3 <= 5
>= Greater than or equal to? 3 >= 5

Booleans

  • These comparisons return special values called Boolean variables (type: bool)


  • There are only two: True and False


  • These are different than the strings “True” and “False”
    • True != "True"

Comparison Operators

Operator Function Example
== Equal? 3 == 5 returns False
!= Not equal? 3 != 5 returns True
< Less than? 3 < 5 returns True
> Greater than? 3 > 5 returns False
<= Less than or equal to 5 <= 5 returns True
>= Greater than or equal to? 3 >= 5 returns False

Floats and ints can be compared

print(8.0 == 8)
True


print(8.0 >= 8)
True


print(8.000001 == 8)
False

Strings

print("cat" == "hello")
False


print("hello" != 4)
True


print("4" == 4)
False

Be careful with = vs. ==!

They are not interchangeable:

  • = is used for variable assignment

  • == is used to check for equality between 2 values

x = 4
x == 5
print(x)
print(x == 5)
4
False

Some additional notes on Booleans

  • You can store them in variables
x = False
y = (4 >= 2)
  • You can return and print them
y = (4 >= 2)
print(y)
True
  • You can combine expressions
print((3 + 2.0) <= (5 * 1))
True

Logical Operators

Combining Comparisons: and

  • and: True if all expressions being combined are True; otherwise False
x = 2
y = 2
print(x > 1 and x == y)
True


x = 2
y = 4
print((x > 1) and (x == y))
False


print(True and False)
False

Combining Comparisons: or

  • or: True if at least one of expressions being combined is True
x = 2
y = 2
print(x > 1 or x == y)
True


x = 2
y = 4
print(x > 1 or x == y)
True


x = 0
y = 2
print(x > 1 or x == y)
False

not

  • not: Returns the opposite boolean value
print(not True)
False


print(not 4 <= 2)
True

If Statements

If Statement (Conditional Execution)

  • Allows some blocks of code to only be executed/run under certain conditions
  • Syntax:
if CONDITION:
  # Insert code here (indented) that will only run if CONDITION is True
# Unindented code inserted here will run as normal

If Statement (Conditional Execution)

if True:
  print("The condition was True, so here we are")
The condition was True, so here we are


if False:
  print("The condition was True, so here we are")

(nothing is printed)

If Statement (Conditional Execution)

x = 4
if (x > 1):
  print("x is greater than 1")
print("done running code")

If Statement (Conditional Execution)

x = 4
if (x > 1):
  print("x is greater than 1")
print("done running code")

If Statement (Conditional Execution)

x = 4
if (x > 1):
  print("x is greater than 1")
print("done running code")
x is greater than 1

If Statement (Conditional Execution)

x = 4
if (x > 1):
  print("x is greater than 1")
print("done running code")
x is greater than 1
done running code

If Statement (Conditional Execution)

x = 4
if (x > 1):
  print("x is greater than 1")
print("done running code")
x is greater than 1
done running code

 

x = 0
if x > 1:
  print("x is greater than 1")
print("done running code")

If Statement (Conditional Execution)

x = 4
if (x > 1):
  print("x is greater than 1")
print("done running code")
x is greater than 1
done running code


x = 0
if x > 1:
  print("x is greater than 1")
print("done running code")

If Statement (Conditional Execution)

x = 4
if (x > 1):
  print("x is greater than 1")
print("done running code")
x is greater than 1
done running code


x = 0
if x > 1:
  print("x is greater than 1")
print("done running code")
done running code

If/Else Statement

  • Can optionally add an else statement afterwards
  • Exactly one of the two blocks of code will run
  • Syntax:
if CONDITION:
  # Indented code here that will run if CONDITION is True
else:
  # Indented code here that will run if CONDITION is False

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is greater than 1

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is greater than 1

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is greater than 1
done running code

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is greater than 1
done running code


x = 0
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is greater than 1
done running code


x = 0
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is greater than 1
done running code


x = 0
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is greater than 1
done running code


x = 0
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is less than or equal to 1

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is greater than 1
done running code


x = 0
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is less than or equal to 1
0

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is greater than 1
done running code


x = 0
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is less than or equal to 1
0
done running code

Examples of If/Else Statements

x = 4
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is greater than 1
done running code


x = 0
if x > 1:
  print("x is greater than 1")
else:
  print("x is less than or equal to 1")
  print(x)
print("done running code")
x is less than or equal to 1
0
done running code

Nested Statements

  • You can nest your statements
x = 0
if x > 1:
  print("x is greater than 1")
else:
  if (x >= 0):
    print("x is greater than or equal to 0, but less than or equal to 1")
  else:
    print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1

If/Elif/Else

  • Can optionally add one or more elif statements
  • These will be checked if the statement before it was False
  • Syntax:
if (CONDITION1):
  # Indented code that executes if CONDITION1 is True
elif (CONDITION 2):
  # Indented code that executes if CONDITION1 is False and CONDITION2 is True
elif (CONDITION 3):
  # Indented code that executes if CONDITION1 and CONDITION2 are False and CONDITION 3 is True
else:
  # Indented code that executes if CONDITION1, CONDITION2, CONDITION3 are all False

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1


x = -4
if x > 1:
  print("x is greater than 1")
elif x >= 0: 
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1


x = -4
if x > 1:
  print("x is greater than 1")
elif x >= 0: 
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1


x = -4
if x > 1:
  print("x is greater than 1")
elif x >= 0: 
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1


x = -4
if x > 1:
  print("x is greater than 1")
elif x >= 0: 
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1


x = -4
if x > 1:
  print("x is greater than 1")
elif x >= 0: 
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is less than 0

If/Elif/Else

x = 0
if x > 1:
  print("x is greater than 1")
elif (x >= 0):
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is greater than or equal to 0, but less than or equal to 1


x = -4
if x > 1:
  print("x is greater than 1")
elif x >= 0: 
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is less than 0

Returning when using if statements

  • You can include multiple return statements in a function, but the function ends the first time a return statement is hit
def describe_number(x):
  if x > 1:
    return "x is greater than 1"
  elif x >= 0: 
    return "x is greater than or equal to 0, but less than or equal to 1"
  else:
    return "x is less than 0"

first = describe_number(2)
print(first)
second = describe_number(-4)
print(second)
x is greater than 1
x is less than 0

Multiple If Statements

x = 4
if x > 1:
  print("x is greater than 1")
if x >= 0: 
  print("x is greater than or equal to 0, but less than or equal to 1")
else:
  print("x is less than 0")
x is greater than 1
x is greater than or equal to 0, but less than or equal to 1

In-class Exercises

Exercise 1

  • Write a function eligible_voter
  • Your function should take as argument a person’s age
  • The function should return True (the boolean) if they are eligible to vote (i.e., at least 18)
  • The function should return False (the boolean) if they are not eligible to vote based on their age

Desired output:

x = eligible_voter(6)
print(x)
False
y = eligible_voter(70)
print(y)
True
z = eligible_voter(34)
print(z)
True
eligible_voter.py
# 3 Feb 2025 - In Class
def eligible_voter(  ): # you need to add an argument
  # your code goes here
  # return statement

Submit on the submit server!

Exercise 2

  • Write a function discounted_admission
  • Your function should take as argument a person’s age
  • If the person is younger than 18 or 65 or older, your function should return “Discounted admission”
  • Otherwise, the function should return “Full price”

Desired output:

x = discounted_admission(6)
print(x)
Discounted admission
y = discounted_admission(70)
print(y)
Discounted admission
z = discounted_admission(34)
print(z)
Full price
discounted_admission.py
# 3 Feb 2025 - In Class
def discounted_admission(  ): # you need to add an argument
  # your code here

Submit on the submit server!

Exercise 3

  • Note: we ran out of time to go through this one, but I encourage you to try it on your own
  • Write a function greater_number
  • Your function should take two numbers as input
  • Your function should return the greater number as a float
  • It doesn’t matter which you return if they are equal
  • Note: You may not use the max function even if you are familiar with it

Desired output:

x = greater_number(5, 7)
print(x)
7.0
y = greater_number(5, 5.0)
print(y)
5.0
greater_number(2, 3)

(nothing is printed, but it returns 3.0)

greater_number.py
# 3 Feb 2025 - In Class
def greater_number(x, y): # you can name the arguments whatever you want
  # your code here

Submit on the submit server!

Topics Covered

  • Comparison Operators (==, !=, <, >, <=, >=)
  • Logical Operators (and, or, not)
  • If/Elif/Else Statements

Next Week

Reading: Think Python Chapter 7

Homework 4 due Sunday, 9 Feb 11:55 PM

  • 6 attempts!