Understanding and Using Booleans
In this tutorial, you'll learn about booleans, a simple yet powerful data type in Python. Booleans help us answer yes/no questions in our code, such as "Is this true?" or "Does this condition meet the requirement?"


1. What Are Booleans?

Booleans represent one of two values:

These values are often the result of comparisons or conditions in a program.


2. Using Booleans

a. Direct Assignment

You can create a boolean variable by assigning True or False directly.
Example:

is_sunny = True

is_raining = False

print("Is it sunny today?", is_sunny)


Output:

Is it sunny today? True


b. Comparisons

Booleans are often used to compare values using comparison operators:

Example:

age = 13

is_teenager = age >= 13 and age <= 19

print("Is the person a teenager?", is_teenager)


Output:

Is the person a teenager? True


c. Logical Operators

You can combine boolean values using logical operators:

Example:

is_weekend = True

is_holiday = False

can_relax = is_weekend or is_holiday

print("Can I relax today?", can_relax)


Output:

Can I relax today? True



3. Booleans in Conditional Statements

Booleans are commonly used in if statements to decide what code to run.

Example:

is_hungry = True

if is_hungry:

    print("Time to eat!")

else:

    print("No need to eat right now.")


Output (if is_hungry is True):

Time to eat!



4. Practice Exercises

Exercise 1: Simple Booleans

Create a program that:

Exercise 2: Comparisons

Write a program that:

Exercise 3: Logical Operators

Create a program that:


5. Bonus Challenge

Write a program that checks if a student passes a test:

Example Output:

Enter your test score: 65  

You passed!



6. What’s Next?

Booleans are the foundation of making decisions in Python. Once you’re comfortable with them, you can explore loops and more complex logic!

Great job learning about booleans! 🎉