Python False Keyword
False is a boolean value in Python that represents something untrue or a "no" condition. It is one of the two Boolean constants (True and False) and is mostly used in conditions, loops and logical operations.
In Python, False is treated as 0 in mathematical operations and as a falsy value in conditional statements. Let's look at an example.
if False:
print("This will never execute")
Since the condition is False, the print statement will not execute.
Let's look at some examples that involve using False keyword.
Using False in Conditional Statements
x = 10
f = x < 5
if f:
print("x is less than 5")
else:
print("x is not less than 5")
Output
x is not less than 5
Explanation: The condition x < 5 evaluates to False, so the else block get executed instead of the if block.
Using False to Initialize Flags.
A flag is a variable used to track a condition in a program. It is usually set to False at first and changed when needed.
f = False # flag variable
a = [1, 3, 5, 7, 9]
for i in a:
if a == 5:
f = True
break
if f:
print("Number found!")
else:
print("Number not found.")
Output
Number not found.
Explanation: flag 'f' starts as False and it becomes true only if 5 is found in the list.
Using False as 0 in Arithmetic
Python treats False as 0 in arithmetic operations therefore in some cases it can be used in place of 0.
print(False + 5)
print(False * 3)
Output
5 0
Explanation: since False is treated as 0, Fasle + 5 results in 5 and False * 3 results in 0.