Python if … statement

Python if statement is considered very similar to the other languages. Hence, if statements have a logical expression through which the data is compared and the decision is mostly made out of the comparison. Read the article to know more about Python… if statement.

What are Python Conditions and if statements

Some of the conditions that Python supports are given below:

  • Equals: a == b
  • Not Equals: a != b
  • Less than a < b
  • Less than or equal to a <= b
  • Greater than: a > b
  • Greater than or equal to a >= b

However, these conditions will be defined in several ways. The most commonly used ways are ”if statements” and loops.

Whereas, the if statement will be written by using the if keyword.

# Example 1: Basic if statement
x = 10

if x > 0:
    print("x is positive")

output

x is positive

What is Indentation?

Python will depend on the indentation to tell the scope of the code. Whereas, several other programming languages will use the curly brackets for this.

Example

# Example: Incorrect usage of if statement without proper indentation
x = 5

if x > 0:
print("x is positive")  # This line is not indented, and it will cause an IndentationError

Output

x is positive

What is Elif?

The elif keyword is looked upon as the Python’s saying in other words ”if the previous conditions were not true, then try this condition.

Example

# Example: Corrected if statement with proper indentation
x = 5

if x > 0:
    print("x is positive")  # This line is properly indented

Output

x is positive

What is Else ?

In Python, we use the else keyword with an if statement to handle situations that aren’t covered by the conditions in the ‘if’ and elif statements.

# Imagine we have a number 'x'
x = 7

# Check if 'x' is even or odd
if x % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

Output

The number is odd.

What is Short Hand If?

In Python, if there is only one statement to execute, then it is required to put it on the same line as the if statement.

# Imagine we have a number 'x'
x = 10

# Shorthand if-else
result = "Even" if x % 2 == 0 else "Odd"
print(result)

Output

Even

What is Short Hand if…Else?

In Python, if there is only one statement to run, for if and else statement. Hence, it is possible to put it on the same line.

# Imagine we have a number 'x'
x = 15

# Shorthand if-else
result = "Even" if x % 2 == 0 else "Odd"
print(result)

Output

odd

What Is And?

The and keyword is considered as a logical operator and is used to merge the conditional statements.

Example

# Imagine we have two conditions related to a number 'x'
x = 8

# Using 'and' statement
if x > 0 and x % 2 == 0:
    print("x is a positive even number.")
else:
    print("x is either not positive or not even.")

Output

x is a positive even number.

What is Or?

The Or keyword is referred to as a logical operator and will be used to merge conditional statements.

# Imagine we have two conditions related to a number 'y'
y = -3

# Using 'or' statement
if y < 0 or y % 2 == 0:
    print("y is either negative or an even number.")
else:
    print("y is neither negative nor an even number.")

Output

y is either negative or an even number.

What is Not ?

The not keyword is referred to as a logical operator and will reverse the result of the conditional statements.

# Imagine we have a boolean variable 'is_sunny'
is_sunny = False

# Using 'not' keyword
if not is_sunny:
    print("It's not sunny today.")
else:
    print("It's a sunny day!")

Output

It's not sunny today.

What is Nested if ?

The nested if statements are referred to as the if statements will be kept inside the if statements.

# Imagine we have a number 'num'
num = 15

# Nested if statements
if num > 0:
    print("num is positive.")
    
    if num % 2 == 0:
        print("num is also even.")
    else:
        print("num is odd.")
else:
    print("num is not positive.")

Output

num is positive.
num is odd.

What is the Pass Statement

The if statements cannot be put as empty, whereas if the if statement is kept without any content, then it is necessary to pass the statements to ignore the error.

Conclusion

In Python, nested if statements provide a way to make more intricate decisions in our programs. By placing one if statement inside another, we can create a hierarchy of conditions.

Hence, this allows us to check multiple criteria and execute different code blocks based on those conditions. It’s like making decisions within decisions.

Python if … statement – FAQs

Q1. What is a Python if statement?

Ans. The if statement will let us run a block code if the certain condition is true.

Q2. What is the syntax of an if statement?

Ans. The syntax for an if statement is the if keyword followed by a Boolean expression enclosed in parentheses

Q3.What is a simple if statement in C?

Ans. The if in C is a decision-making statement that is used to execute a block of code based on the value of the given expression

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment