Python if else statement: The comprehensive guide

When writing a program, you might want a block of code to run only when a condition is met. Then, in case of Python, if and if – else statements help to run conditional operations. In this guideline, we will provide you with comprehensive and detailed tutorials about Python if else statement as well as if statement

Python if statement

If statement can help you to test a specific condition. For example, if the condition is TRUE, the block of code, or if – block will be executed. On the other hand, supposed the condition equals to false, the block in the if statement won’t run

To understand further about Python if statement, let’s consider this following example. In this case, a customer orders a ham sandwich. The price of a sandwich order won’t appear if the customer orders a ham roll. Then, the code will be as follow

The code return will be Price: $1.75

Let’s go into detail with this code of this example a customer orders a ham sandwich. We create a variable named “ sandwich_order” with the value “ Ham Roll”

After that, we use if – statement to check if the sandwich_order is equal to Ham Roll. Suppose this condition is met, the print () statement will be executed. Otherwise, nothing will happen with this case of a customer orders a ham sandwich

Python if – else statement

Python if – else statement is quite similar to if – statement. However, with Python if statements, the block of code only be executed if the condition is met. Otherwise, nothing happens. On the other hand, with Python if – else, if the condition equals FALSE, a block of code ( or the else statement) will be executed.

Now, let’s consider this following example. Using the same “restaurant” example, but now you  want to build an app to check if a customer runs up a tab at your restaurant. In case the customers run a tab over $20, then, they have to pay off it first to order more food. If the customers don’t have any tab over $20, nothing will happen

To do this, we use the following code

Let’s go into detail with this code. We create a Python variable named “ tab” to track customers’ tab. Then, we use if – statement to check if the tab is over $20 or not. Supposed the tab is more than 20, then the Print() statement right after the if statement will be executed.

On the other hand, if the tab is lower than 20, the Print() statement after else – clause will be executed

Python elif statement

Now we’ve gone through the Python if – else statement. Let’s move to another statement – Python elif statement. This statement will help you evaluate many conditions as well as outcomes for each one. Thanks to that, you’ll be able to create as many elif statements as you want.

To understand this statement further, let’s see the example below

With the same “ restaurant” example, you want to offer customer four sandwich fillings:

  • Ham Roll: $1.75
  • Cheese Roll: $1.80
  • Bacon Roll: $2.10
  • Other Filled Roll: $2.00

In case you want to calculate the customers’ orders, then you can use the following code

The code returns: Price: $2.10

In this case, we use if – statement to check a specific condition, two elif blocks to test for alternative condition, and else – statement in case no condition are met.

With this codes, here are the possible outcomes

  • If customers order a ham roll, the if- statement will be executed. Then, it will print “ Price: $1.75” to the console
  • In case customer orders a cheese roll, then elif – statement will be executed. It will print “ Price: $1.80” to the console
  • Just like that, if customer orders a Bacon Roll, the elif statement will print “ Price: $2.10” to the console
  • Finally, if customer orders different filling, we will display “ Price: $2.00” on the console

If you have more options, you can add more elis statement as you want. 

Python Nested If Statements

This statement allows you to use Python another if statement inside and outer if statement. Simply speaking, it’s an if statement inside. This type of statement will help you check if a condition is met only after another condition is met

Now, let’s look into this example to understand further about Python Nested If Statement. Coming back to the restaurant example. You want to check the price of customer’s orders only after checking if the customer has ordered some specific rolls. Then, you can use the following code

Our code returns: Price: $2.00

Let’s break down this code. Firstly, the program checks whether or not the sandwich order is equal to Other Fill Roll. Then, it will compare the ordered sandwich to the list of sandwiches on the menu. In case customers order a fill roll that is in the menu, the else statement will be executed

Now, suppose that you order Ham Roll filling. Then, the first if statement will be evaluated to be True since Ham Roll is not equal to Other Filled Roll. Our order will be compared to the list of sandwiches that we have specified

In conclusion

Above are some basic knowledge and understanding about Python if else statement, if statement as well as nested if statement. However, in case you want to effectively work with Python, and avoid unexpected problems, you’d better contact experts and professional developers. In this case, we highly recommend AHT TechMobile app development services to fulfil all your requirements. Contact us here!

Tags

Share
AHT Logo

Contact Us

Thank you for your message. It has been sent.