Python False Keyword



In Python, False keyword represents the boolean value. It is a result of comparison operation. The integer value of false is zero.

It is case-sensitive. It is commonly used in control flow statements like if,while and for loops to control the flow and execute based on logic.

Integer Value Of False Keyword

The integer value of False keyword is 0. In arithmetic operations, we can use False instead of zero.

Example

Lets find the integer value of False keyword with following example −

x=False
y=int(x)
print("The integer value of ",x,":",y)

Output

Following is the output of the above code −

The integer value of  False : 0

False Keyword is Case sensitive

False keyword is case-sensitive. It must be written as False, With F capitalized. Using false instead of False will results in a NameError

Example

x=false
print("We will get NameError",x)

Output

Traceback (most recent call last):
  File "E:\pgms\Keywords\false.py", line 11, in <module>
    x=false
      ^^^^^
NameError: name 'false' is not defined. Did you mean: 'False'?

False Keyword in Conditional Statement

The False keyword is used in if block, while loops. if block is executed only if the condition is True. If the given condition is False, that particular block will not be executed. Similarly in while loop the statements inside the loop will not be executed.

Example : False keyword in if statement

Lets understand the False keyword with an example. Here, the value of x is 9. The x is a positive number which is greater than 0. The given condition is x is less than 0 which is False so the code inside the block will not be executed.

x=9
if x<0:
    print(x,"is a negative number")
else:
    print(x,"is a positive number")

Output

Following is the output of the above code −

9 is a positive number

False keyword in loops

When the given condition is not satisfied then the condition is False.In such conditions, the statements inside the while loop will not be executed.

Example

var=False
while var:
    print("This statements will not be executed as the condition is False.")
   
print("This statements gets executed as it is outside the while loop.")

Output

This statements gets excecuted as it is outside the while loop.

False keyword in Functions

Lets understand False keyword in functions. The function will return True or False based on the condition. If the condition is satisfied returns True else, returns False

def natural(num):
    return num>0
    
x=-4
print("The given number is natural number True/False :",natural(x))
y=9
print("The given number is natural number True/False :",natural(y))

Output

Following is the output of the above code −

The given number is natural number True/False : False
The given number is natural number True/False : True
python_keywords.htm
Advertisements