The if else statement in Python can be incredibly helpful when you need to perform certain actions based on certain conditions. But just like with anything else, there are right and wrong ways to do it. In this article, we’ll go over how and when to use the if else statement in Python, plus some examples of where you might use it. I highly recommend you get the “Python Crash Course Book” to learn Python.
Example 1: if else in Python
age = 70 if age < 60: print("Young Man") else: print("Old Man")
Output:
Old Man
Example 2:
marks = int(input("Enter Your Marks = ")) if marks < 33: print("Fail") else: print("Pass")
Output:
Enter Your Marks = 45 Pass
Example 3:
age = float(input("Enter Your Age = ")) if age < 18: print("You are kid") else: print("You are adult")
Output:
Enter Your Age = 22 You are adult
You can also find tutorials related to advance skills from AiHints.