Python Programming IV Sem Lab Manual
PART A
1. Perform the following operations on number list:
a) Sum
b) Average
c) Max
d) Min
e) Sort f) Reverse
# Input: List of numbers
numbers = [10, 20, 5, 8, 15, 25]
# a) Sum
total_sum = sum(numbers)
print("Sum:", total_sum)
# b) Average
average = total_sum / len(numbers)
print("Average:", average)
# c) Max
maximum = max(numbers)
print("Max:", maximum)
# d) Min
minimum = min(numbers)
print("Min:", minimum)
# e) Sort
sorted_list = sorted(numbers)
print("Sorted List:", sorted_list)
# f) Reverse
reversed_list = list(reversed(numbers))
print("Reversed List:", reversed_list)
Output:
Sum: 83
Average: 13.833333333333334
Max: 25
Min: 5
Sorted List: [5, 8, 10, 15, 20, 25]
Reversed List: [25, 15, 8, 5, 20, 10]
Abhishek A, ICIS Department, Srinivas University
2. Perform the following operations on string list:
a) Swap elements of string list
b) Combining two list
# Input: Two string lists
list1 = ["Dean", "HOD", "Professor"]
list2 = ["Mangaluru", "Kudla", "Dakshina Kannada"]
# a) Swap elements of string list
list1[0], list1[2] = list1[2], list1[0] # Swap first and last elements
print("List after swapping elements:", list1)
# b) Combine two lists
combined_list = list1 + list2 # Concatenating two lists
print("Combined List:", combined_list)
Output:
List after swapping elements: ['Professor', 'HOD', 'Dean']
Combined List: ['Professor', 'HOD', 'Dean', 'Mangaluru', 'Kudla', 'Dakshina
Kannada']
3. Perform matrix operations using NumPy
import numpy as np
x = np.array([[1,6],[8,35]])
y = np.array([[1,3], [2,7]])
print("The element wise addition of matrix is:")
print(np.add(x,y))
print("The element wise subtraction of matrix is:")
print(np.subtract(x,y))
print("The element wise division of matrix is:")
print(np.divide(x,y))
print("The element wise multiplication of matrix is:")
print(np.multiply(x,y))
print("The product of matrices is: ")
Abhishek A, ICIS Department, Srinivas University
print(np.dot(x,y))
Output:
The element wise addition of matrix is:
[[ 2 9]
[10 42]]
The element wise subtraction of matrix is:
[[ 0 3]
[ 6 28]]
The element wise division of matrix is:
[[1. 2.]
[4. 5.]]
The element wise multiplication of matrix is:
[[ 1 18]
[ 16 245]]
The product of matrices is:
[[ 13 45]
[ 78 269]]
4. Write a python program to find tuples which have all elements divisible by k from a
list of tuples
# Input: List of tuples and value of k
tuples_list = [(10, 20, 30), (12, 15, 18), (8, 16, 24), (7, 14, 21)]
k=5
# Filter tuples where all elements are divisible by k
result = [tup for tup in tuples_list if all(element % k == 0 for element in tup)]
# Output result
print("Tuples with all elements divisible by", k, "are:", result)
Output:
Tuples with all elements divisible by 5 are: [(10, 20, 30)]
Abhishek A, ICIS Department, Srinivas University
5. Write a python program for removing duplicates from tuples
x = input("Enter the value : ")
y = tuple(x)
print("The original tuple is : ")
print(y)
result = set (y)
print("The tuple after removing duplicates : ")
print(tuple(result))
Output:
Enter the value : MAASTHI AMMA
The original tuple is :
('M', 'A', 'A', 'S', 'T', 'H', 'I', ' ', 'A', 'M', 'M', 'A')
The tuple after removing duplicates :
('T', 'A', 'M', 'I', 'S', 'H', ' ')
6. Perform the following operations using NumPy:
a) Joining two arrays
b) Splitting arrays
c) Searching element
d) Sorting
e) Filtering
import numpy as np
# a) Joining two arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
joined_array = np.concatenate((array1, array2))
print("Joined Array:", joined_array)
# b) Splitting arrays
split_array = np.array_split(joined_array, 3)
print("Split Arrays:", split_array)
# c) Searching element
search_element = 5
result = np.where(joined_array == search_element)
print(f"Element {search_element} found at index:", result[0])
# d) Sorting
unsorted_array = np.array([3, 1, 5, 2, 4])
sorted_array = np.sort(unsorted_array)
print("Sorted Array:", sorted_array)
Abhishek A, ICIS Department, Srinivas University
# e) Filtering
filter_condition = joined_array > 3
filtered_array = joined_array[filter_condition]
print("Filtered Array (elements > 3):", filtered_array)
Output:
Joined Array: [1 2 3 4 5 6]
Split Arrays: [array([1, 2]), array([3, 4]), array([5, 6])]
Element 5 found at index: [4]
Sorted Array: [1 2 3 4 5]
Filtered Array (elements > 3): [4 5 6]
7. Pandas with experiment
Create excel table of students using pandas having
1) USN
2) Name
3) M1
4) M2
5) M3
Calculate total and average and display result in table format
import pandas as pd
data = {
'USN': ['03SU23CC010', '03SU23CC068', '03SU23CC032', '03SU23DS019',
'03SU23DS022'],
'Name': ['AlRICK', 'SHREENIDHI', 'KALIDAS', 'SHARAN', 'VEEKSHITHA'],
'M1': [75, 82, 90, 65, 88],
'M2': [80, 72, 85, 70, 91],
'M3': [78, 84, 92, 68, 85]
}
df = pd.DataFrame(data)
df['Total'] = df['M1'] + df['M2'] + df['M3']
df['Average'] = df['Total'] / 3
print(df)
df.to_excel('student_marks.xlsx', index=False)
Output:
USN Name M1 M2 M3 Total Average
0 03SU23CC027 GOKUL 75 80 78 233 77.666667
1 03SU23CC068 SHREENIDHI 82 72 84 238 79.333333
2 03SU23CC032 KALIDAS 90 85 92 267 89.000000
3 03SU23DS019 SHARAN 65 70 68 203 67.666667
4 03SU23DS022 VEEKSHITHA 88 91 85 264 88.000000
Abhishek A, ICIS Department, Srinivas University