0% found this document useful (0 votes)
2 views42 pages

Python Lab Manual

The document contains a series of Python programming exercises for a lab course in 2025-26, covering various topics such as arithmetic operations, checking even/odd numbers, functions, lists, tuples, dictionaries, and sets. Each section includes code snippets and their corresponding outputs, demonstrating the functionality of different Python constructs. The exercises aim to provide hands-on experience with fundamental programming concepts in Python.

Uploaded by

khansana7752
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views42 pages

Python Lab Manual

The document contains a series of Python programming exercises for a lab course in 2025-26, covering various topics such as arithmetic operations, checking even/odd numbers, functions, lists, tuples, dictionaries, and sets. Each section includes code snippets and their corresponding outputs, demonstrating the functionality of different Python constructs. The exercises aim to provide hands-on experience with fundamental programming concepts in Python.

Uploaded by

khansana7752
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programming Lab | 2025-26

1) Python Program to Perform Arithmetic Operations


on 2 values

a=int(input("Enter the a value"))


b=int(input("Enter the b value"))
c=a+b
print("Sum of 2 numbers is ",c)
d=a-b
print("Difference of 2 numbers is ",d)
e=a*b
print("Product of 2 numbers is ",e)
f=a//b
print("Quotient of ",a, "and",b,"is ",f)
g= a%b
print("Remainder of ",a,"and",b,"is ",g)

Output
Python Programming Lab | 2025-26

2) Python Program to find Sum and Product of N Numbers

n = int(input("Enter the N value"))


sum=0
prod=1
# to find sum of n numbers
for i in range(1,n+1):
sum = sum + i
# to find product of n numbers
for i in range(1,n+1):
prod = prod * i
print("Sum of", n, "values is ", sum)
print("Product of", n, "values is ", prod)

Output
Python Programming Lab | 2025-26

3) Python Program to Check Given Number is Even or Odd

n = int(input("Enter N Value"))
if(n%2==0):
print("Given Number ", n, "is Even")
else:
print("Given Number ", n, "is Odd")

Output

Enter N Value 7

Given Number 7 is Odd

Enter N Value 8

Given Number 8 is Even


Python Programming Lab | 2025-26

4) Python Program to Print Even and Odd Numbers from 1 to N

n = int(input("Enter N Value"))
for i in range(1,n+1):
if(i%2==0):
print(i ,"is Even ")
else:
print(i, "is Odd ")

Output

Enter N Value10
1 is a Odd Number
2 is a Even Number
3 is a Odd Number
4 is a Even Number
5 is a Odd Number
6 is a Even Number
7 is a Odd Number
8 is a Even Number
9 is a Odd Number
10 is a Even Number
Python Programming Lab | 2025-26

5) Python Program to Print Number of Even and Odd Numbers from 1 to N

n = int(input("Enter N Value"))
ecount=ocount=0
for i in range(1,n+1):
if(i%2==0):
ecount=ecount+1
else:
ocount=ocount+1
print("Number of Even Numbers from 1 to",n," is ",
ecount)
print("Number of Odd Numbers from 1 to ",n," is ",
ocount)

Output

Enter N Value 10

Number of Even Numbers from 1 to 10 is 5

Number of Odd Numbers from 1 to 10 is 5


Python Programming Lab | 2025-26

6) Python Program to Print Sum of Even and Odd Numbers from 1 to N

Output

Enter N Value 10

Sum of Even Numbers from 1 to 10 is 30

Sum of Odd Numbers from 1 to 10 is 25


Python Programming Lab | 2025-26

7) Python Program to Illustrate Functions

Function With No Argument

OUTPUT

Hello, smith

Function With No Argument

OUTPUT

Enter the name Ivan


Hello, Ivan
Python Programming Lab | 2025-26

Function With Default Argument

OUTPUT

Function With Keyword Argument

OUTPUT
Python Programming Lab | 2025-26

Function With Arbitrary Keyword Argument

Code Code

Output Output
Python Programming Lab | 2025-26

8) Python Program to Print Sum of Individual Digits of the Given


Number

def sumOfIndividualDigits(n):
s=0
while n>0:
r=n%10
s=s+r
n=n//10
return s;
n = int(input("Enter the N Value"))
res = sumOfIndividualDigits(n)
print("Sum of Individual Digits of the gven Number is ", res)

Output

Enter the N Value 176

Sum of Individual Digits of the gven Number is 14


Python Programming Lab | 2025-26

9) Python Program Check the Given Number is Prime or not

Output

enter N value 5
Given Number is a prime Number
enter N value 6
Given number is not a Prime Number
Python Programming Lab | 2025-26

10) Python Program to print prime numbers from 1 to N

Output

2 is a Prime Number
3 is a Prime Number
5 is a Prime Number
7 is a Prime Number
Python Programming Lab | 2025-26

11) Python Program to Check the given number is Armstrong or Not

Output

Enter the N Value 153


153 is a Armstrong Number
Enter the N Value 123
123 is not a Armstron
Python Programming Lab | 2025-26

12) Python Program to print the given number is Reverse

Output

Enter a number: 234


Reversed number: 432
Python Programming Lab | 2025-26

13) Python Program to check the given number is perfect or not

Output

Enter a number: 8
8 is not a perfect number.
Enter a number: 6
6 is a perfect number.
Python Programming Lab | 2025-26

14) Python Program to print nth Fibonacci Number

Output

f1 = 1
f2 = 1
Enter N Value 8
2
3
5
8
13
21
8 th fibonacci number is 21
Python Programming Lab | 2025-26

15) Program to check the given number is Harshad Number or not

Output

Enter the n value 81


The Given Number is a Hashad Number
Enter the n value52
The Given Number is not a Hashad Number
Python Programming Lab | 2025-26

16) Program to Illustrate Python List and its void methods

# creating a List
l1 =[2,3,4]
print("original List",l1)

# Appending an Element at the end of the list


[Link](6)
print("After Appending List",l1)

# Appending a list of Elements at the end of the list


[Link]([8,9,10])
print('After extending the list',l1)

# to insert an element at 3rd position


[Link](3,20)
print('after inserting element at 3 rd position',l1)

# sort method
print("Before sorting",l1)
[Link]()
print("After sorting",l1)

# removing an element at 1st position


del l1[1]
print('after removing element at 1st pos',l1)

# removing an element at 5th position


removed = [Link](5)
print("removed element",removed)
print("after removing element 5th element ",l1)

# modifying an element at 3rd position


l1[3] = "Korth"
print("After modifying ",l1)

# modifying elements from 1st to 3rd position


l1[1:3] = "hello","hai","good"
print("after modifying",l1)
Python Programming Lab | 2025-26

Output

original List [2, 3, 4]

After Appending List [2, 3, 4, 6]

After extending the list [2, 3, 4, 6, 8, 9, 10]

after inserting element at 3 rd position [2, 3, 4, 20, 6, 8, 9, 10]

Before sorting [2, 3, 4, 20, 6, 8, 9, 10]

After sorting [2, 3, 4, 6, 8, 9, 10, 20]

after removing element at 1st pos [2, 4, 6, 8, 9, 10, 20]

removed element 10

after removing element 5th element [2, 4, 6, 8, 9, 20]

After modifying [2, 4, 6, 'Korth', 9, 20]

after modifying [2, 'hello', 'hai', 'good', 'Korth', 9, 20]


Python Programming Lab | 2025-26

17) Program to Illustrate Python List and its return type methods

Output

removed element 78

after Removing an element from the list are [20, 34, 67, 23, 67]

Number of elements in l2 are 2

Position of 23 in the list is 3


Python Programming Lab | 2025-26

18) Program to Illustrate Python List and its Functions

l1=[34,56,34,12,78,10]
print("Original List elements are",l1)
print('[Link] Elements in the list is',len(l1))
sorted = sorted(l1)
print('original List',l1,'sorted list',sorted)
print("Smallest Element in the list is ",min(l1))
print("Largest Element in the list is ",max(l1))
print("Sum of elements in the list is ",sum(l1))
print("Reverse Order of elements in the list is ", list(reversed(l1)))

Output

Original List elements are [34, 56, 34, 12, 78, 10]

[Link] Elements in the list is 6

original List [34, 56, 34, 12, 78, 10] sorted list [10, 12, 34, 34, 56, 78]

Smallest Element in the list is 10

Largest Element in the list is 78

Sum of elements in the list is 224

Reverse Order of elements in the list is [10, 78, 12, 34, 56, 34]
Python Programming Lab | 2025-26

19) Program to Illustrate Python tuples

t1 =('hello','hai','smith','jones',3,4,5,6,7)
print('Original List',t1)
# Adding elements to tuple. As it is immutable,need to convert into list and then append
y =list(t1)
[Link]("orange")
t1 =tuple(y)
print('''After Adding the element 'orange' to the original tuple''',t1)
#Adding a tuple to another tuple
t2 = ("apple","banana","cherry")
print(" new tuple",t2)
t2 += t1
print("after concatenating 2 tuples",t2)
# Modifying the tuple
print("Before modifying the tuple elements",t1)
y =list(t1)
y[1] ="kiwi"
t1 =tuple(y)
print("After modifying the 1st position element",t1)
# Accessing the elements of original tuple
print("elements of the original tuple are ")
for i in t1:
print(i)
# accessing 3rd element
print('3rd element of the tuple is ',t1[3])
#Accessing elements from 2nd to 5th
print('from 2nd to 6th ',t1[2:6])
# Accessing last 3 elements
print('Accessing last 3 elements',t1[-1:-4:-1])
#Accessing elements from first to last
print('Accessing from first to last',t1[1:])
Python Programming Lab | 2025-26

Output

Original List

('hello', 'hai', 'smith', 'jones', 3, 4, 5, 6, 7)

After Adding the element 'orange' to the original tuple

('hello', 'hai', 'smith', 'jones', 3, 4, 5, 6, 7, 'orange')

new tuple

('apple', 'banana', 'cherry')

after concatenating 2 tuples

('apple', 'banana', 'cherry', 'hello', 'hai', 'smith', 'jones', 3, 4, 5, 6, 7, 'orange')

Before modifying the tuple elements

('hello', 'hai', 'smith', 'jones', 3, 4, 5, 6, 7, 'orange')

After modifying the 1st position element

('hello', 'kiwi', 'smith', 'jones', 3, 4, 5, 6, 7, 'orange')

elements of the original tuple are

hello
kiwi
smith
jones
3
4
5
6
7
Orange
3rd element of the tuple is jones

from 2nd to 6th

('smith', 'jones', 3, 4)

Accessing last 3 elements

('orange', 7, 6)

Accessing from first to last

('kiwi', 'smith', 'jones', 3, 4, 5, 6, 7, 'orange')


Python Programming Lab | 2025-26

20) Program to Illustrate Python tuple methods and Functions

t1= (1,2,3,4,9,2,10,2)
print(" Original Tuple",t1)
print("Number of Occurences of element 2 in the original tuple is ",[Link](2))
print("Position of the element 30 in the given tuple is ", [Link](9))
print("Number of Elements in the tuple is ",len(t1))
print("Biggest Number in the Tuple is",max(t1))
print("Smallest Number in the Tuple is",min(t1))
print("Sum of the elements of the tuple is",sum(t1))
print("Sorting Tuple Elements ",sorted(t1))

Output

Original Tuple (1, 2, 3, 4, 9, 2, 10, 2)

Number of Occurrences of element 2 in the original tuple is 3

Position of the element 30 in the given tuple is 4

Number of Elements in the tuple is 8

Biggest Number in the Tuple is 10

Smallest Number in the Tuple is 1

Sum of the elements of the tuple is 33


Python Programming Lab | 2025-26

21) Program to Illustrate Python Dictionary

Output

Original Dictionary {1: 'Monday', 2: 'Tuesday', 3: 'Wednsday'}


after appending new key value {1: 'Monday', 2: 'Tuesday', 3: 'Wednsday', 4: 'Thursday'}
after appending new element {1: 'Monday', 2: 'Tuesday', 3: 'Wednsday', 4: 'Thursday',
6: 'saturday'}
keys of the original dictionary are dict_keys([1, 2, 3, 4, 6])
values of the dictionary are dict_values(['Monday', 'Tuesday', 'Wednsday', 'Thursday',
'saturday'])
popped value of the key 2 Tuesday
after popping {1: 'Monday', 3: 'Wednsday', 4: 'Thursday', 6: 'saturday'}
Python Programming Lab | 2025-26

22) Program to Illustrate Python Set


Python Programming Lab | 2025-26

Output

original set elements are {2, 56, 'hello', 78, 'hai'}

After Adding new element to the set {'new', 2, 56, 'hello', 78, 'hai'}

After removing 'hello' from the set {'new', 2, 56, 78, 'hai'}

after popping the first element {2, 56, 78, 'hai'}

original set 1 {1, 2, 3, 5}

Original set 2 {1, 2, 5, 9}

s1 U s2 = {1, 2, 3, 5, 9}

s1-s2 = {3}

s2-s1 = {9}

s1 intersection s2 = {1, 2, 5}

packing and unpacking a tuple

apple

banana

cherry

packing and unpacking a tuple

apple

banana

['cherry', 'strawberry', 'raspberry']


Python Programming Lab | 2025-26

23) Illustration of Python String manipulations


23.1) Python Program to get a string made of the 𝑓irst 2 and last 2 characters
of a given string.

Output

Pyld

23.2) Python program to get a string from a given string where all
occurrences of its 𝑓irst char have been changed to '$', except the 𝑓irst
char itself.

Output

resta$t

23.3) Python program to get a single string from two given strings, separated by
a space and swap the 𝑓irst two characters of each string.

Output

xyc abz
Python Programming Lab | 2025-26

23.4) Python program to add 'ing' at the end of a given string (length should be at
least 3). If the given string already ends with 'ing', add 'ly' instead. If the string
length of the given string is less than 3, leave it unchanged.

Output

None

abcing

stringly

23.5) Python program to remove the nth index character from a nonempty string.

Output

ython

Pyton

Pytho
Python Programming Lab | 2025-26

23.6) Write a Python program to change a given string to a newly string where
the 𝑓irst and last chars have been exchanged.

Output

dbca

23.7) Write a Python program to remove characters that have odd index values in
a given string.

Output

characters at odd index places are : ace


Python Programming Lab | 2025-26

23.8) Write a Python program that accepts a comma-separated sequence of words as


input and prints the distinct words in sorted form (alphanumerically).

Output

Input comma eparated sequence of words red,black,pink,green,black

black, green, pink, red

23.9) Write a Python function to get a string made of 4 copies of the last two characters

of a speci𝑓ied string (length must be at least 2).

Output

Onononon
Python Programming Lab | 2025-26

23.10) Python program to lowercase the 𝑓irst n characters in a string.

Output

PythONWORLD

23.11) Program to Illustrate the Python String Methods.

str = "welcome to python world"


print("Given String is : ", str)
print("Given String in Upper Case is : ",[Link]())
print("Given String in Lower Case is : ",[Link]())
print("Captitalize first letter of the given string is : ",[Link]())
print("After swapping the case of the given string is : ",[Link]())
print('''to check the given string ends with 'world' is :''',[Link]('world'))
print('''to check the given string ends with 'world' is :''',[Link]('python'))
print('''to check the given string starts with 'world' is :''',[Link]('world'))
print('''to check the given string starts with 'world' is :''',[Link]('welcome'))
print('''[Link] times 'e' occurs in the given string is ''',[Link]('e'))

print('to check the given string contains all alphabets',[Link]())

Output

Given String is : welcome to python world

Given String in Upper Case is : WELCOME TO PYTHON WORLD

Given String in Lower Case is : welcome to python world


Python Programming Lab | 2025-26

Captitalize first letter of the given string is : Welcome to python world

After swapping the case of the given string is : WELCOME TO PYTHON WORLD

to check the given string ends with 'world' is : True

to check the given string ends with 'world' is : False

to check the given string starts with 'world' is : False

to check the given string starts with 'world' is : True

[Link] times 'e' occurs in the given string is 2

to check the given string contains all alphabets True

23.12) Python Program to check whether the given string exists in the Original String

Output

to is found in the Welcome to Python world

23.13) Python Program to replace a word in the given string

Output

Welcome to python world


Python Programming Lab | 2025-26

24) Python Program to Illustrate Class-Object

class Rectangle:
def init (self,l,b):
self._l=l
self. b=b
def area(self):
return self._l*self. b
r1=Rectangle(2,3)
print('Area of first Rectangle',[Link]())
r2 = Rectangle(4,5)
print('Area of second rectangle',[Link]())

Output

Area of first Rectangle 6

Area of second rectangle 20


Python Programming Lab | 2025-26

25) Python Program to Illustrate Constructor Overloading

class Const:
def init (self,a=0,b=0,c=0):
self.a=a
self.b=b
self.c=c
def sum(self):
return self.a+self.b+self.c
s1 = Const(3,4)
sum2 = [Link]()
print('sum of 2 numbers',sum2)
s2 = Const(2,3,4)
sum3 = [Link]()
print('sum of 3 numbers',sum3)

Output

sum of 2 numbers is 7
sum of 3 numbers is 9
Python Programming Lab | 2025-26

26) A. Python Program to Illustrate Method Overloading

class Meth:
def sum(self,x=0,y=0,z=0):
return x+y+z
s1=Meth()
so2=[Link](2,3)
so3=[Link](1,2,3)
print("sum of 2 numbers is ",so2)
print("sum of 3 numbers is ",so3)

Output

sum of 2 numbers is 5
sum of 3 numbers is 6
Python Programming Lab | 2025-26

27) B. Python Program to Illustrate Method Overloading

class methOverloading:
def add(self, a, b):
return a + b
def add(self, a, b, c):
return a + b + c
def add(self, *args):
return sum(args)
# Creating an object of MyClass
obj = methOverloading()
# Calling the overloaded add method
print([Link](2, 3)) # Output: 5
print([Link](2, 3, 4)) # Output: 9
print([Link](2, 3, 4, 5, 6)) # Output: 20

Output

Sum of 2 Numbers is 5
Sum of 3 Numbers is 9
Sum of 5 Numbers is 20
Python Programming Lab | 2025-26

28) Python Program to Illustrate Single Inheritance

# Base class
class Animal:
def init (self, name):
[Link] = name

def make_sound(self):
pass # This method will be overridden in the derived
classes

# Derived class inheriting from the Animal class


class Dog(Animal):
def init (self, name, breed):# Call the constructor of
the base class using the super() function
super(). init (name)
[Link] = breed

def make_sound(self):
return "Woof!"
# Create instances of the derived classes
dog = Dog("Buddy", "Golden Retriever")

# Accessing attributes and methods of the base class through


the derived classes
print([Link] ," is a", [Link] ," and says ",
dog.make_sound())

Output

Buddy is a Golden Retriever and says Woof!


Python Programming Lab | 2025-26

29) Python Program to Illustrate Heriarchical Inheritance

class Animal:
def init (self,
name): [Link]
= name
def make_sound(self):
pass
def move(self):
print(f"{[Link]} is moving.")
class Dog(Animal):
def make_sound(self):
print("Woof!")
class Cat(Animal):
def make_sound(self):
print("Meow!")
# Creating objects of the derived classes
dog = Dog("Buddy")
cat = Cat("Whiskers")
# Calling the methods
dog.make_sound() # Output: Woof!
[Link]() # Output: Buddy is moving.
cat.make_sound() # Output: Meow!
[Link]() # Output: Whiskers is moving.

Output

Woof!
Buddy is moving
Meow
Whiskers is moving
Python Programming Lab | 2025-26

30) Python Program to Illustrate Multi Level Inheritance

Output

('Hi my name is ', 'John Doe', ' and I am ', 35, ' years old.')
I am an employee and I am working.
I am managing a team.
Python Programming Lab | 2025-26

31) Python Program to Illustrate Pandas package

import pandas as pd
df = pd.read_csv(r"C:\Users\Padmaja
R\OneDrive\Desktop\[Link]")
print("The complete Employee Data from CSV file")
print(df)

# to get specific columns


col = input("Enter the column name")
print(df[col])

# to get specific rows


print("Employees who earn salary > 30000")
rows = [Link]("SALARY>30000")
print(rows)

# TO GET SUM OF SALARIES OF EMPLOYEES


print("Total salary paid to employees are")
tot_sal = df['SALARY'].sum()
print(tot_sal)

# to get No. of employees in each department


dept_counts = df['[Link].'].value_counts()
print(" [Link] Employees in Each Department", dept_counts)
Python Programming Lab | 2025-26

Output

The Complete Employee Data from CSV 𝑓ile


ENO. EMP_NAME DESIGNATION SALARY [Link]. AGE
E0001 SMITH SALES MANAGER 20000 10 32
E0002 JONES SYSTEM ANALYST 30000 20 26
E0003 KING MANAGER 50000 10 40
E0004 IVAN SOFTWARE ENGINEER 37000 20 28
E0005 BAYROSS SALES MANAGER 26000 10 30
E0006 KORTH SYSTEM ANALYST 32000 10 35
E0007 LEON SYSTEM ANALYST 37000 20 29
E0008 MATHEWS SOFTWARE ENGINEER 30000 10 34
E0009 ALEX SOFTWARE ENGINEER 28000 10 33
E0010 BOB SYSTEM ANALYST 35000 20 36

Enter the column name EMP_NAME


EMP_NAME
SMITH
JONES
KING
IVAN
BAYROSS
KORTH
LEON
MATHEWS
ALEX
BOB
Employees who earn salary > 30000
ENO. EMP_NAME DESIGNATION SALARY [Link]. AGE
E0003 KING MANAGER 50000 10 40
E0004 IVAN SOFTWARE ENGINEER 37000 20 28
E0006 KORTH SYSTEM ANALYST 32000 10 35
E0007 LEON SYSTEM ANALYST 37000 20 29
E0010 BOB SYSTEM ANALYST 35000 20 36
Total salary paid to employees are
325000
[Link] Employees in Each Department [Link].
10 6
20 4

You might also like