Booleans are simple and easy to use concepts that exist in every programming language. A boolean represents an idea of “true” or “false.” While writing an algorithm or any program, there are often situations where we want to execute different code in different situations. Booleans help our code to do just that easy and effective. More often, a boolean value is returned as a result of some kind of comparison operations.
There are two Boolean keywords:
Operators : Operators are special symbols in Python that is used to perform arithmetic or logical computations. The values on which operation is to be done are called operands.while the the operation is denoted by operator(eg. +, -, /, *, %, etc.)
Comparison operators are used to compare values. It returns either
False after computing the condition.
|>||Greater than – True if left operand is greater than the right||x > y|
|<||Less than – True if left operand is less than the right\\\||x < y|
|==||Equal to – True if both operands are equal||x == y|
|!=||Not equal to – True if operands are not equal||x != y|
|>=||Greater than or equal to – True if left operand is greater than or equal to the right||x >= y||<=||Less than or equal to – True if left operand is less than or equal to the right||x <= y|
There are Three Logical operators:
|and||True if both the operands are true||x and y|
|or||True if either of the operands is true||x or y|
|not||True if operand is false.||not x|
A Truth Table is a small table that allows us, to give the results for the logical operators.
and Table : It takes two operands.
|a||b||a and b|
or Table : It takes two operands.
|a||b||a or b|
not Table : It takes only one operand.
Example 1 :Checking whether a list is empty or not. We will pass the list in the
bool() function. When the list is empty,
False is returned and if the list is not empty
True is returned.
Example 2 : Prinitng a range of number using the while loop, the while loop will run until the condition is
1 2 3
Example 3 :With the help of boolean, we can bound our program.
Example 4 :Check greater of two numbers with the help of conditionals. With the help of boolean we can compare the results and execute accordingly
num_2 is greater than num_1
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.