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 Elif conditions



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.

  1. If the net worth of a company is higher than other firms, give it Rank 1.

  2. If the net worth of a company is higher than others but lower than the above firm, give it Rank 2.

  3. 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:

Example: 

mrx = 77 ample = 54 if mrx > ample: print("77 is larger than 54")

 

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:

Example: 

mrx = 10 ample = 7 if mrx < ample: print("No, 10 is not smaller than 7")

Indentation

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:

Example: 

mrx = 10 ample = 7 if mrx < ample: print("No, 10 is not smaller than 7") #You will receive an invalid message

Elif

When it comes to Python if else conditions, the elif keyword means “if the previous conditions aren’t true, try this condition”.

Example: 

mrx = 10 ample = 10 if mrx > ample: print("mrx is larger than ample") elif mrx == ample: print("The values of mrx and ample are the same")

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:

Example: 

mrx = 10 ample = 7 if mrx == ample: print("The values of mrx and ample are the same") elif mrx > ample: print("mrx is larger than ample")

elif example


Python Else Statement

In Python, the else statement catches everything that isn’t captured by the previous conditions.

Example: 

mrx = 7 ample = 10 if mrx == ample: print("The values of mrx and ample are the same") elif mrx > ample: print("mrx is larger than ample") else: print("ample is larger than mrx")

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:

Example: 

mrx = 10 ample = 10 if mrx == ample: print("ample is larger than mrx") else: print("The values of mrx and ample are the same")

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:

Example: 

mrx = 77 ample = 54 if mrx > ample : print("mrx is larger than ample")

Example: 

mrx = 10 ample = 10 if mrx > ample: print("mrx is larger than ample") elif mrx == ample: print("The values of mrx and ample are the same")

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:

Example: 

mrx = 7 ample = 10 print("Yes, mrx is larger than ample") if mrx > ample else print("No")

This above approach is known as Ternary Operators, or Conditional Expressions.

Example: 

mrx = 10 ample = 10 print("The values of mrx and ample are the same") if mrx == ample else print("No")

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:

Example: 

mrx = 10 ample = 10 print("mrx = ample") if mrx == ample else print("mrx") if mrx > ample else print("ample")

Example:2

mrx = 7 ample = 10 print("mrx") if mrx > ample else print("ample") if ample > mrx else print("mrx = ample")

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:

Example: 

mrx_1 = 95 mrx_2 = 110 mrx_3 = 70 if mrx_1 < mrx_2 and mrx_1 > mrx_3: print("Statement is true for both conditions")

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:

Example: 

mrx_1 = 70 mrx_2 = 110 mrx_3 = 70 if mrx_1 < mrx_2 and mrx_1 == mrx_3 and mrx_2 > mrx_3: print("Statement is true for both 3 conditions")

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:

Example: 

mrx_1 = 95 mrx_2 = 110 mrx_3 = 70 if mrx_1 < mrx_2 or mrx_1 == mrx_3: print("Minimum single of the conditions is True")

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:

Example: 

mrx_1 = 70 mrx_2 = 110 mrx_3 = 70 if mrx_1 > mrx_2 and mrx_1 == mrx_3 and mrx_2 < mrx_3: print("Minimum single of the conditions is True")

Nested If

Nested if statements contain if statements within if statements.

Example: 

mrx = 10 if mrx > 5: print("Greater than five,") if mrx > 8: print("More than 8 as well") else: print("but not greater than 8")

Example: 

mrx = 25 if mrx < 36: print("Smaller than 36,") if mrx < 27: print("Less than 27 as well") else: print("but not smaller than 27")

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:

Example: 

mrx = 77 ample = 54 if mrx > ample: pass

Example: 

mrx_1 = 95 mrx_2 = 110 mrx_3 = 70 if mrx_1 < mrx_2 and mrx_1 > mrx_3: pass

Python If Else Importance

Following are the importance of using if else with in a python program:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
Your feedback is highly appreciated. Please take a moment to share your reactions for the betterment of this site.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *