Assignment 1: Operations on a Larger List of Values
python
Copy code
# 1. Print the Original List
values = [12, 34, 56, 78, 90, 23, 45, 67, 89, 101]
print("Original List:", values)
# 2. Append Multiple Elements
values.extend([202, 303, 404])
print("List after appending 202, 303, 404:", values)
# 3. Insert Elements
values.insert(5, 15) # Insert 15 at index 5
print("List after inserting 15 at index 5:", values)
values.insert(2, 25) # Insert 25 at index 2
print("List after inserting 25 at index 2:", values)
# 4. Remove an Element
values.remove(67)
print("List after removing 67:", values)
# 5. Reverse the List
values.reverse()
print("Reversed List:", values)
# 6. Indexing
print("Element at index 7:", values[7])
# 7. Slicing
print("Slice from index 4 to 8:", values[4:9])
print("Slice from beginning to index 5:", values[:6])
print("Slice with every third element:", values[::3])
# 8. Find the Index of an Element
print("Index of value 90:", values.index(90))
Assignment 2: Dictionary-Based Data System
python
Copy code
# 1. Initialize Dictionary
students = {}
# 2. Add Student Information
students[101] = {'name': "Aarav", 'age': 20, 'courses': ['AIML', 'Data Structures']}
students[102] = {'name': "Ishita", 'age': 22, 'courses': ['NHCE', 'Discrete Math', 'Algorithms']}
students[103] = {'name': "Rahul", 'age': 21, 'courses': ['AI Ethics', 'New Horizon', 'Databases']}
# 3. Add New Courses
students[101]['courses'].append('Python Programming')
students[103]['courses'].append('Machine Learning')
# 4. Add Grades
students[101]['grades'] = {'AIML': 'A', 'Data Structures': 'B+'}
students[102]['grades'] = {'NHCE': 'A-', 'Discrete Math': 'B', 'Algorithms': 'A'}
students[103]['grades'] = {'AI Ethics': 'B+', 'New Horizon': 'A-', 'Databases': 'B'}
# 5. Access Data
print("Ishita's age:", students[102]['age'])
print("Rahul's grades:", students[103]['grades'])
print("Aarav's courses:", students[101]['courses'])
# 6. Update Information
students[102]['age'] = 23
students[103]['grades']['Databases'] = 'A+'
# 7. Remove a Course
students[102]['courses'].remove('Discrete Math')
del students[102]['grades']['Discrete Math']
# 8. Print All Student Information
for student_id, details in students.items():
print(f"ID: {student_id}, Name: {details['name']}, Age: {details['age']}")
print("Courses:", details['courses'])
print("Grades:", details['grades'])
Assignment 3: NumPy and Pandas Operations
1. NumPy Arithmetic Operations
python
Copy code
import numpy as np
# 1. Create a 2x2 array
arr1 = np.arange(4, dtype=np.float_).reshape(2, 2)
arr2 = np.array([5, 10])
# Perform operations
print("Addition:\n", np.add(arr1, arr2))
print("Subtraction:\n", np.subtract(arr1, arr2))
print("Multiplication:\n", np.multiply(arr1, arr2))
print("Division:\n", np.divide(arr1, arr2))
2. CSV Operations with Pandas
python
Copy code
import pandas as pd
# Create DataFrame and Save to CSV
scores_df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'Score': [75, 85, 95, 65, 55]})
scores_df.to_csv("students.csv", index=False)
scores_df[['Name']].to_csv("students_names.csv", index=False)
# Read and Display First 3 Rows
df = pd.read_csv("students.csv")
print(df.head(3))
# Increase Scores
df['Score'] = df['Score'].apply(lambda x: min(x + 10, 100))
df.to_csv("updated_students.csv", index=False)
Assignment 4: DataFrame and Visualization
python
Copy code
import pandas as pd
import matplotlib.pyplot as plt
data_india = [
{'Name': 'Aarav', 'Age': 20, 'Score': 85, 'City': 'Mumbai'},
{'Name': 'Vivaan', 'Age': 21, 'Score': 90, 'City': 'Delhi'},
{'Name': 'Aditya', 'Age': 19, 'Score': 95, 'City': 'Bengaluru'},
{'Name': 'Vihaan', 'Age': 18, 'Score': 80, 'City': 'Chennai'},
{'Name': 'Reyansh', 'Age': 22, 'Score': 88, 'City': 'Hyderabad'},
{'Name': 'Krishna', 'Age': 23, 'Score': 93, 'City': 'Ahmedabad'},
{'Name': 'Shivansh', 'Age': 24, 'Score': 78, 'City': 'Kolkata'},
{'Name': 'Ayaan', 'Age': 25, 'Score': 85, 'City': 'Pune'},
{'Name': 'Kabir', 'Age': 21, 'Score': 91, 'City': 'Jaipur'},
{'Name': 'Arjun', 'Age': 20, 'Score': 87, 'City': 'Lucknow'}
df = pd.DataFrame(data_india)
# Selecting Columns and Filtering Rows
print(df[['Name', 'Score']])
print(df[df['Score'] > 90])
# Line and Scatter Plots
plt.plot(df['Age'], df['Score'], label='Line Chart')
plt.scatter(df['Age'], df['Score'], color='red', label='Scatter Plot')
plt.xlabel('Age')
plt.ylabel('Score')
plt.legend()
plt.show()
Assignment 5: Handling Missing Data and Variability Analysis
python
Copy code
import pandas as pd
import numpy as np
# Data with missing values
ages = [23, 29, np.nan, 35, 22, 26, np.nan, 30, 32, 34]
df = pd.DataFrame(ages, columns=['Age'])
# Identify Missing Values
print("Missing Values:\n", df.isnull())
# Fill Missing Values with Mean
mean_age = df['Age'].mean()
df['Age'].fillna(mean_age, inplace=True)
replace nan directly with mean_age
print("Data after filling missing values:\n", df)
# Calculate Mean, Variance, and Standard Deviation
mean_age = df['Age'].mean()
variance_age = np.var(df['Age'])
std_dev_age = np.std(df['Age'])
print(f"Mean Age: {mean_age}")
print(f"Variance of Age: {variance_age}")
print(f"Standard Deviation of Age: {std_dev_age}")