Tutorial

If else Python condition statement

7 min read

The If-else condition statement is a fundamental part of programming and is used to control the flow of a program. In Python, they are used to make decisions based on certain conditions. The syntax is simple and easy to understand, making it a great tool for beginners to learn. In this article, we will learn about the if-else statements in Python and use some examples to help us understand how to use them in our Python code.

The if-else statements in Python are of the following types:

  1. if statement
  2. if-else statement
  3. if-elif-else statement

Moreover, we will also be learning about the nested if statements in Python.

If Statements in Python

An if statement in Python is used to control the flow of a program based on certain conditions. It allows us to execute different code based on whether a certain condition is true or false.

The basic syntax of Python if statement is as follows-

if condition:
    # code to execute if condition is true

We can use this syntax to write an example for if statements.

Example of if statement in Python-

x = 15
if x > 10:
    print("x is greater than 10")

Here, the condition x > 10 is true, so the code inside the if block is executed and the output is “x is greater than 10“.

You can also use logical operators as well as comparison operators like and, or, and not,==, !=, <, >, <=, and >=.

Example of Python if statement with operators included-

x = 5
y = 10
if x > 2 and y < 15:
    print("x is greater than 2 and y is less than 15")

Here, the condition x > 2 and y < 15 is true, so the code inside the if block is executed and the output is “x is greater than 2 and y is less than 15“.

It is also possible to use if statements with lists, strings, or any other iterable data types by using the in keyword.

Example of if statement in a Python list-

fruits = ["apple", "banana", "orange"]
if "banana" in fruits:
    print("banana is in the list")

Here, the condition “banana” in fruits is true, so the code inside the if block is executed and the output is “banana is in the list“.

By using if statements along with logical and comparison operators, you can create powerful and flexible control structures for your Python programs.

Python if else statement

If-else statements in Python are used to make decisions in your program based on certain conditions. They allow you to control the flow of your program and execute different codes based on whether a condition is true or false.

The syntax for an if-else statement in Python is as follows:

if condition:
    # code to execute if condition is true
else:
    # code to execute if condition is false

We can use this syntax to write an example for if-else statements.

Example of Python if else with the help of a simple program-

x = 15
if x > 10:
    print("x is greater than 10")
else:
    print("x is not greater than 10")

Here, the condition x > 10 is true, so the code inside the if block is executed and the output is “x is greater than 10“.

We can also use if-else statements with logical operators like and, or, and not.

Example of if else block including operators-

x = 15
y = 20
if x > 10 and y > 15:
    print("x is greater than 10 and y is greater than 15")
else:
    print("condition is false")

Here, the condition x > 10 and y > 15 is true, so the code inside the if block is executed and the output is “x is greater than 10 and y is greater than 15“.

If-elif-else Statements in Python

We use the elif (short for “else if”) to check multiple conditions inside an if-else statement.

Example of Python elif statement-

x = 15
if x > 20:
    print("x is greater than 20")
elif x > 10:
    print("x is greater than 10 but not greater than 20")
else:
    print("x is not greater than 10")

Here, the first condition x > 20 is false, so the program checks the next condition x > 10, which is true, so the code inside the elif block is executed and the output is “x is greater than 10 but not greater than 20“.

Note: It is important to keep in mind that the code inside the if and else block should be indented by a tab or 4 spaces, otherwise, it will raise an indentation error.

Shorthand if else python or python one line if else

In Python, the ternary operator can be used to write a one-line if-else statement.

The basic syntax of the operator is as follows:

value_if_true if condition else value_if_false

Here, it evaluates to value_if_true if the condition is true and value_if_false if the condition is false.

Example of ternary operator with if else in Python-

x = 5
print("x is greater than 3") if x > 3 else print("x is less than or equal to 3")
# output: x is greater than 3

Multiple if else one line python

In Python, it is possible to write multiple if-else statements in one line using the ternary operator, by chaining multiple ternary operators together.

The basic syntax for chaining multiple ternary operators is as follows:

value_if_condition_1 if condition_1 else value_if_condition_2 if condition_2 else value_if_condition_3

Here, it will check each condition in order and return the corresponding value if the condition is true. If none of the conditions are true, the last value in the chain will be returned.

Example of Python multiple if else :

x = 5
print("x is 1") if x == 1 else print("x is 2") if x == 2 else print("x is not 1 or 2")
# output: x is not 1 or 2

Note: As the chain of the ternary operator becomes longer and more complex, it can become harder to read and understand. It is generally recommended to use multiple if-else statements in cases where the conditions are complex or multiple conditions are to be checked.

Nested if else in python

In Python, it is possible to write nested if-else statements, where one or more if-else statements are placed inside another if-else statement.

This allows for more complex decision-making, where multiple conditions are evaluated in succession.

The basic syntax for a nested if-else statement is as follows:

if condition_1:
    # code to execute if condition_1 is true
    if condition_2:
        # code to execute if condition_2 is true
    else:
        # code to execute if condition_2 is false
else:
    # code to execute if condition_1 is false

Example using the above-shown format:

x = 5
y = 8
if x > y:
    print("x is greater than y")
else:
    if x < y:
        print("x is less than y")
    else:
        print("x is equal to y")
# output: x is less than y

Here, the first if-else statement checks if x is greater than y. If it is then the message “x is greater than y” is printed. If it is not, the second if-else statement checks if x is less than y. If it is, the message “x is less than y” is printed. If it is not, the message “x is equal to y” is printed.

It is also possible to nest multiple if-else statements within an else block, which allows for even more complex decision-making.

However, it is important to keep in mind that as the number of nested if-else statements increases, the code can become more complex and harder to understand. So it is important to use nested if-else statements judiciously and to keep the code organized and readable.

Conclusion

In conclusion, if-else statements in Python are a powerful concept for controlling the flow of our program and making decisions based on certain conditions.

By using if, elif, and else statements, we can check multiple conditions and execute different codes based on whether those conditions are true or false.

If-else statements are very useful in a wide range of programming situations, including user input validation, data manipulation, and creating control structures for your code.

They can also be used with various data types, such as strings, lists, and dictionaries, making them a versatile and essential part of any Python programmer’s toolkit.

Hope you liked the tutorial, if you are interested in learning Python and need a Python tutor then you can contact us.