Intro to Python
Share
Explore
Learn Python

icon picker
Conditionals

This section covers:
Boolean expressions
Logical operators
If/else statements

Boolean Expressions

In the previous section, we learned what an expression is. Boolean expressions are expressions that evaluate to a boolean value - either true or false.
We’ve already seen an example of boolean expressions: comparing 2 numbers. 10 > 0 is a boolean expression that uses the > comparison operator to check if 10 is greater than 0. Other comparison operators are < (less than), == (equals), != (not equals).

Logical Operators

Python has 3 logical operators: and, or, and not. Like all Python operators, these perform operations on expressions. Specifically, they can only be used on boolean expressions, and using a logical operator will result in a boolean value.
and is True if both expressions are True
or is True if one or both expressions are True
not is True if the expression is False (it does a reversal)

4 > 5 and 6 != 10 # evaluates to False
4 < 5 and 6 != 10 # evaluates to True
4 > 5 or 6 != 10 # evaluates to True
not 4 > 5 # evaluates to True

If/else Statements

If/else statements let you tell the program how to make decisions. Every if/else statement has a boolean expression as its condition.
if number_of_points >= 10:
print("You win!")
else:
print("You still need more points")
Task
Status
1
Learn about booleans
Not Started
2
Learn about logical operators
Not Started
3
Learn about if/else statements
Not Started
No results from filter

Next Section ➡️


Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.