Python for Data Science: Chapter 1: Basics of Python

Python: Flow Control Statements

There are various types of conditional statements - i) if statements ii) if‒else or alternate statements iii) Nested if iv) Chained conditionals

Flow Control Statements

There are various types of conditional statements ‒

i) if statements

ii) if‒else or alternate statements

iii) Nested if

iv) Chained conditionals


1. if Statement

The  if statement is used to test a particular condition. If the condition is true then it executes the block of statements which is called as if block.


The if statement is the simplest form of the conditional statement.

Syntax :

if condition:

statement

Example

if a<10:

print (" The number is less than 10" )

Example:1

Write a python program to check whether a given number is even or odd.

Solution:

In [2]: print("Enter value of n: ")

n = int(input())

Enter value of n:

10

In [3]: if(n%2==0):

print("Even Number")

if(n%2==1):

print("Odd Number")

Even Number



2. Alternative Statements

The if‒else statement provides an else block combined with the if statement which is executed in the false case of the condition. The flowchart for if‒else is –


Syntax

if condition:

         statement

else:

         statement

If the condition is true, then the if‒block is executed. Otherwise, the else‒block is executed.

ifelseDemo.py

print("Enter value of n")

n=int(input())

if n%2==0:

print("Even Number")

else:

print("Odd Number")

Output

In [6]: import ifelseDemo

Enter value of n

11

Odd Number



3. Nested if Statements

When one if condition is present inside another if then it is called nested if conditions. Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting.

Example:2

Write a python program to compare two numbers using nested conditionals.

Solution :

NestedIfDemo.py

print("Enter value of a")

a=int(input())

print("Enter value of b")

b=int(input())

if a= =b:

    print("Both the numbers are equal")

//Nested if‒else

else:

    if a<b:

         print("a is less than b")

else:

      if a>b:

          print("a is greater than b")

Output

Enter value of a

20

Enter value of b

10

a is greater than b

 

4. Chained Conditionals

Sometimes there are more than two possibilities. These possibilities can be expressed using chained conditions. The syntax for this is as follows ‒

if condition:

Statement

elif condition:

Statement

else:

Statement

The chained conditional execution will be such that each condition is checked in order. The elif is basically an abbreviation of else if.

There is no limit on the number of elif statements.

If there is else clause then it should be at the end.


In chained execution, each condition is checked in order and if one of the conditions is true then corresponding branch runs and then the statement ends. In this case if there are any remaining conditions then those conditions won't be tested.

Example:3

Write a python program to display the result such as distinction, first class, second class, pass or fail based on the marks entered by the user.

Solution :

print("Enter your marks")

m=int(input())

if m >= 75:

    print("Grade : Distinction")

elif m>= 60:

    print("Grade: First Class")

elif m>= 50:

    print("Grade: Second Class")

elif m>= 40:

    print("Grade: Pass Class")

else:

    print("Grade: Fail")

Output

Enter your marks

45

Grade: Pass Class

Example:4

Write a python program to find the largest among the three numbers.

Solution :

In [7]:

x = int(input("Enter first number"))

y= int(input("Enter second number"))

z = int(input("Enter third number"))

Enter first number10

Enter second number20

Enter third number30

In [8]:

if(x>y) and (x>z):

print("first number is largest")

elif (y>x) and (y>z):

print("second number is largest")

else:

print("third number is largest")

third number is largest


 

Review Question

1. Explain three types of conditional statements.

 

Python for Data Science: Chapter 1: Basics of Python : Tag: Computer Programming, Python, Data Science : - Python: Flow Control Statements


Python for Data Science: Chapter 1: Basics of Python



Under Subject


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science - Laboratory

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation