Photo by Kevin Bhagat on Unsplash
The Elsceptional Odyssey: A Python's Adventure into the Depths of Nested If-Else
In Python, you can use nested if-else statements to create more complex decision-making logic. Nested if-else statements allow you to check multiple conditions within each other. Let's plunge into the depths of knowledge and find out what are nested if-else statements.
The basic syntax of the nested if-else statement is:
Remember to indent your code properly to define the nested structure. Each nested block should be indented one level deeper than its containing block.
Example 1
Type the below code into your editor.
In the above example, we first check if the variable "age" is greater than or equal to 18. If it is, the code block under if age >= 18 executes. Inside that block, we check if "age" is greater than or equal to 21. If it is, the code block under if age >= 21 executes. If not, the code block under else executes. If "age" is less than 18, the code block under the initial else statement executes. Your code window should give the below result:
Change the age variable to 1.
The result:
Since the variable age is 16, it doesn't satisfy the condition age >= 18, so the code block under the initial else statement executes, printing "You are minor."
Example 2
Type the below code into your editor.
In the above example, we have a variable num initialized to 10. First, we check if the num is greater than 0. If it is, we print that the number is positive. Inside the positive number condition, we further check if the number is even by using the modulo operator % to check if it's divisible by 2. To delve deeper into the modulo operator, I encourage you to explore my blog post titled "Mathematical Mischief: An In-depth Exploration of Python Arithmetic and the Intricacies of Number Manipulation."
If it is, we print that the number is even. If the number is not even, we conclude that it must be odd. If the initial condition (greater than 0) is not satisfied, we reach the else statement and print that the number is non-positive. Feel free to modify the conditions and statements as per your requirements or add more nested if-else blocks to create more complex logic.
The "output" window should look like the below:
Let's change the variable num to 15.
The "output" window should have the below result.
The initial condition num > 0 is true because 15 is greater than 0. The program enters the if block. It prints "The number is positive." Next, it evaluates the nested condition num % 2 == 0 to check if the number is even. Since 15 is not divisible by 2 (the remainder is 1), the condition num % 2 == 0 is false. Consequently, the program executes the else block inside the nested if statement. It prints "The number is odd."
Now, let's change the number to a negative.
The initial condition num > 0 is false because -8 is not greater than 0. The program goes to the else block. It prints "The number is non-positive." Since the number is non-positive, the program does not evaluate the nested if-else statements. Therefore, the output will be:
That was all for today. In the next episode of our programming sitcom, we'll dive into the captivating world of input functions and how they can make our Python programs dance to the beat of user commands. Then, we shall come back to nested if-else statements and see how they work together with input functions. Stay tuned and continue to dive into the pool of knowledge.