← Back to Blog

Everything is True or False

The problem...

You've worked with text. You've worked with numbers.

But some questions don't have a text or number answer.

Is the user logged in? Is the price above 100? Is the name correct?

These questions have only two possible answers. Yes or no. True or false.

The idea!

Python has a type for exactly this: bool.

A bool can only be one of two values: True or False.

Nothing else.

Making it real

is_logged_in = True
is_admin = False

print(is_logged_in)  # True
print(is_admin)      # False
print(type(is_logged_in))  # <class 'bool'>

Notice the capital T and F. true and false don't work — Python is case sensitive.

In practice

You don't always assign True or False directly. Python generates them for you.

print(10 > 5)    # True
print(10 < 5)    # False
print(10 == 10)  # True
print(10 == 9)   # False
print(10 != 9)   # True

These are called comparison operators. They always return a bool.

The comparison operators

  • == — equal to
  • != — not equal to
  • > — greater than
  • < — less than
  • >= — greater than or equal to
  • <= — less than or equal to

Going further

Bools work with variables too — not just raw numbers.

age = 20
price = 150

print(age >= 18)    # True
print(price > 200)  # False
print(age == 20)    # True

What's really happening

Every comparison Python makes returns a bool.

You'll use this constantly — in decisions, in loops, in conditions.

Bool is small. But it's everywhere.

Heads up!

  • True and False are capitalized — always
  • == compares values — = assigns a value. Don't mix them up
  • True and False are also 1 and 0 in Python — but that's for later

The mindset shift

Stop thinking: "I need a number or text to store information."

Start thinking: "Some information is just yes or no — and Python has a type for that."

What you should understand now

  • bool has only two values — True and False
  • Comparison operators always return a bool
  • True and False are capitalized
  • == compares, = assigns — never confuse them
[ login to bookmark ] // copied! 37 views · 1 min
// resources
Code Example bool_basics.py
← prev This One's Yours — Numbers next → What If One Thing Is True and the Other Isn't?
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.