Editor settings

General settings

Save settings

Learn to program with Python

Chain booleans with the and keyword #

In Python, and and or are logical operators used to chain together multiple conditions in an expression.

and returns True if all conditions in the expression evaluate to True. If any condition is False, the entire expression evaluates to False. For example:

x = 5
y = 10
z = 15

result = x < y and y < z
print(result) # Output: True

result_2 = x < y and y == z
print(result_2) # Output: False

In the above example, the variable result is True, since both conditions were True: x < y and y < z. However, the variable result_2 is False because one of the conditions is False (y == z)


Exercise #

Use and to set the variables' values.

Tests #

  • Variable true_and_true should be True
  • Variable false should be False
  • Use and keyword to set the value of variable true_and_true
  • Use and keyword to set the value of variable false
Output will be displayed here