Tutorials › Python Tutorial › Python Booleans and Operators

Python Booleans and Operators

11 min read · Lesson 9 of 24

A Boolean is a value that is either True or False. Booleans are how programs make decisions: is the customer over 18? Is the balance enough? Has the deadline passed? In Python, the Boolean type is called bool.

True and False

Note the capital letters. true (lowercase) is not a Python value and will cause an error.

Example PYTHON
is_member = True
has_paid = False
print(is_member)
print(type(has_paid).__name__)
Result
True
bool
Try it Yourself »

Comparison operators

Comparing two values produces a Boolean.

OperatorMeaningExampleResult
==equal to5 == 5True
!=not equal to5 != 3True
>greater than5 > 8False
<less than5 < 8True
>=greater than or equal5 >= 5True
<=less than or equal4 <= 3False
Example PYTHON
balance = 800
fare = 150
print(balance >= fare)
print(balance == 1000)
print("Nairobi" == "nairobi")
Result
True
False
False
Try it Yourself »
Warning: = assigns a value; == compares two values. Writing if x = 5: is a syntax error. This is one of the most common beginner mistakes.

You can also chain comparisons, just like in maths:

Example PYTHON
age = 22
print(18 <= age < 35)
Result
True
Try it Yourself »

Logical operators

Logical operators combine Booleans.

OperatorResult is True when…
andboth sides are True
orat least one side is True
notthe value is False (it flips it)
Example PYTHON
age = 20
has_id = True
print(age >= 18 and has_id)
print(age < 18 or has_id)
print(not has_id)
Result
True
True
False
Try it Yourself »

Truthy and falsy values

The bool() function converts any value to a Boolean. Most values count as True. Only "empty" or zero-like values count as False:

  • False and None
  • the number zero: 0, 0.0
  • empty collections: "", [], (), {}, set()
Example PYTHON
print(bool("Kisii"))
print(bool(""))
print(bool(0))
print(bool(-5))
print(bool([]))
print(bool([0]))
Result
True
False
False
True
False
True
Try it Yourself »

This means you can write if name: instead of if name != "":. You will use this a lot in Python If...Else.

Assignment operators

Assignment operators update a variable using its current value. They are shortcuts.

OperatorSame as
x += 3x = x + 3
x -= 3x = x - 3
x *= 3x = x * 3
x /= 3x = x / 3
x //= 3x = x // 3
x %= 3x = x % 3
x **= 3x = x ** 3
Example PYTHON
wallet = 1000
wallet -= 250
wallet += 100
wallet *= 2
print(wallet)
Result
1700
Try it Yourself »

Membership and identity operators

in and not in check whether a value is inside a collection. is and is not check whether two names point to the same object in memory.

Example PYTHON
towns = ["Thika", "Machakos", "Embu"]
print("Embu" in towns)
print("Meru" not in towns)

result = None
print(result is None)
Result
True
True
True
Try it Yourself »
Tip: Use is only to compare with None (or True/False). For comparing values such as numbers and strings, always use ==.

Operator precedence

When an expression has several operators, Python follows an order, from highest to lowest:

  1. () brackets
  2. ** power
  3. *, /, //, %
  4. +, -
  5. comparisons: ==, !=, <, >, <=, >=, in, is
  6. not
  7. and
  8. or
Example PYTHON
print(2 + 3 * 4)
print((2 + 3) * 4)
print(True or False and False)
Result
14
20
True
Try it Yourself »

When in doubt, add brackets. They make your intention clear to both Python and the next person reading your code.

Exercises

Exercise

Complete the comparison that checks if a equals b.

print(a  b)
Exercise

Fill in the logical operator so the result is True only when both are true.

print(is_adult  has_ticket)

Next, start working with collections in Python Lists.

Sign in or create a free account to track progress, earn XP, badges and a certificate.