Data Structures Using Python
UNIT: II
Statements- Sequential Staments- Decision Making
Staements- If, If-Else, Nested If Statement , If-elif
Statement, Iterative Statements- while and For loop, The
Range Function, Nested Loop, Transfer Statements- Break,
Continue, and Pass Statement, The else Statement used
with Loops.
Statements: A program’s control flow is the order in which the
program’s code executes. Python has four types of statements.
They are
1. Sequential - default mode
2. Selection - used for decisions and branching
3. Repetition - used for looping, i.e., repeating a piece of code
multiple times.
4. Transfer statements - used for jumping
Sequential statements: Sequential statements are a set of
statements whose execution process happens in a sequence.
1. Write a Python Program to calculate sum of two
numbers.
#Python Program to calculate sum of two numbers
a=float(input("Enter first number: "))
b=float(input("Enter second number: "))
sum=a+b;
print("Sum of ",a," and ", b," is ",sum)
2. Write a Python Program to calculate simple interest.
# Python Program to calculate simple interest
p=float(input("Enter Principal Amount: "))
t=float(input("Enter time in years: "))
r=float(input("Enter rate of interest per years: "))
si=p*t*r/100
print("Simple Interest =",si)
3. Write a C program to find area of the triangle when
three sides are given.
Krishnaveni Degree College :: Narasaraopet Page No. : 1
Data Structures Using Python
UNIT: II
# Python Program to calculate area of the triangle
import math;
a=float(input("Enter first side: "))
b=float(input("Enter second side: "))
c=float(input("Enter third side: "))
s=(a+b+c)/2;
area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print("Area of the triangle with sides ",a,",",b," and ", c," is
",round(area,3))
4. Write a Python program to find a/(b-c).
# Python Program to calculate a/(b-c)
a=float(input("Enter first side: "))
b=float(input("Enter second side: "))
c=float(input("Enter third side: "))
res=a/(b-c)
print("Result = ",res)
Selection Statements: Selection statements allows the program to
choose set of statements for execution based upon the outcome
of an expression or the value of a variable. There are various
types of if statement in Python. They are
1. Simple if statement
2. if-else statement
3. nested if statement
4. if-elif-else statement
Simple if Statement: The simple if statement initially tests the
condition. If the condition is true it executes the statement-block
and moves to the next statement. If the condition is false it skips
the statement-block and directly moves to the next statement.
Syntax: Flowchart:
if(condition):
statement-block;
next-statement;
Krishnaveni Degree College :: Narasaraopet Page No. : 2
Data Structures Using Python
UNIT: II
Example: Write a Python program to find the absolute
value of number.
# Python Program to find the absolute value of the given number
num=float(input("Enter any number: "));
abs = num;
if(num < 0):
abs = -1 *num;
print("Absolute value of ",num," is ",abs);
if-else Statement: The if-else statement is an extension of simple
if statement. If-else statement initially tests the condition. If the
condition is true it executes the true-statement-block and moves
to the next statement. If the condition is false it executes the
false-statement-block and moves to the next statement. In any
case , either true-statement-block or false-statement-block is
executed and moves to the next statement
Syntax: Flowchart:
if(condition):
true-statement-block;
else:
false-statement-
block;
next-statement;
Example: Write a C program to check whether the number
is even or odd.
# Program to check whether the given number is even or odd
num=int(input("Enter any integer number: "));
if(num % 2 == 0):
print(num,"is an even number");
else:
Krishnaveni Degree College :: Narasaraopet Page No. : 3
Data Structures Using Python
UNIT: II
print(num," is an odd number");
Nested – if: When one if statement is nested in another if
statement then it is
called as nested if statement.
Syntax: Flowchart:
if(condition1):
if(condition2):
statement1-block;
else:
statement2-block;
else:
statement3-block;
next-statement
Example: Write a C Program to
check whether year is leap year or not
# Python Program to find year is leap year or not
year=int(input("Enter any year: "));
if(year%4 == 0):
if( year%100 == 0):
if (year%400 == 0):
print(year," is a leap year\n");
else:
print(year," is not a leap year\n");
else:
print(year," is a leap year\n");
else:
print(year," is not a leap year\n");
if-elif-else: When one if statement is added in the else part of
another if statement then it is called as if-elif-else ladder
statement.
Syntax:
Flowchart:
if(condition1):
statement1-block;
Krishnaveni Degree College :: Narasaraopet Page No. : 4
Data Structures Using Python
UNIT: II
elif(condition2):
statement2-block;
elif(condition3):
statement3-block;
else:
statement4-block;
next-statement;
Example: Write a Python Program to find largest among 3
numbers.
# Python Program to find largest among 3 numbers
a=float(input("Enter first number: "))
b=float(input("Enter first number: "))
c=float(input("Enter first number: "))
if((a>b)and(a>c)):
print(a," is largest");
elif(b>c):
print(b," is largest");
else:
print(c," is largest");
Switch: Python does not currently support switch or case
statements as in other languages. Python syntax supports
readability even in the presence of a good number of if elif
statements.
Example: Write a Python program to display name of the
day using if-elif statement.
# Python Program to display name of the day
dayno=int(input("Enter the day number: "))
if(dayno==1):
print(dayno, "day in the week is Monday\n")
elif(dayno==2):
print(dayno, "day in the week is Tuesday \n")
elif(dayno==3):
Krishnaveni Degree College :: Narasaraopet Page No. : 5
Data Structures Using Python
UNIT: II
print(dayno, "day in the week is Wedesday \n")
elif(dayno==4):
print(dayno, "day in the week is Thursday \n")
elif(dayno==5):
print(dayno, "day in the week is Friday \n")
elif(dayno==6):
print(dayno, "day in the week is Saturday \n")
else:
print(dayno,"is a wrong day number\n")
Iterative Statements: Iterative Statement repeats set of
instruction until the condition for termination is met. These
statements appear in the source code only once, but it execute
many times. Such kinds of statements are also called as loops.
Iteration Statement in Python are mainly of two types. They are
1. 'while' Statement
2. 'for' Statement
while Statement: The while loop is an entry controlled loop. It
tests the condition before executing the body of the loop. The
condition can be any relational or logical expression. The body of
the loop will be executed as long as the conditional expression is
true. When condition becomes false, control passes to the next
line of code immediately following the loop.
Syntax: Flowchart:
while condition:
Statement_1;
Statement_2;
next_statement;
Example: Write a Python
program to display sum of natural numbers
Krishnaveni Degree College :: Narasaraopet Page No. : 6
Data Structures Using Python
UNIT: II
#Python Program to display sum of natural numbers
n=int(input("Enter how many natural numbers: "));
i=1;
sum=0;
while(i<=n):
sum=sum+i;
i=i+1;
print("sum of ",n," natural numbers is ",sum);
For loop: In Python, the for statement is used to iterate through a
sequence like a list, a tuple, a set, a dictionary, or a string. The for
statement is used to repeat the execution of a set of statements
for every element of a sequence. The general syntax of for
statement in Python is as follows.
Syntax: Flowchart:
for <variable> in <sequence>:
Statement_1;
Statement_2;
Statement_3;
Next_statement;
Example: Write a Python
program to display name is vertical order.
#Python Program to display name is vertical order
name=input("Enter your name: ");
print("Printing characters of ",name,"is vertical order");
for ch in name:
print(ch);
Krishnaveni Degree College :: Narasaraopet Page No. : 7
Data Structures Using Python
UNIT: II
The range Function: The range() function returns a sequence of
numbers, starting from 0 by default, and increments by 1 (by
default), and stops before a specified number.
Syntax:
range(start, stop, step)
Paramet Description
er
Start Optional. An integer number specifying at which position
to start. Default is 0.
Stop Required. An integer number specifying at which position
to stop (not included).
Step Optional. An integer number specifying the
incrementation. Default is 1
Example: Write a Python program to display n natural
numbers using range function.
#Python Program to display sum of natural numbers
n=int(input("Enter how many natural numbers: "));
i=1;
print(n,"natural numbers are");
for i in range(1,n+1):
print(i);
Example: Write a Python program to display n odd
numbers using range function.
#Python Program to display sum of natural numbers
n=int(input("Enter how many odd numbers: "));
i=1;
end
print(n,"odd numbers are");
for i in range(1,2*n,2):
print(i);
Nested Loops: Python allows its users to have nested loops, that
is loops that can be placed within another loop. Although this
feature will work with any loop like while loop as well as for loop,
but it is most commonly used with the for loop. Because this is
easiest to control.
Krishnaveni Degree College :: Narasaraopet Page No. : 8
Data Structures Using Python
UNIT: II
Example: Write a Python program to display triangle of
numbers.
1
12
123
#Python Program to display triangle of numbers
n=int(input("Enter how many rows: "));
print(“Triangle with “,n,” rows is as follows”);
for i in range(1,n+1):
for j in range(1, i+1):
print(i+end=” “);
print();
Jump Statements: Python supports three jump statements.
They are
1. break
2. continue and
3. pass
These statements transfer control of execution to another part of
the program.
break: In Python the break statement is used to terminate the
loop.
Example: Write a Python Program to check whether the
given number is prime or not using break.
# Python program to check whether the given number is prime or
not using break
n=int(input("Enter any number: "));
i=n//2;
while(i<=n//2):
if((n%i)==0):
break;
i=i+1;
if(i>n/2):
printf(n," is a prime number ");
Krishnaveni Degree College :: Narasaraopet Page No. : 9
Data Structures Using Python
UNIT: II
else:
printf(n," is not a prime number ");
continue: continue Statement in Python is used to skip all the
remaining statements in the loop and move controls back to the
top of the loop.
# Python program to check whether the given number is prime or
not using continue
n=int(input("Enter any number: "));
i=1;
cnt=0;
while(i<=n):
if((n%i)!=0):
i=i+1;
continue;
i=i+1;
cnt=cnt+1;
if(cnt==2):
print(n," is a prime number ");
else:
print(n," is not a prime number ");
pass: pass Statement in Python does nothing. It specifies a null
operation or simply No Operation(NOP) statement. Nothing
happens when pass statement is executed.
# Python program to print the given number after some delay.
n=int(input("Enter any number: "));
i=1;
while(i<=n):
pass:
i=i+1;
print(“Given number",n);
The else Statement used with Loops: In Python the else
statement can be associated with a loop statement.
Krishnaveni Degree College :: Narasaraopet Page No. : 10
Data Structures Using Python
UNIT: II
else Statement used while Loop: If the else statement is used with
the while loop , the else statement is executed when condition
becomes False. If the else statement is used with the for loop ,
the else statement is executed when the loop has completed
iterating.
Syntax:
while condition:
Statement_1;
Statement_2;
else:
false statement;
next_statement;
Example: Write a Python program to display n natural
numbers
#Python Program to display sum of natural numbers
n=int(input("Enter how many natural numbers: "));
i=1;
print(n,"natural numbers are");
while(i<=n):
print(i);
i=i+1;
else:
print("End of printing");
else Statement used for Loop: If the else statement is used with
the for loop , the else statement is executed when the loop has
completed iterating.
Syntax:
for <variable> in <sequence>:
Statement_1;
Statement_2;
Statement_3;
else:
False_satement;
next_statement;
Krishnaveni Degree College :: Narasaraopet Page No. : 11
Data Structures Using Python
UNIT: II
Example: Write a Python program to display n natural
numbers
#Python Program to display sum of natural numbers
n=int(input("Enter how many natural numbers: "));
print(n,"natural numbers are");
for I in range(1,n+1):
print(i);
i=i+1;
else:
print("End of printing");
Output:
C:\python>python printnat.py
Enter how many natural numbers: 5
5 natural numbers are
1
2
3
4
5
End of printing
Krishnaveni Degree College :: Narasaraopet Page No. : 12