Python Basics Tutorial 9 – Python elif Statements

Notes from the video Python Course Singapore | Learn Python Programming | Tutorial 9 – Python elif Statements’:

 

In the previous tutorial, we learnt about if statements and else statements.

elif is short for else if and is used between the if and else statements. You can include as many elif statements as you wish.

You can think of if, elif, and else as a series of tests.

If one of test is true. then the other tests below it won’t run.

For example, if the if-test was true, then the elif and else tests won’t run.

Or if the if-test was not true, and the first elif-test was true, then the other elif-tests below it and the else-test won’t run.

Let’s look at an example:

If marks == 100:

          print(“full marks”)

elif marks > 49:

              print(“pass”)

else:

              print(“fail”)

 

Python displays full marks if marks was equal to 100. The elif and else tests won’t run.

If marks was equal to 50, it would fail the if-test, and pass the elif-test.
Python would execute the print() function and display pass and would not run the else test.

If marks was equal to 40, it would fail the if-test, and the elif-test and pass the else-test.
Python would execute the print() function and display fail.

Get FREE LESSONS and TIPS on how YOU can excel at programming and business

Awesome! Thank you for subscribing!