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.
- Understanding Code Reuse and Modularity in Python 3
- Understanding Python Pickling with example
- Understanding for-loop in Python
- Deconstructing Interpreter: Understanding Behind the Python Bytecode
- Understanding the Execution of Python Program
- Understanding Python Dataclasses
- Understanding Logistic Regression
- Understanding Types of Means | Set 1
- Understanding Types of Mean | Set 2
- Understanding different Box Plot with visualization
- Understanding of OpenSeq2Seq
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Python | Contiguous Boolean Range
- Python | Boolean list initialization
- Python program to fetch the indices of true values in a Boolean list
- Python | Ways to concatenate boolean to string
- Python | Ways to convert Boolean values to integer
- Python | Filter list by Boolean list
- Python | Boolean List AND and OR operations
- Python - False indices in a boolean list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.