Python If-else statements

In this tutorial, we will learn about If-else Statements in Python and we will also cover different examples related to Python If-else statements and, we will cover these topics.

  • Python If-else statements
  • Indentation in Python
  • The if statement
  • The if-else statement
  • The elif statement

Python If-else statements

In python or any other language, Decision Making is a very important feature. Decision-making helps to run the specific block of code on a particular decision. Conditions are very important and play the role of backbone in decision making. We can perform the decision-making by using the following Statements.

StatementsDescription
If- StatementIt is used to run the specific block and if the condition is true it is only executed at that time only.
If-else StatementThis condition is almost the same as the if statement but in this, it also provides us the block for the false condition where it checks that if the condition is true it will run that block only but if the condition is false then it will run the else block.
Nested-If StatementIn Nested- if statement is the if statement that targets another if statement inside the if statement. Python allows us to use nested if inside the if statement.

Indentation in Python

In Python, it doesn’t allow the use of the parentheses in the block-level code. It uses the indentation to declare the block. Basically, it refers to the spaces and tabs that we used at the beginning of the statement. If two statements are inside the same indentation then it is the same block of code.

In python, the indentation takes four spaces to indent the statements. We will further see how the indentation is used in decision-making statements with different python examples.

The if statement

The if statement is used as the most simple decision-making statement. It is used to run the specific block and if the condition is true it is only executed at that time only. The condition of the if statement is only valid when the logical expression is either appraise as true or false.

Flow Chart:

Python If-else statements
Python If Statement

Syntax Code:

if expression:  
    statement  

Example:

Code:

In the following code, we are showing the example of the If condition.

number = int(input("enter the number?"))  
if number%2 == 0:  
    print("Number is even") 

Output:

After running the following code we get the following output where it checks the number is even using the if condition.

If statement Example
If statement Example

Example 1.0:

Code:
In the following code, we are checking the value with the largest number.

p = int(input("Enter p? "));  
y = int(input("Enter y? "));  
t = int(input("Enter t? "));  
if p>y and p>t:  
    print("p is largest");  
if y>p and y>t:  
    print("y is largest");  
if t>p and t>y:  
    print("t is largest"); 

Output:

After running the following code we get the following output where it prints the largest value after comparing from the other assigned values.

Python If Statement
If statement

The if-else statement

The If- else statement is almost the same as the if statement but in this, it also provides us the block for the false condition where it checks that if the condition is true it will run that if block only but if the condition is false then it will run the else block.

Flow Chart:

Python If else Statement
Python If-else Statement
Python If-else Statement

Syntax Code:

if condition:  
    #block of statements   
else:   
    #another block of statements (else-block)   

Example: WAP to check whether a person is eligible to vote or not.

Code:

In the following code, we are checking the eligibility of a person to vote where it uses the condition if py_age>=18 if it is true then it will run the if block but on another, side if age is less than 18 then it will run the else block.

py_age = int (input("Enter your age? "))  
if py_age>=18:  
    print("You are eligible to vote !!");  
else:  
    print("Sorry Your are not eligible"); 

Output:

After running the following code we get the following output we can see a person who is 17 is unable to vote but the person who is 24 is eligible to code.

Python If else Statement
Python If else Statement

Example: WAP to check whether a number is even or not.

Code:

In the following code, We are checking whether the number is even or odd. Where it matches the condition if the number is divisible by 2 then it is an even number but if the number is not divisible by 2 then it is an odd number.

number = int(input("enter the number?"))  
if number%2 == 0:  
    print("Number is even...")  
else:  
    print("Number is odd...")  

Output:

After running the following code we get the following output where it matches the condition if the number is divisible by 2 then it is an even number but if the number is not divisible by 2 then it is an odd number.

Python if else statement
Python if-else statement

The elif statement

In Nested- if statement is the if statement that targets another if statement inside the if statement. Python allows us to use nested if inside the if statement.

Flow Chart:

Nested If Statement
Nested If Statement

Example Code:

In the following code, we are finding the equal number using the elif condition.

num = int(input("Enter the num?"))  
if num==10:  
    print("num is equals to 10")  
elif num==50:  
    print("num is equal to 50");  
elif num==100:  
    print("num is equal to 100");  
else:  
    print("num is not equal to 10, 50 or 100"); 

Output:

After running the following code we get the following output where it checks the number if it is equal to 10,50, and 100 then it will print the value with the num is equal to 50 if no same value is assigned then it will print the number is not equal.

Nested If Example
Nested If Example

Example 2:

In the following code, we are giving the grade to students according to marks scored in exam subjects using the elif condition.

score = int(input("Enter the score? "))  
if score > 85 and score <= 100:  
   print("Congrats ! you scored grade A ...")  
elif score > 60 and score <= 85:  
   print("You scored grade B + ...")  
elif score > 40 and score <= 60:  
   print("You scored grade B ...")  
elif (score > 30 and score <= 40):  
   print("You scored grade C ...")  
else:  
   print("Sorry you are fail ?")  

Output:

After running the following code we get the following output where we can see the two different outputs where one student who scores marks less than 40 is failed and having marks between 85-100 is having the grade A.

elif example
Elif example

So, in this tutorial, we discussed Python If-else statements and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python If-else statements
  • Indentation in Python
  • The if statement
  • The if-else statement
  • The elif statement

Do follow the following tutorials also:

Comments are closed.