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, the price of a sandwich order won’t appear if the customer orders a ham roll. Then, the code will be as follow

Python if else statement

The code return will be Price: $1.75

Let’s go into detail with this code. 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

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

Python if else statement

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

Python if else statement

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

Python if else statement

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, working with Python is not easy, especially when you’re a beginner. Therefore, to avoid unexpected problems and issues, we highly recommend you contact professional developers. In this case, you should contact ArrowHitech – one of the best outsourcing IT in Vietnam. Having more than 12 years of experience, we can help you handle difficulties in not only coding, but also in eCommerce. What are you waiting for? Contact us here or via email: support@arrowhitech.com or hotline + 84 243 7955 813

Tags

Share