Python if else

In this tutorial, we will learn about python if else statement like if statement python and python if-else statements, which will help users create decisions in a python program with different forms of if…elif…else statement.

Python if else

What is a python if statement?

Python if statements are used to implement decision-making statements in the Python programming language.

if statement syntax:

if <condition>:
<statement></statement></condition>

As shown in the above syntax:

  • First, we are declaring if statements.
  • We have to add the condition, and it is mandatory to add a semicolon: at the end of the condition.
  • Then we add a statement that will execute once the condition is True.
  • If the condition is evaluated as False, the statement under if statement will not execute.
python if statement

We can use operators to evaluate a condition or use other objects to assess the condition.

  • 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

The following code will only be executed when the if statement’s condition is evaluated as True.

Example:

X = 23
if X < 30:
print(“The number is less than”, X)
print(“END”)

Output:

The number is less than 23
END

Example:

X = 31
if X < 30:
print(“The number is less than”, X)
print(“END”)

Output:

END

As we can see here value is greater than the condition, and as a result, the condition is evaluated as False, and the statement below did not execute.

Example: Python if Statement

# If the number is positive, then print a message
num = 3
if num > 0:
print(num, “is a positive number.”)
print(“This is always printed.”)
num = -1
if num > 0:
print(num, “is a positive number.”)
print(“This is also always printed.”)

When we run the above program, we will get the following output:

3 is a positive number
This is always printed.
This is also always printed.

In this example, we have num > 0 as the test expression. So when the if condition is evaluated as True, only statements within the if condition will be executed.

When the variable num is equal to 3, the test expression is True, and statements inside the body are executed.

If the variable num is equal to -1, the test expression is false, and statements inside the body are skipped, and the print() statement will fall outside of the if block. Hence, it is executed regardless of test expression.

Python else if Statement

Syntax of python if-else statement.

if <condition>:
<statement of=“” if=“”>
else:
<statement of=“” else=“”></statement></statement></condition>

In the if-else in python, first, if condition will be evaluated and if the result is True, then statement of if will be executed, but if the condition is evaluated as false, then the else statement will be executed.

Python else if Statement

Let take an example of an if-else statement.

X = 34
if X < 30:
print(“The number is less than”, X)
else:
print(“The number is more than”, X)
print(“This Statement will always run.”)

Output:

The number is more than 34
This Statement will always run.

Python if…elif Statement

Python elif statement used to add multiple expressions just like else if statement.

In simple English language, we can quote, “if the previous conditions were not true, then try this condition.”

Syntax of elif python.

if <condition>:
<statement of=“” if=“”>
elif: <condition>:
<condition of=“” elif=“”></condition></condition></statement></condition>

Example of elif statement:

a = 33
b = 33
if b > a:
print(“b is greater than a”)
elif a == b:
print(“a and b are equal”)

The output of elif statement:

a and b are equal

In the above example, a variable is equal to variable b. The first condition is not validated, but the second elif condition is True, so get the output as  “a and b are equal.”

Example of elif statement:

Age = int(input(“Enter your Age ?”))
if Age < 18:
print(“You are not adult”)
elif Age > 18:
print(“You are adult”)

The output of elif statement:

Enter your Age? 24
You are adult

Let add if..elif..else together.

Here we will implement if..elif..else together in one program.

Syntax of if..elif.else

if <condition>:
<statement of=“” if=“”>
elif: <condition>:
<condition of=“” elif=“”>
else:
<statement of=“” else=“”></statement></condition></condition></statement></condition>
if..elif..else together

Example of if…elif…else

Age = int(input(“Enter your Age ?”))
if Age < 18:
print(“You are not adult”)
elif Age > 18:
print(“You are adult”)
else:
print(“Something went wrong”)

Output:

Enter your Age ? 11
You are not adult

Advance example of a conditional statement

Let us say we are given a time, and we have to tell what phase of the day it is- (morning, noon, afternoon, evening, or night). We have to check if the given time against multiple ranges of time is lies within each of the 5 phases. Therefore, the following conditions apply:

  1. Morning: 0600 to 1159
  2. Noon: 1200
  3. Afternoon: 1201 to 1700
  4. Evening: 1701 to 2000
  5. Night: 2000 to 0559
time = int(input(“Enter the Time:”))
if (time >= 600) and (time < 1200):
print (“Morning”);
elif (time == 1200):
print (“Noon”);
elif (time > 1200) and (time <= 1700):
print (“Afternoon”);
elif (time > 1700) and (time <= 2000):
print (“Evening”);
elif ((time > 2000) and (time < 2400)) or ((time >= 0) and (time < 600)):
print (“Night”);
else:
print (“Invalid time!”)

Output:

As we can see here, we are using and keyword and/or keyword, a logical operator, and combined conditional statements.

Python Nested if statements

In nested if statements, we can implement if statements within if statements.

Syntax:

if <condition>:
if <condition>:
<statement>
else <condition>:
<statement></statement></condition></statement></condition></condition>

Example of nested if statement

num = float(input(“Enter a number: “))
if num >= 0:
if num == 0:
print(“Zero”)
else:
print(“Positive number”)
else:
print(“Negative number”)

Output:

Enter a number: 25Output:
Positive number

In about example, we input a number to check if the number is positive or negative or zero. Then we print an appropriate message using a nested if statement.

One-Line if Statements

In one line or shorthand if statements, we can implement conditional statements in one line only.

Unlike usual if statements, they can be used in the same line. This technique is known as Ternary Operators or Conditional Expressions.

Syntax:

if<condition>: <statement></statement></condition>

and

if<condition>: <statement_1>;<statement_2>;………;<statement_n></statement_n></statement_2></statement_1></condition>

There can even be more than one on the same line, separated by semicolons:

Example of one-line statements

if a > b: print(“a is greater than b”)

Output:

 

Example of if…else one-line statements

a = 2
b = 330
print(“A”) if a > b else print(“B”)

Output:

As we can see in the above example in the if…else statement, we are not using a semicolon :

Let us see an example of a nested one-line statement.

Example of Nested One Line Statement

a = 330
b = 330
print(“A”) if a > b else print(“=”) if a == b else print(“B”)

Output:

=