python conditional statement

Table of contents

  1. Conditional statements in Python
    1. if statement
    2. if-else statement
    3. elif statement
    4. Nested statements
  2. One-line if statements
  3. Summary

Conditional Statements in Python

The most important part of any programming language is decision-making. It is necessary when we want to run code under certain conditions that must be met for the code to run successfully. In this tutorial, we will learn about conditional statements, why we need them, and when to use them. We’ll also look at the different uses of conditional operators. In Python, we can achieve decision making with the following operators:
  • if
  • if-else
  • elif
  • nested if
Let’s look at each of them individually.

“IF” Statement

The simplest type of decision-making statement is an “if” statement. It takes an expression and checks if it evaluates to be True, if it does, the “if“ statement’s block of code is successfully executed. Syntax of “if” statement:

if ( expression ):
  Statement 1
  Statement 2
  ...
  Statement n
intro_python_condition
Now, let’s practice what we’ve learned:

# Assigning a value 3 to the variable num
num = 3

# Starting 'if body' to check if num is less than 0
if num < 0:
    
    # Printing this message if the condition is met
    print(num, "is a negative number.")
    
# Printing this message if the condition is not met
print(num, "is a positive number.")

Output:
3 is a positive number.
  In the output above, we can see that our requirement wasn’t met because 3 is not less than 0, therefore “if” body’s code was ended and it executed the statement after “if” body.

“IF-ELSE” Statement

The “if-else” statement executes both the True and False parts of a condition. The “if” block code is run if the condition is True, and the “else” block is executed if the condition is False. The “else” block will only be executed when the condition becomes False. Syntax of “if-else” statement:
if( expression ):
  Statement
else:
  Statement
if_else_image
Let’s rewrite our code above by adding else statement there:

# Assigning a value 5 to the variable num
num = 5

# Starting 'if body' to check if num is less than 0
if num < 0:
    
    # Printing this message if the condition is met
    print(num, "is a negative number.")

# Starting 'else body' to check if num is greater than 0
else:
    # Printing this message if the condition is met
    print(num, "is a positive number.")
Output:
5 is a positive number.
  In the code above we can see that in the body of “if” statement 5 was checked to be less than 0 which is False. Then 5 was checked by the body of “else” to be more than 0, which is True, so it printed “5 is a positive number.”

“ELIF” Statement

The “elif” operator is used to verify several criteria, only when the provided condition is false. It’s identical to the “if-else” statement, with the exception that “else” doesn’t check the condition, whereas “elif” does. Syntax of “elif” statement:

if( expression1 ):
  statement
elif ( expression2 ) :
  statement
elif( expression3 ):
  statement
…
else:
  statement
Elif Statement
Let’s use if-elif-else statements to see how it works:

# Assigning a value 10.9 to the variable num
num = 10.9

# Using if to check if num is greater than 0
if num > 0:
    
    # Printing this message if num more than 0
    print(num,"is a positive number")

# Using elif to check if num is equal to 0
elif num == 0:
    
    # Printing this message if num is 0
    print(num,"is zero")

# Using else to check if num is num less than 0
else:
    print(num,"is a negative number")
Output:
10.9 is a positive number
  In this program we’ve checked if 10.9 is positive, negative or equal to zero. According to it, the assigned message was printed.

“NESTED IF” Statements

Any if statement can be found inside another if statement, which is known as nested if statements. When we have to filter variables numerous times we use nesting. We can use the concept of if, if-else, and even if-elif-else statements combined to form a more complex structure.
nested_loop
See example of using nested if statements below:

# Assigning a value 10 to the variable num
num = 10

# Checking if num is less than 20
if num < 20:
    print(num, "is less than 20")
    
    # if inside another if
    if num == 15:
        print (num,"is 15")
    elif num == 10:
        print(num,"is 10")
    elif num == 5:
        print(num,"is 5")
    elif num < 5:
        print(num,"is less than 5")
Output:
10 is less than 20 10 is 10
 

One-Line If Statements

We can write “if,” “if-else,” and “elif” statements on the same line in Python without worrying about indentation. Syntax of if statement in one line:

if (condition): statement 1; statement 2; statement 3;…;statement n
Let’s see the example of the code using if statements in one line below:
num=15

# Using if in one line
if num > 14.9 : print (num,"is greater than 14.9")
Output:
15 is greater than 14.9
  These is the example of using if-else statement in one line:

a = 10
b = 10.1

# Using if-else in one line
print(a,"is greater than",b) if a > b else print(a," is equal to",b) if a == b else print(b,"is greater than",a)
Output:
10.1 is greater than 10
 

Summary

In this tutorial we learned what decision making is, using different if statements. We also learned how to use these conditional statements within other statements, and how to write them in one line of code.
Leave a Reply