Python Boolean Variable Types – POFTUT

Python Boolean Variable Types


Logic operations are one of the important topics in application development. Python supports logic operations and value as boolean. Boolean is data type used to represent logic values True and False. True generally used to positive or enabled situations. False is generally used negative or disabled situations. For example if we want to express existence of an item we will use boolean value.

Create Boolean Variable

Creating boolean value is easy as defining strings and integers. We will provide initialization value to the given variable with equal sign.  Below are some creation examples of boolean variables.

home_exist=True 
car_exist=False 
home_exist 
#True 
car_exist 
#False
Create Boolean Variable
Create Boolean Variable

Not Logic

Reversing operations or notting is useful feature of boolean logic operations. not keyword will flip the current logical value. If the current logical value is True and not ed new value will be False . If the current value is False and not ted new value will be True .

home_exist 
#True 
not home_exist 
#False 
not True 
#False 
not False 
#True
Not Logic
Not Logic

Check With If

Python provides conditionals in order to branch code execution. If .. elif .. else statements can be used with logical values. If provided value is True this means condition is met.

In this example we will check if car_exist is True print  I have a car to to standard output.

car_exist=True 
if car_exist: 
   print("I have a car")
Check With If
Check With If

Convert To Boolean Value

Up to now we have used True and False as boolean values. We can use different literals like string and number as boolean value too. Here some of these values and related True or False representation. We will use bool function in order to convert given values into boolean values.

bool(1) 
#True 
bool(0) 
#False 
bool(-1) 
#True 
bool(10) 
#True 
bool('') 
#False 
bool('test') 
#True
Convert To Boolean Value
Convert To Boolean Value

LEARN MORE  Php - Operators

Leave a Comment