The if statement in Python gives you the power to make choices and take actions in your code based on whether or not something is true, using expressions of Boolean type. This works just like it does in English: if something is true, you do something; otherwise, you don’t do anything. I highly recommend you get the “Python Crash Course Book” to learn Python.
You can use logical conditions in the if statement. These logical conditions are:
Conditions | Description |
a > b | Greater than |
a < b | Less than |
a >= b | Greater than or equal to |
a <= b | Less than or equal to |
a == b | Equals |
a != b | Not Equals |
Example 1: Simple if statement in Python
a = 10 b = 5 if a > b: print("a is greater than b")
Output:
a is greater than b
Example 2
If you enter a number greater than 80, it will show the following output. If you enter marks less than or equal to 80, it will not show anything.
a = int(input("Enter Your Marks")) if a > 80: print("Your grade is A")
Output:
Your grade is A
Example 3
If you enter a number greater than 0, it will show the following output. If you enter a zero or negative number, it will not show anything.
c = int(input("Enter any number")) if c > 0: print("c is positive")
Output:
c is positive