Python Booleans
In Python, Booleans are a fundamental data type used to represent the concepts of true and false.
They are written using the keywords True
and False
.
1is_active = True 2is_inactive = False
Booleans are often used in control flow statements, such as if
and while
loops, to determine whether a certain condition is true or false. They can also be used in comparison operations, such as greater than, less than, equal to, etc.
Python provides several built-in functions and methods for working with Booleans, such as bool()
which converts a value to its Boolean representation, and logical operators such as and
, or
, and not
that allow you to create more complex Boolean expressions.
1x = 5 2print(bool(x)) # Output: True 3print(x > 3 and x < 10) # Output: True
Ternary operator
you can use ternary operator to create a shorthand version of an if-else
statement.
1is_active = True 2status = 'active' if is_active else 'inactive' 3print(status) # Output: 'active'