02 Oct Modules, Libraries
LAB 1: Write a Python function math_operations(num: int) -> dict that takes a user input integer num and returns a dictionary containing: The square root of num. The sine of 90 degrees. The factorial of num. Constraint num will be a non-negative integer between 1and 100. For sine calculation, convert degrees to radians using the math.radians() function. Use the math module to perform these operations.
import math
def math_operations(num):
if num < 0 or num > 100:
raise ValueError("Number must be between 1 and 100.")
square_root = math.sqrt(num)
sine_90 = math.sin(math.radians(90))
factorial = math.factorial(num)
results = {
"square_root": square_root,
"sine_90": sine_90,
"factorial": factorial
}
return results
num = 5
results = math_operations(num)
print(results)
LAB2: Write a Python function get_current_datetime() -> dict that returns a dictionary containing: The current date. The current time. The current date and time formatted as YYYY-MM-DD HH:MM:SS. Constraint Use the datetime module to get the current date and time. The function should not accept any arguments.
import datetime
def get_current_datetime():
now = datetime.datetime.now()
date = now.strftime("%Y-%m-%d")
time = now.strftime("%H:%M:%S")
formatted_datetime = now.strftime("%Y-%m-%d %H:%M:%S")
results = {
"date": date,
"time": time,
"formatted_datetime": formatted_datetime
}
return results
current_datetime = get_current_datetime()
print(current_datetime)
LAB3 : Write a Python function file system operations() -> dict that performs the following operations using the os module and returns a dictionary with: The current working directory. Alist of files in the current directory. A confirmation message after creating a new directory called new folder". Constraint. Use the os module for all file system operations. The function should not accept any arguments.
import os
def file_system_operations() -> dict:
try:
current_directory = os.getcwd()
files_in_directory = os.listdir()
os.mkdir('new_folder')
confirmation_message = "new_folder created successfully."
return{
"current_directory": current_directory,
"files_in_directory": files_in_directory,
"confirmation_message": confirmation_message
}
except FileExistsError:
return{
"error":"Directory already exist"
}
except Exception as e:
return{
"error": str(e)
}
print(file_system_operations())
LAB 4: Write a Python function numpy_operations() -> dict that performs the following operations using the NumPy library: Create a NumPy array [1, 2, 3, 4, 51. Add 10 to each element in the array. Calculate and return the mean of the original array. The function should return a dictionary with the modified array and the mean. Constraint Use the numpy library for all operations. The function should not accept any arguments.
import numpy as np
def numpy_operations():
array = np.array([1, 2, 3, 4, 5])
mean_value = np.mean(array)
modified_array = array + 10
return {
'modified_array': modified_array,
'mean': mean_value
}
result = numpy_operations()
print(result)
LAB 5 : Write a Python function pandas_operations() -> dict that performs the following operations using the Pandas library: Create a DataFrame with the following data: {"Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35]} Filter the DataFrame to include only rows where the Age is greater than 28. Calculate the sum of the Age column. The function should return a dictionary containing the filtered DataFrame and the sum of ages. Constraint Use the pandas library for data manipulation. The function should not accept any arguments.
import pandas as pd
def pandas_operations():
data = {"Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35]}
df = pd.DataFrame(data)
filtered_df = df[df['Age'] > 28]
age_sum = df['Age'].sum()
return {
'filtered_df': filtered_df,
'age_sum': age_sum
}
result = pandas_operations()
print(result)
.png)