if statement in Python with examples

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:

ConditionsDescription
a > bGreater than
a < bLess than
a >= bGreater than or equal to
a <= bLess than or equal to
a == bEquals
a != bNot Equals
Logical Conditions

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

Leave a Comment

Your email address will not be published.