Python for Data Science: Laboratory Programs in Python : Basics of Python : Python Programs using conditional and looping constructs
Conditional:
There are various types of conditional statements ‒
1) if statements
2) if‒else or alternate statements
3) Nested if
4) Chained conditionals
Looping:
1. while Loop
2. for Loop
3. The break
4. The continue
5. The pass
if condition:
statement
if a<10:
print (" The number is less than 10" )
Ex. 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

if condition:
statement
else:
statement
If the condition is true, then the if‒block is executed. Otherwise, the else‒block is executed.
Ex. 2
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

Ex. 3
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
Ex. 4
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
Write a python program to find the largest among the three numbers.
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

Syntax
while test_condition:
body of while
else:
statement
Example
while i<=10:
i=i+1
else:
print("Invalid value of i")
Ex. 6
Write a python program for computing the sum of n natural numbers and finding out the average of it.
Solution:
The python script is as given
sum_avg.py

Output
Click on New‒>Python 3(ipykernel) inorder to get the prompt window

Issue the import command as follows ‒
In [1]: import sum_avg
Enter the value of n:
5
sum is: 55
average is: 11.0

Ex. 7
Write a python program to display square of n numbers using while loop.
Solution:
print("Enter the value of n")
n int(input())
i=1
print("The sqaure table is as given below...")
while i<=n:
print(i,i+1)
i=i+1
Output

In [1]: import test
Enter the value of n
7
The sqaure table is as given below...ordered
1 1
2 4
3 9
4 16
5 25
6 36
7 49
Ex. 8
Write a python program for displaying even or odd numbers between 1 to n.
Solution :
print("Enter the value of n")
n = int(input())
i=1
j=i
while j<=n:
if i%2== 0:
print(i," is even")
else:
print(i," is odd")
j=j+1
i=j
Output

In [1]: import test
Enter the value of n
10
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
Ex. 9
Write a python program to display fibonacci numbers for the sequence of length.
Solution :
print("Enter the value of n")
n=int(input())
a=0
b=1
i=0
print("Fibonacci Sequence is...")
while i<n;
print(a)
c=a+b
a=b
b=c
i=i+1
Output
Enter the value of n
10
Fibonacci Sequence is...
0
1
1
2
3
5
8
13
21
34
Syntax
for variable in sequence:
Body of for loop
Example
for val in numbers:
val=val+1
Similarly, we can have for loop with
else statement
Syntax
for variable in sequence:
Body of for loop
else:
Statement

Example
for i in myList:
print(i)
else:
print("The number is not present in your list")
Ex. 10
Write a python program to find the sum of 1 to 10 numbers.
Solution :
print("Enter the value of n")
n = int(input())
sum = 0
for i in range(1,n+1):
sum = sum+i
print("The sum of ",n,"numbers is ",sum)
Output
Enter the value of n
5
The sum of 5 numbers is
15
Ex. 11
Write a python program to display the multiplication table.
Solution:
print("Enter number for its multiplication table")
n=int(input())
for i in range(1,11):
print(n,"X",i,i*n)
Output
Enter number for its multiplication table
10
10 X 1 10
10 X 2 20
10 X 3 30
10 X 4 40
10 X 5 50
10 X 6 60
10 X 7 70
10 X 8 80
10 X 9 90
10 X 10 100
Ex. 12
Write a program to print 1+1/2+1/3+1/4+......... +1/N series.
Solution :
print("Enter value of N")
n=int(input())
sum=0
for i in range(1,n+1):
sum=sum+1/i
print("The Sum is ",sum)
Output
Enter value of N
5
The Sum is 2.283333333333333
Ex. 13
Write a python program to check whether a given number is prime or not.
Solution :
# User can enter the value thru keyboard
print("Enter the number");
num = int(input())
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) = = 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
# if input number is less than
# or equal to 1, it is not prime
else:
print(num,"is not a prime number")
Output
Enter the number
3
3 is a prime number
Ex. 14
Write a python program to find the prime numbers between given intervals.
Solution :
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("Prime numbers between", lower, "and",upper, "are:")
for num in range(lower,upper + 1):
prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) = = 0:
break
else:
print(num)
Output
Enter lower range: 1
Enter upper range: 10
Prime numbers between 1 and 10 are:
2
3
5
7
Ex. 15
Write a python program to display the star pattern as
*
**
***
****
*****
…
…
Users should enter the value of N
Solution :
print("Enter value of n")
n=int(input())
for i in range(0,n):
for j in range(0,i+1):
print('* ',end=""")
print("")
Output
Enter value of n
10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
Ex. 16
Write a python program to display the number pattern as follows
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
Solution:
for i in range (1,6):
for k in range(1,i):
print(end="")
for j in range(i,6):
print(" ",j,end="")
print();
Output
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
Write a python program to print the pattern as given below
A
A B
A B C
A B C D
A B C D E
for i in range(1, 6):
for j in range(65, 65+i):
a = chr(j)
print(a," ",end=""")
print()
A
A B
A B C
A B C D
A B C D E
Write a python program to print the pattern for alphabets
A A A A A
B B B B
C C C
D D
E
num=65 #ASCII Value for A
for i in range(0,5):
for j in range(i,5):
ch=chr(num+i) # Converting ASCII to character
print(ch," ",end=""")
print();
A A A A A
B B B B
C C C
D D
E
break
for i in range(1,11):
if i==5:
print("Element {} Found!!!".format(i))
break
print(i)
Output
1
2
3
4
Element 5 Found!!!
continue
In while loop the continue passes the program control to conditional test.Following example illustrates the idea of Condition
i=0
while i<10:
i=i+1
if i%2==0:
continue
print(i)
1
3
5
7
9
Pass
for i in range(1,5):
if i==3:
pass
print("Reached at pass statement")
print("The current number is ",i)
The current number is 1
The current number is 2
Reached at pass statement
The current number is 3
The current number is 4
Write a python program to print sum of cubes of the values of n variables.
Solution :
sum = 0
n = int(input("Enter some number: "))
for i in range(1, n+1):
sum = sum+(i*i*i)
print("Sum of cubes of first",n,"numbers is = ",sum)
Output
Enter some number: 5
Sum of cubes of first 5 numbers is = 225
>>>
Find the syntax error in the code given: while True print('Hello world').
Solution:
There must be colon after true and the print statement must be indented. The correct code is as follows ‒
while True:
print('Hello world')
Python for Data Science: Laboratory Programs in Python : Tag: Computer Programming, Python, Data Science : Laboratory Programs in Python - Python Programs using conditional and looping constructs
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science - Laboratory
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 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