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.
is_member = True
has_paid = False
print(is_member)
print(type(has_paid).__name__)True bool
Comparison operators
Comparing two values produces a Boolean.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | equal to | 5 == 5 | True |
!= | not equal to | 5 != 3 | True |
> | greater than | 5 > 8 | False |
< | less than | 5 < 8 | True |
>= | greater than or equal | 5 >= 5 | True |
<= | less than or equal | 4 <= 3 | False |
balance = 800
fare = 150
print(balance >= fare)
print(balance == 1000)
print("Nairobi" == "nairobi")True False False
= 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:
Logical operators
Logical operators combine Booleans.
| Operator | Result is True when… |
|---|---|
and | both sides are True |
or | at least one side is True |
not | the value is False (it flips it) |
age = 20
has_id = True
print(age >= 18 and has_id)
print(age < 18 or has_id)
print(not has_id)True True False
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:
FalseandNone- the number zero:
0,0.0 - empty collections:
"",[],(),{},set()
print(bool("Kisii"))
print(bool(""))
print(bool(0))
print(bool(-5))
print(bool([]))
print(bool([0]))True False False True False True
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.
| Operator | Same as |
|---|---|
x += 3 | x = x + 3 |
x -= 3 | x = x - 3 |
x *= 3 | x = x * 3 |
x /= 3 | x = x / 3 |
x //= 3 | x = x // 3 |
x %= 3 | x = x % 3 |
x **= 3 | x = x ** 3 |
wallet = 1000
wallet -= 250
wallet += 100
wallet *= 2
print(wallet)1700
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.
towns = ["Thika", "Machakos", "Embu"]
print("Embu" in towns)
print("Meru" not in towns)
result = None
print(result is None)True True True
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:
()brackets**power*,/,//,%+,-- comparisons:
==,!=,<,>,<=,>=,in,is notandor
print(2 + 3 * 4)
print((2 + 3) * 4)
print(True or False and False)14 20 True
When in doubt, add brackets. They make your intention clear to both Python and the next person reading your code.
Exercises
Complete the comparison that checks if a equals b.
print(a b)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.
SKOPE