Varnitjain 2300290120277 Cs3d Pythonassignment02
Varnitjain 2300290120277 Cs3d Pythonassignment02
2300290120277
CS3D
PYTHON LAB ASSIGNMENT 02
1)
2)
import string
word_counts = {}
for word in words:
word_counts[word] = word_counts.get(word, 0) + 1
3)
library_catalog = {}
search_title = "1984"
if search_title in library_catalog:
print(f"Book: {search_title}, Author: {library_catalog[search_title]}")
else:
print("Book not found.")
4)
tasks = []
print("To-Do List:")
for idx, task in enumerate(tasks, 1):
print(f"{idx}. {task}")
5)
inventory = {}
new_product = "Laptop"
new_quantity = 50
new_supplier = "TechSupplier Inc."
inventory[new_product] = {'quantity': new_quantity, 'supplier': new_supplier}
with open('cpy.txt', 'w') as file:
for product, details in inventory.items():
file.write(f"{product},{details['quantity']},{details['supplier']}\n")
print("Inventory List:")
for product, details in inventory.items():
print(f"Product: {product}, Quantity: {details['quantity']}, Supplier: {details['supplier']}")
6)
import requests
url = "https://api.exchangerate-api.com/v4/latest/USD"
response = requests.get(url)
data = response.json()
7)
import time
import threading
from datetime import datetime
while True:
current_time = datetime.now().strftime("%H:%M:%S")
print(f"Current Time: {current_time}", end='\r')
time.sleep(1)
alarm_time = input("Enter alarm time in HH:MM:SS format or press Enter to skip: ")
if alarm_time:
while True:
current_time = datetime.now().strftime("%H:%M:%S")
print(f"Current Time: {current_time}", end='\r')
time.sleep(1)
if current_time == alarm_time:
print(f"Alarm! It's {alarm_time}!")
break
8)
import math
while True:
op = input("\n1. + 2. - 3. * 4. / 5. sqrt 6. ! 7. Exit\nChoose operation: ")
if op == "1":
num1, num2 = input("Enter two numbers: ").split()
print(float(num1) + float(num2))
elif op == "2":
num1, num2 = input("Enter two numbers: ").split()
print(float(num1) - float(num2))
elif op == "3":
num1, num2 = input("Enter two numbers: ").split()
print(float(num1) * float(num2))
elif op == "4":
num1, num2 = input("Enter two numbers: ").split()
print(float(num1) / float(num2) if float(num2) != 0 else "Error")
elif op == "5":
num = float(input("Enter number: "))
print(math.sqrt(num) if num >= 0 else "Error")
elif op == "6":
num = int(input("Enter integer: "))
print(math.factorial(num) if num >= 0 else "Error")
elif op == "7":
break
else:
print("Invalid")
10)
while True:
print("\n1. Encrypt")
print("2. Decrypt")
print("3. Exit")
if choice == '1':
message = input("Enter message: ")
key = int(input("Enter key: "))
encrypted = ''.join([chr((ord(c) + key - 32) % 95 + 32) for c in message])
print(f"Encrypted: {encrypted}")
11)
balance = 0
while True:
print("\n1. Deposit")
print("2. Withdraw")
print("3. Check Balance")
print("4. Exit")
if choice == '1':
amount = float(input("Enter deposit amount: "))
balance += amount
print(f"Deposited: {amount}")
12)
employees = []
while True:
print("\n1. Add Employee")
print("2. View Employees")
print("3. Exit")
if choice == '1':
name = input("Enter employee name: ")
emp_id = input("Enter employee ID: ")
salary = float(input("Enter salary: "))
employees.append({"Name": name, "ID": emp_id, "Salary": salary})
print("Employee added.")
13)
import pandas as pd
data = None
while True:
print("\n1. Load CSV 2. Filter 3. Sort 4. Handle Missing 5. Exit")
choice = input("Choose an option: ")
if choice == '1':
data = pd.read_csv(input("Enter CSV file path: "))
print(data)
elif choice == '2' and data is not None:
col, val = input("Enter column and value to filter: ").split()
print(data[data[col] == val])
elif choice == '3' and data is not None:
col = input("Enter column to sort: ")
print(data.sort_values(by=col))
elif choice == '4' and data is not None:
print(data.fillna("N/A"))
elif choice == '5':
break
else:
print("Invalid or no data loaded.")
14)
import pandas as pd
import matplotlib.pyplot as plt
plt.figure()
if plot_type == "Line":
plt.plot(data[x_col], data[y_col], color=color)
elif plot_type == "Bar":
plt.bar(data[x_col], data[y_col], color=color)
elif plot_type == "Scatter":
plt.scatter(data[x_col], data[y_col], color=color)
elif plot_type == "Histogram":
plt.hist(data[y_col], bins=10, color=color)
plt.title(title)
plt.xlabel(x_col)
plt.ylabel(y_col)
plt.legend([plot_type])
plt.show()
15)
import numpy as np
import matplotlib.pyplot as plt