Python Conditions If Else
The purpose of this post is to demonstrate Python if else, Elif and conditions with examples in order to achieve the learning objectives.
Python if else Statement
Python If else statement allow us to run block code only if a particular condition is True or False.
Assigning ranks (1, 2, 3) according to the net worth of a company, for example.
If the net worth of a company is higher than other firms, give it Rank 1.
If the net worth of a company is higher than others but lower than the above firm, give it Rank 2.
When the net worth of a company is lower than above two and higher than rest, give it Rank 3.
A Python if else statement can be expressed in a number of different ways. Let’s take a closer look at each of them one by one.
Python If statements
When it comes to Python If statements, Python supports the common logical conditions from mathematics:
- Equals: mrx == ample
- Not Equals: mrx != ample
- Less than: mrx < ample
- Less than or equal to: mrx <= ample
- Greater than: mrx > ample
- Greater than or equal to: mrx >= ample
There are several ways to use these conditions, but “if statements” and loops are the most common.
The if keyword is applied to write an “if statement”.
check the number 77 is greater than 54 by if statement:
In the above example, we set two variables, mrx and ample, to test whether mrx is greater than ample as part of the if statement.
We know that 77 is greater than 54, since mrx is 77, and ample is 54, so we print to screen that “77 is greater than 54 ”.
Below is an incorrect if example.
Check the number 10 is smaller than 7 by if statement:
When it comes to Python if else, Python relies on indentation (whitespace at the beginning of a line) to define scope. In other programming languages, curly brackets are commonly implemented.
An error will occur if the If statement is not indented:
When it comes to Python if else conditions, the elif keyword means “if the previous conditions aren’t true, try this condition”.
In the above example mrx is equal to ample, so the first condition doesn’t hold, but the elif condition does, so we display to screen “The values of mrx and ample are the same”.
here is another example:
Python Else Statement
In the above example, ample exceeds mrx, so first condition and elif condition do not hold, therefore, we go to else, and print out “ample is greater than mrx ”.
It is also possible to have an else without the elif condition:
Short Hand If
If you want only a single statement to run, you can place it on a similar line as the if statement.
Single line if statement:
Short Hand If Else
Put all the statements on the same line if there is only one statement to execute, an if statement and an else statement.
Single line if else statement:
This above approach is known as Ternary Operators, or Conditional Expressions.
In Short Hand If … Else the same line can also contain more than one else statement.
A single row if else statement, with 3 conditions:
Python And Statement
In Python if else conditions, the and keyword is utilised as a logical operator.
Check if mrx_2 is larger than mrx_1, and if mrx_1 is larger than mrx_3:
Check if mrx_2 is larger than mrx_1, and if mrx_1 is equal to mrx_3, and mrx_2 is larger than mrx_3:
Python Or Statement
In Python if else conditions, the or statement is utilised as a logical operator.
Check if mrx_2 is larger than mrx_1, or if mrx_1 is equal to mrx_3:
Check if mrx_1 is larger than mrx_2, and if mrx_1 is equal to mrx_3, and mrx_3 is larger than mrx_2:
Nested if statements contain if statements within if statements.
The pass Statement
In Python if else – You cannot have an empty if statement, but if you have one that does not have any content, add a pass statement to avoid unwanted messages.
In this case, an empty if statement would throw an error if it omitted the pass statement:
Python If Else Importance
Following are the importance of using if else with in a python program:
- The if-else statement is a fundamental construct in Python that allows you to make decisions based on certain conditions. It helps your code adapt and respond differently based on the evaluation of these conditions, enabling you to create more dynamic and interactive programs.
- The if-else statement allows you to control the flow of execution in your program. By using conditions and logical expressions, you can specify which blocks of code should be executed under different circumstances. This control over code execution is crucial for achieving the desired behavior and producing accurate results.
- If-else statements are often used to handle errors and exceptions in Python. By checking for specific error conditions using if statements, you can implement error handling mechanisms and provide alternative paths or messages to handle unexpected situations gracefully. This helps improve the robustness and reliability of your code.
- With if-else statements, you can tailor the behavior of your program to specific requirements or user inputs. By evaluating conditions, you can define different outcomes or actions based on different scenarios. This customization capability allows your program to adapt and respond flexibly to various situations, making it more versatile and user-friendly.
- If-else statements are essential for implementing various algorithms and solving problems in Python. They are commonly used in loops, recursion, and other control flow structures to create efficient algorithms that handle different cases and conditions. Whether it’s sorting data, searching for specific elements, or implementing complex logic, if-else statements play a vital role in algorithm design and implementation.