Loading...

Python Boolean Type

Everyday scenario

Think about logging into a website. You type your username and password, and the system checks if they are correct. If they are, you’re granted access. If not, you’re denied. Behind the scenes, the program is using Boolean values to make that decision.

Below is a cheat sheet for this lesson, highlighting the key concepts and notes for quick revision.

Python boolean type

In this lesson, we’ll create a small login system to explore the Boolean type in Python.

You’ve been hired to program login system! To do it, you need to understand Python’s Boolean type.

The core idea

A Boolean (or bool) is the simplest data type in Python. It only has two possible values:

  • True
  • False

Booleans usually come from comparisons or conditions.

Examples:

python
3 lines
|
20/ 500 tokens
1
2
3
print(5 > 3)     # True
print(2 == 4)    # False
print("a" in "apple")  # True
Code Tools

These results are Boolean values that can be stored in variables and used in decisions.

Why this matters

Look at our login system. It only has two outcomes: the password is correct, or it isn’t. Using Booleans makes it easy for the program to decide what to do next.

Quick recap

  • Boolean values: True or False.
  • Created from comparisons (>, <, ==, !=).
  • Useful in conditions like if statements.

Try it in code

Let’s build a basic login check:

python
6 lines
|
32/ 500 tokens
1
2
3
4
5
6
password = "python123"

user_input = "python123"
is_correct = (user_input == password)

print("Password correct?", is_correct)
Code Tools

Output:

text
1 lines
|
6/ 500 tokens
1
Password correct? True

If you change user_input to "hello", the output will be:

text
1 lines
|
6/ 500 tokens
1
Password correct? False

Learn concept with AI

If you’re using an AI assistant, try this prompt:

Prompt:
Write a Python program that asks the user for a password. If the password matches "python123", print Access Granted. Otherwise, print Access Denied. Use Boolean values in your code.

The AI will generate the full solution with Booleans and conditions.

Great progress! Now let’s test your understanding.

Task

Create a small program that checks if a student passed an exam.

  • Passing score = 50
  • Student score = 72

The program should use a Boolean value to decide if the student passed and print:

text
2 lines
|
6/ 500 tokens
1
2
Score: 72
Passed: True

Solution

python
7 lines
|
28/ 500 tokens
1
2
3
4
5
6
7
score = 72
passing_score = 50

passed = score >= passing_score

print("Score:", score)
print("Passed:", passed)
Code Tools
Perfect! You’ve now learned how to use Booleans in Python to represent yes/no or true/false situations.