Back

Activity 1.10: Booleans and If Statements

divider

Activity 1.10

Booleans and If Statements

Key Concepts

Booleans and Comparison Operators

The if Statement

Code Blocks

Logical Operators

Booleans

A boolean is a value that's either True or False — nothing else.

is_raining = True
has_umbrella = False

Comparison Operators

Comparing two values always produces a boolean.

health = 100
is_alive = health > 0

Comparison Operators

  • == Equal to
  • != Not equal to
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to

So What Is a Boolean For?

On its own, a boolean isn't much use — you just printedTrue or False to the screen.

Its real job is to answer a yes-or-no question that your program then acts on.

That's an if statement.

The if Statement

An if statement runs a block of code only when its condition is True.

age = int(input("Enter your age: "))
if age >= 16:
print("You can drive!")

Indentation Defines the Block

Python doesn't use curly braces to mark a code block — it uses a colon and indentation. Every indented line right after the if belongs to it.

Only Runs When True

Terminal window
Enter your age: 17 [Enter]
You can drive!
Terminal window
Enter your age: 12 [Enter]
[No output]

Multiple Independent Checks

You can write several if statements in a row — each one is checked independently, so more than one can run.

Logical Operators

Sometimes one comparison isn't enough. Logical operators combine booleans, and Python spells them out as words.

  • and — true only if both sides are true
  • or — true if either side is true
  • not — flips a boolean to its opposite

Logical Operators

You'll need and today to check whether a number falls between two values — that takes two comparisons at once.

Today's Objectives

  • Writing boolean expressions with comparison operators
  • Writing an if statement
  • Using indentation to define a code block
  • Combining conditions with and, or, and not

Key Terms

Boolean
A value that is either True or False.
Comparison Operator
An operator like == or >= that compares two values and produces a boolean.
if Statement
Runs a block of code only when its condition is True.
Condition
The boolean expression an if statement checks.
Code Block
A group of statements, marked in Python by indentation.
Logical Operator
and, or, and not — combine or invert booleans.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-10-conditionals.


Task 1: Booleans Are Values

  • Declare the variables shown, then write two boolean expressions.
  • Print each result, including one flipped with not.
Booleans and If Statements
print("--- Booleans Are Values ---")
name = "Mr. Mortimer"
age = 30
balance = 150.00
is_teacher = True
can_drive = age >= 16
can_buy_car = balance > 31000
print(f"Is my name Mr. Mortimer? {name == 'Mr. Mortimer'}")
print(f"Am I old enough to drive? {can_drive}")
print(f"Can I afford the car? {can_buy_car}")
print(f"Am I a teacher? {is_teacher}")
print(f"Am I NOT a teacher? {not is_teacher}")
input("Press enter to continue...")

Task 2: Your First if — Age Validator

  • Check the user's age against three independent milestones (16, 18, 35).
  • Notice that entering 35 runs all three blocks, and entering 12 runs none.
Booleans and If Statements
print("--- Booleans Are Values ---")
name = "Mr. Mortimer"
age = 30
balance = 150.00
is_teacher = True
can_drive = age >= 16
can_buy_car = balance > 31000
print(f"Is my name Mr. Mortimer? {name == 'Mr. Mortimer'}")
print(f"Am I old enough to drive? {can_drive}")
print(f"Can I afford the car? {can_buy_car}")
print(f"Am I a teacher? {is_teacher}")
print(f"Am I NOT a teacher? {not is_teacher}")
input("Press enter to continue...")
print("--- Age Validator ---")
user_age = int(input("Enter your age: "))
if user_age >= 16:
print("Time to hit the road! You've earned your driving permit.")
if user_age >= 18:
print("Adulting 101: You can now vote and live independently.")
print("Please exit your mom's basement.")
if user_age >= 35:
print("You're old enough to run for president.")
nick_name = input("What's your nickname (e.g., 'The Awesome')? ")
print(f"{nick_name} for president!")
input("Press enter to continue...")

Task 3: Score Calculator

  • Check whether a score out of 100 passes or fails.
Booleans and If Statements
print("--- Booleans Are Values ---")
name = "Mr. Mortimer"
age = 30
balance = 150.00
is_teacher = True
can_drive = age >= 16
can_buy_car = balance > 31000
print(f"Is my name Mr. Mortimer? {name == 'Mr. Mortimer'}")
print(f"Am I old enough to drive? {can_drive}")
print(f"Can I afford the car? {can_buy_car}")
print(f"Am I a teacher? {is_teacher}")
print(f"Am I NOT a teacher? {not is_teacher}")
input("Press enter to continue...")
print("--- Age Validator ---")
user_age = int(input("Enter your age: "))
if user_age >= 16:
print("Time to hit the road! You've earned your driving permit.")
if user_age >= 18:
print("Adulting 101: You can now vote and live independently.")
print("Please exit your mom's basement.")
if user_age >= 35:
print("You're old enough to run for president.")
nick_name = input("What's your nickname (e.g., 'The Awesome')? ")
print(f"{nick_name} for president!")
input("Press enter to continue...")
print("--- Score Calculator ---")
score = int(input("Enter your score (0-100): "))
if score >= 60:
print("You passed!")
if score < 60:
print("You did not pass. Keep studying!")
input("Press enter to continue...")

Task 4: Temperature Check — Using and

  • Check a Fahrenheit temperature against three ranges.
  • The middle range needs two comparisons joined by and, because "between 32 and 65" is really two conditions at once.
Booleans and If Statements
print("--- Booleans Are Values ---")
name = "Mr. Mortimer"
age = 30
balance = 150.00
is_teacher = True
can_drive = age >= 16
can_buy_car = balance > 31000
print(f"Is my name Mr. Mortimer? {name == 'Mr. Mortimer'}")
print(f"Am I old enough to drive? {can_drive}")
print(f"Can I afford the car? {can_buy_car}")
print(f"Am I a teacher? {is_teacher}")
print(f"Am I NOT a teacher? {not is_teacher}")
input("Press enter to continue...")
print("--- Age Validator ---")
user_age = int(input("Enter your age: "))
if user_age >= 16:
print("Time to hit the road! You've earned your driving permit.")
if user_age >= 18:
print("Adulting 101: You can now vote and live independently.")
print("Please exit your mom's basement.")
if user_age >= 35:
print("You're old enough to run for president.")
nick_name = input("What's your nickname (e.g., 'The Awesome')? ")
print(f"{nick_name} for president!")
input("Press enter to continue...")
print("--- Score Calculator ---")
score = int(input("Enter your score (0-100): "))
if score >= 60:
print("You passed!")
if score < 60:
print("You did not pass. Keep studying!")
input("Press enter to continue...")
print("--- Temperature Check ---")
current_temp = int(input("What is the current temperature in Fahrenheit? "))
if current_temp <= 32:
print("Brrr! It's freezing. Don't forget your coat!")
if current_temp > 32 and current_temp <= 65:
print("It's a bit chilly. A light jacket should be perfect.")
if current_temp > 65:
print("It's warm outside. Enjoy the nice weather!")
input("Press enter to continue...")

Task 5: Letter Grade Calculator

  • Check a grade percentage against five letter-grade ranges.
  • Every middle range needs its own and. Keep an eye on how repetitive this gets — the next activity fixes exactly that.
Booleans and If Statements
print("--- Booleans Are Values ---")
name = "Mr. Mortimer"
age = 30
balance = 150.00
is_teacher = True
can_drive = age >= 16
can_buy_car = balance > 31000
print(f"Is my name Mr. Mortimer? {name == 'Mr. Mortimer'}")
print(f"Am I old enough to drive? {can_drive}")
print(f"Can I afford the car? {can_buy_car}")
print(f"Am I a teacher? {is_teacher}")
print(f"Am I NOT a teacher? {not is_teacher}")
input("Press enter to continue...")
print("--- Age Validator ---")
user_age = int(input("Enter your age: "))
if user_age >= 16:
print("Time to hit the road! You've earned your driving permit.")
if user_age >= 18:
print("Adulting 101: You can now vote and live independently.")
print("Please exit your mom's basement.")
if user_age >= 35:
print("You're old enough to run for president.")
nick_name = input("What's your nickname (e.g., 'The Awesome')? ")
print(f"{nick_name} for president!")
input("Press enter to continue...")
print("--- Score Calculator ---")
score = int(input("Enter your score (0-100): "))
if score >= 60:
print("You passed!")
if score < 60:
print("You did not pass. Keep studying!")
input("Press enter to continue...")
print("--- Temperature Check ---")
current_temp = int(input("What is the current temperature in Fahrenheit? "))
if current_temp <= 32:
print("Brrr! It's freezing. Don't forget your coat!")
if current_temp > 32 and current_temp <= 65:
print("It's a bit chilly. A light jacket should be perfect.")
if current_temp > 65:
print("It's warm outside. Enjoy the nice weather!")
input("Press enter to continue...")
print("--- Letter Grade Calculator ---")
final_grade = int(input("Enter your final grade percentage (0-100): "))
if final_grade >= 90:
print("You got an A! Excellent work!")
if final_grade >= 80 and final_grade < 90:
print("You got a B! Great job!")
if final_grade >= 70 and final_grade < 80:
print("You got a C. Solid effort.")
if final_grade >= 60 and final_grade < 70:
print("You got a D. You passed, but there's room to improve.")
if final_grade < 60:
print("You got an F. Let's review the material and try again.")
divider

Checkpoint

Verify your program works correctly.

Example Output
--- Booleans Are Values ---
Is my name Mr. Mortimer? True
Am I old enough to drive? True
Can I afford the car? False
Am I a teacher? True
Am I NOT a teacher? False
Press enter to continue...
--- Age Validator ---
Enter your age: 35 [Enter]
Time to hit the road! You've earned your driving permit.
Adulting 101: You can now vote and live independently.
Please exit your mom's basement.
You're old enough to run for president.
What's your nickname (e.g., 'The Awesome')? Sleepy [Enter]
Sleepy for president!
Press enter to continue...
--- Score Calculator ---
Enter your score (0-100): 76 [Enter]
You passed!
Press enter to continue...
--- Temperature Check ---
What is the current temperature in Fahrenheit? 50 [Enter]
It's a bit chilly. A light jacket should be perfect.
Press enter to continue...
--- Letter Grade Calculator ---
Enter your final grade percentage (0-100): 66 [Enter]
You got a D. You passed, but there's room to improve.
divider

Reflection

Answer the following questions before submitting your work.

  1. How does Python know which lines belong inside an if statement's block?
  2. In Task 2, what would happen if you entered an age of 10? Which messages, if any, would print — and why?
  3. In the Letter Grade Calculator, why does the B range need two conditions joined by and instead of just one?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete