Hslip18


Q1]
import random

def reverse_elements(arr):
reversed_array = {v: k for k, v in arr.items()}
return reversed_array

def traverse_random_order(arr):
random_order = random.sample(arr.items(), len(arr))
return dict(random_order)

def convert_to_variables(arr):
for key, value in arr.items():
globals()[key] = value

def display_with_keys(arr):
for key, value in arr.items():
print(f'{key}: {value}')

Sample associative array

associative_array = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

while True:
print("\nMenu:")
print("1. Reverse the order of each element’s key-value pair.")
print("2. Traverse the element in an array in random order.")
print("3. Convert the array elements into individual variables.")
print("4. Display the elements of an array along with key.")
print("5. Exit")

choice = int(input("Enter your choice (1-5): "))

if choice == 1:
    reversed_array = reverse_elements(associative_array)
    print("Reversed Array:", reversed_array)
elif choice == 2:
    random_order = traverse_random_order(associative_array)
    print("Random Order:", random_order)
elif choice == 3:
    convert_to_variables(associative_array)
    print("Variables Created:", globals())
elif choice == 4:
    display_with_keys(associative_array)
elif choice == 5:
    print("Exiting the program.")
    break
else:
    print("Invalid choice. Please enter a number between 1 and 5.")

Q2]
A]
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

Load iris dataset

iris_df = pd.read_csv('iris.csv')

Create box plots

plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
sns.boxplot(x='species', y='sepal_length', data=iris_df)
plt.subplot(2, 2, 2)
sns.boxplot(x='species', y='sepal_width', data=iris_df)
plt.subplot(2, 2, 3)
sns.boxplot(x='species', y='petal_length', data=iris_df)
plt.subplot(2, 2, 4)
sns.boxplot(x='species', y='petal_width', data=iris_df)

plt.show()

B]
import pandas as pd

Load heights and weights dataset

hw_df = pd.read_csv('heights_weights.csv')

Print first 5 rows

print("First 5 Rows:")
print(hw_df.head())

Print last 5 rows

print("\nLast 5 Rows:")
print(hw_df.tail())

Print random 10 rows

print("\nRandom 10 Rows:")
print(hw_df.sample(10))