PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  • Indentation:
  • if-else statement:
  • if-elif statement:
  • Nested if:
  • Assignment:
  1. Python For Data Analytics
  2. 1.Python
  3. 1.Python Documents

7.Conditional Statements In Python

Previous6.Strings In PythonNext8.Branching using Conditional Statements and Loops in Python

Last updated 2 years ago

What are Conditional Statements?

We have often used conditional statements in our daily life, such as :

if it rains ,i will go to office.

Syntax:

In [ ]:

if expression:  
    statement  

Above Figure Shows , Actions or decisions based on the Condition. If the condition is True, i will not go to office. Here are some Examples for if statement:

How to Find if a number is even?

In [2]:

number = int(input("Enter the number:"))

if(number%2==0):
    print(f'{number} is an even number')
    print(f'{number} is not an odd number')

print("Program ended")
Enter the number:10
10 is an even number
10 is not an odd number
Program ended

Indentation:

Python indentation is a way of telling python intrepreter that the group of statements/code/instrtuctions belong to a particular block.

In more simpler terms , indentation is nothing but a bunch of spaces or tabs signifying a particular line of code belongs to a same group.

Now You saw what will happen if the condition is true in above case. All the code with same indentation will run. But what will happen if the condition is False?

In [3]:

number = int(input("Enter the number:"))

if(number%2==0):
    print(f'{number} is an even number')
    print(f'{number} is not an odd number')

print("Program ended")
Enter the number:15
Program ended

So, Here the codes with same indentation did not run, it shows that if condition is True, Body of if will run. Body of if (Codes written under if statement by using indentation.), if Condition is False, Body of if will not run.

Note:We generally use Tab to define indentations. One Tab means 4 spaces.

if-else statement:

The if-else statement is similar to if statement except the fact that, it also provides the block of the code for the false case of the condition to be checked.

If the condition provided in the if statement is false, then the else statement will be executed.

For Example : . if coffee house is open i'll go there else i will be at home.

In [ ]:

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

To find if a number is even or odd:

In [2]:

number = int(input("Enter the number:"))

if(number%2==0):
    print(f'{number} is an even number')
else:
    print(f'{number} is an odd number')
Enter the number:10
10 is an even number

In [3]:

number = int(input("Enter the number:"))

if(number%2==0):
    print(f'{number} is an even number')
else:
    print(f'{number} is an odd number')
Enter the number:15
15 is an odd number

if-elif statement:

The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon what condition is True.

Syntax:

In [ ]:

if expression 1:   
    # block of statements   
  
elif expression 2:   
    # block of statements   
  
elif expression 3:   
    # block of statements   
  
else:   
    # block of statements 

So , elif - statement , in more simpler terms runs body of first if or elif statement with true condition and if any of the condition is not true , body of else executes.

For Example:

Program to find grade of a student based on marks:

In [1]:

marks = int(input("Please enter the marks:")) 

if(marks>90):
    grade = "A"
elif(marks>70):
    grade="B"
elif(marks>50):
    grade = "C"
elif(marks>30):
    grade = "D"
else:
    grade = "F"

print(f"your grade : {grade}")
Please enter the marks:40
your grade : D

Nested if:

There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if construct.

In more simpler terms, Nested if is actually if statement under an if statement.

Syntax:

In [ ]:

if expression 1:   
    # block of statements   
  
    if expression 2:   
        # block of statements   
  
        if expression 3:   
            # block of statements   

Program to find Largest Number among three numbers:

In [1]:

a = int(input("enter the number:"))
b = int(input("enter the number:"))
c = int(input("enter the number:"))

if(a>b):
    if(a>c):
        large = a
    else:
        large = c
else:
    if(b>c):
        large = b
    else:
        large = c

print("Largest Number is: ", c)
enter the number:5
enter the number:10
enter the number:15
Largest Number is:  15

Note:Run above code in your editor and change values to see , how this code is working.

Assignment:

  • 1.A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years. Ask user for their salary and year of service and print the net bonus amount.

  • 2.A school has following rules for grading system: a. Below 25 - F b. 25 to 45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A Ask user to enter marks and print the corresponding grade.

  • 3.A 4 digit number is entered through keyboard. Write a program to print a new number with digits reversed as of orignal one. E.g.- INPUT : 1234 OUTPUT : 4321 INPUT : 5982 OUTPUT : 2895

  • 4.Write a program to check if a year is leap year or not. If a year is divisible by 4 then it is leap year but if the year is century year like 2000, 1900, 2100 then it must be divisible by 400.