Concept of Boolean in Conditional Statements
A boolean is a data type that can have one of two values: true or false. They are often used to represent the true value of a condition. Boolean values are commonly used in control flow statements, such as if and while loops, and in conjunction with comparison operators.
Here are some examples of how Boolean values can be used in Python:
Example:
x = 5
y = 3
# Using a Boolean in an if statement
if x > y:
print("x is greater than y")
# Using a Boolean in a while loop
while x > 0:
print(x)
x -= 1
# Using a Boolean with a comparison operator
result = x == y
print(result) # False
Output:
x is greater than y
5
4
3
2
1
False
Boolean values can also be created using the bool() function, which returns the Boolean value of an expression.
Example:
x = 5
result = bool(x)
print(result) # True
Output:
True
The bool() function returns True if the expression passed to it is true, and False otherwise. In Python, 0, empty strings, empty lists, empty sets, empty dictionaries, and the special value None are considered false; all other values are considered true.
Boolean values are an essential part of programming and are used to control the flow of the program and make decisions based on the values of expressions and variables.
Boolean Expression
Boolean expressions are expressions that return true or false values. Multiple Boolean expressions can be combined to form a Boolean expression.
Boolean Operator
The boolean type has three basic operators. These are AND, OR, NOT. In the case of AND, if the value of all the variables is true, then the expression is true, otherwise, the expression is false. In the case of OR, if the value of at least one variable is true, then the expression is true, otherwise, the expression is false. NOT is a unary operator. It usually returns the opposite value of a variable or expression. In conditional statements in the control structure, this concept is very useful.
Truth Table
The issues are highlighted in the Truth Table below:
P | Q | P AND Q | P OR Q | NOT P | NOT Q |
0 | 0 | 0 | 0 | 1 | 1 |
0 | 1 | 0 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 0 | 1 |
1 | 1 | 1 | 1 | 0 | 0 |
In addition to these basic operators, there are other operators that are made up of these three. For example, XOR, XAND, NAND, NOR, etc. This will be discussed again in the next section.
If Statement
“If statement” is the basic feature of conditional statements in the control structure. Some statements or code blocks can be run based on a specific condition using the if statement in Python. If a condition or expression is true, then the statement will execute when it is run.
Case #1:
P = 38
Q = 10
if P > Q:
print("Inside of if statement")
print("P is greater than Q")
print("Outside of if statement")
Output:
Inside of if statement
P is greater than Q
Outside of if statement
Case #2:
P = 38
Q = 10
if P > Q:
print("Inside of if statement")
print("P is greater than Q")
print("Outside of if statement")
Output:
Inside of if staement
P is greater than Q
Outside of if statement
In the above program, the if condition is checked to see if 38 is greater than 10. When it gets bigger, the two lines inside it are executed. since it is a true condition. So 38 is greater than 10, and lines are being printed IF the scope is finished. Again, the last line is a general statement outside of these conditions. And so this line is also being printed in general.
And yes, this is to say that it is not included in the statement or not, it is determined by using the appropriate white space in front of the indentation or statement. In the above example if 38>10: is the condition or expression.
Indentation is mandatory in Python; otherwise, an error will be created. The program in the example above cannot run on the Python console because it contains multiple statement lines with multiple indents. Create a Python file with the statements, and then run it or run it on the IDE.
Nested if
First, there is an “if” expression and then another if expression in the accompanying statement. If the first condition is true, then the statement inside it runs properly. There is an if expression like the general statement that also runs and determines its authenticity and runs the next statement.
Example:
value = 38
if value > 10:
print("Bigger than 10")
if value <= 50:
print("Between 10 and 50")
Output:
Bigger than 10
Between 10 and 50
else Statement
In the previous section, we have seen how, the “if” condition is true, then the code block under it is run. And “else” is related to “if”. That is if the said if condition is not true, then the code block under else is run or executed.
Example:
P = 38
if P == 10:
print("This is if block for the number 10")
else:
print("else block with the number 38")
Output:
else block with the number 38
if else Chain
Since the “if” scope can contain any code, it must contain one or more if or else logics. As in the example below, the first thing to check with an if is whether the value of Q is 50. If not, then program control goes to another step where another if is checked to see whether the value of Q is 11. Otherwise, it is going to an else block related to it, and with an, if in it is re-checking whether the value of Q is 7 and since this condition is also false then the output printed on the screen is below,
Example:
Q = 38
if Q == 50:
print("The value is 50")
else:
if Q == 11:
print("The value is 11")
else:
if Q == 7:
print("The value is 7")
else:
print("The value isn't 5, 11 or 7")
print(f"It is {Q}")
Output:
The value isn't 5, 11 or 7
It is 38
The funny thing is that the chain of if-else if can be written with a little expression elif. The above program can also be written as below,
Example:
Q = 38
if Q == 50:
print("The value is 50")
elif Q == 11:
print("The value is 11")
elif Q == 7:
print("The value is 7")
else:
print("The value isn't 5, 11 or 7")
print(f"It is {Q}")
Output:
The value isn't 5, 11 or 7
It is 38
Ternary Operator in Conditional Statements
The natural meaning of the word “ternary” is threefold. Hearing its name, it is understood that this operator works with three arguments. Meanwhile, we already know about if and else. So, if, else, and with a value, these three things can be easily written as conditional expressions by implementing the concept of the operator.
Assuming, the value of P is assigned 10. Then I want to assign a value for Q. That could be 10 or 20. So, I put a condition to determine which one will actually be.
The condition is:- if (P >= 10 and P <20) i.e. if the value of a is greater than or equal to 10 and less than 20 then this condition will be true and then 20 will be assigned as the value of Q. If the condition is false, 30 will be assigned in Q. Exactly these are written in one line which is, in fact, an application of the ternary operator.
P = 10
Q = 20 if (P >= 10 and P < 20) else 30
print(Q)
Output:
20
Another example,
role = 1
data = "Admin" if role == 1 else "User"
print(data)
Output:
Admin
Further use of else
Others can be used with for and while loops, except if only. For example, when the work of a for loop ends normally, the code of the else block attached to it is executed. Let’s look at the example below,
item = 38
while(item < 48):
print(item)
item += 1
else:
print("Executed")
Output:
38
39
40
41
42
43
44
45
46
47
Executed