MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python If Else & Elif Conditional


Our compiler will execute the if statement to check whether it is true or false now; if it’s true, the compiler will execute the code in the “if” section of the program and skip the bunch of code written in “elif” and “else.” But if the “if” condition is false, then the compiler will move towards the elif section and keep on running the code until it finds a true statement(there could be multiple elif statements). If this does not happen, then it will execute the code written in the “else” part of the program.

An “if” statement is a must because without an if, we cannot apply “else” or “else-if” statements. On the other hand else or else if statement is not necessary because if we have to check between only two conditions, we use only “if and else” and even though if we require code to run only when the statement returns true and do nothing if it returns false then an else statement is not required at all.

Now Let’s talk about some technical issues related to the working of decision statements:

There is no limit to the number of conditions that we could use in our program. We can apply as many elif statements as we want, but we can only use one “else” and one “if” statement.
We can use nested if statements, i.e., if statement within an if statement. It is quite helpful in many cases.

Decision statements can be written using logical conditions, which are:

Equal to
Not equal to
Less than
Greater than
Greater than equal to
Less than equal to

We can also use Boolean or our custom-made conditions too.

Example


print("What is your age?")
age = int(input())
if age<18:
    print("You cannot drive")

elif age==18:
    print("We will think about you")

else:
    print("You can drive")

Output

What is your age?
45
You can drive


Conclusion

In this page (written and validated by ) you learned about Python If Else & Elif Conditional . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python For Loop.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.