Part_B
Part_B
def selection_sort(array):
length = len(array)
for i in range(length - 1):
minInd = i
for j in range(i + 1, length):
if array[j] < array[minInd]:
minInd = j
array[i], array[minInd] = array[minInd], array[i]
return array
try:
numerator = int(input("enter numerator value :"))
denominator = int(input("enter denominator value :"))
result = numerator/denominator
except ZeroDivisionError:
print("Error: Denominator cannot be 0.")
else: print(result)
finally:
print("This is finally block.")
# Create a dictionary
student = {
"name": "Smith",
"age": 20,
"university": "ABC University",
"major": "Computer Science"
}
# Accessing dictionary values
print("Name:", student["name"])
print("Age:", student["age"])
print("University:", student["university"])
print("Major:", student["major"])
# Modifying dictionary values
student["age"] = 21
student["university"] = "XYZ University"
print("Modified Dictionary:")
print(student)
# Adding a new key-value pair
student["gpa"] = 3.8
print("Updated Dictionary:")
print(student)
# Removing a key-value pair
del student["major"]
print("Dictionary after removing 'major' key:")
print(student)
# Iterating over dictionary keys
print("Dictionary Keys:")
for key in student.keys():
print(key)
# Iterating over dictionary values
print("Dictionary Values:")
for value in student.values():
print(value)
#Iterating over dictionary key-value pairs
print("Dictionary Key-Value Pairs:")
for key, value in student.items():
print(key + ":", value)
5) Demonstrate use of Tuples
# Creating a tuple
f = ("apple", "banana", "orange", "kiwi")
# Accessing tuple elements
print("First fruit:", f[0])
print("Second fruit:", f[1])
# Iterating over a tuple
print("All fruits:")
for fruit in f:
print(fruit)
# Concatenating tuples
more_fruits = ("grape", "mango")
all_fruits = f + more_fruits
print("All fruits combined:")
print(all_fruits)
# Tuple unpacking
a, b, c, d, e, f = all_fruits
print("Unpacked variables:")
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
import numpy as np
# Create an array
arr = np.array([1, 2, 3, 4, 5])
# Display the array
print("Original Array:")
print(arr)
# Perform array operations
# Calculate the sum of the array
arr_sum = np.sum(arr)
# Calculate the mean of the array
arr_mean = np.mean(arr)
# Calculate the maximum value in the array
arr_max = np.max(arr)
# Calculate the minimum value in the array
arr_min = np.min(arr)
# Display the results
print("Sum of the array:", arr_sum)
print("Mean of the array:", arr_mean)
print("Maximum value in the array:", arr_max)
print("Minimum value in the array:", arr_min)
8) Create data frame from excel sheet and perform simple operations.
import pandas as pd
# Read Excel sheet into a DataFrame
df = pd.read_excel("C:/Users/hp/Desktop/Book1.xlsx",sheet_name='Value')
# Display the DataFrame
print("Original DataFrame:")
print(df)
# Perform simple operations on the DataFrame
# Calculate the sum of a column
column_sum = df['data'].sum()
# Calculate the mean of a column
column_mean = df['data'].mean()
# Calculate the maximum value in a column
column_max = df['data'].max()
# Calculate the minimum value in a column
column_min = df['data'].min()
# Display the results
print("Sum of 'data' column:", column_sum)
print("Mean of 'data' column:", column_mean)
print("Maximum value in 'data' column:", column_max)
print("Minimum value in 'data' column:", column_min)