8/27/25, 2:39 PM Assignment1.
ipynb - Colab
keyboard_arrow_down Python Assignment 1
# Q1: Write a Python program to input 5 subject marks from a user, calculate
# total, average, and percentage, and display results in a formatted way.
print("Enter marks for 5 subjects:")
subject1 = float(input("Subject 1: "))
subject2 = float(input("Subject 2: "))
subject3 = float(input("Subject 3: "))
subject4 = float(input("Subject 4: "))
subject5 = float(input("Subject 5: "))
total = subject1 + subject2 + subject3 + subject4 + subject5
average = total / 5
percentage = (total / 500) * 100
# Display results
print("\n----- Result -----")
print("Total Marks :",total)
print("Average Marks :", average)
print("Percentage :", format(percentage,".2f"),"%")
Enter marks for 5 subjects:
Subject 1: 85
Subject 2: 96
Subject 3: 45
Subject 4: 22
Subject 5: 17
----- Result -----
Total Marks : 265.0
Average Marks : 53.0
Percentage : 53.00 %
# Q2: Write a Python program to check whether a given number is prime or not.
num = int(input("Enter a number: "))
if num <= 1:
print(f"{num} is not a prime number.")
else:
is_prime = True
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
Enter a number: 26
26 is not a prime number.
# Q3: Create a Python list of 10 integers. Write functions to:
# Find the maximum and minimum values.
# Sort the list in ascending order (without using built-in `sort ()`).
numbers = [12, 5, 23, 7, 18, 9, 3, 21, 1, 14]
max_val = numbers[0]
for num in numbers:
if num > max_val:
max_val = num
min_val = numbers[0]
for num in numbers:
if num < min_val:
min_val = num
for i in range(len(numbers)):
for j in range(i + 1, len(numbers)):
if numbers[i] > numbers[j]:
numbers[i], numbers[j] = numbers[j], numbers[i]
[Link] 1/3
8/27/25, 2:39 PM [Link] - Colab
# Display results
print("Maximum:", max_val)
print("Minimum:", min_val)
print("Sorted List:", numbers)
Maximum: 23
Minimum: 1
Sorted List: [1, 3, 5, 7, 9, 12, 14, 18, 21, 23]
# Q4: Write a Python function that takes a string and returns the number of
# vowels, consonants, digits, and special characters
vowel_letters = "aeiouAEIOU"
consonant_letters = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
digit_chars = "0123456789"
text = input("Enter a string: ")
vowels = 0
consonants = 0
digits = 0
special_chars = 0
for char in text:
if char in vowel_letters:
vowels += 1
elif char in consonant_letters:
consonants += 1
elif char in digit_chars:
digits += 1
elif char != " ":
special_chars += 1
# Display the result
print("Vowels :", vowels)
print("Consonants :", consonants)
print("Digits :", digits)
print("Special Chars :", special_chars)
Enter a string: Hello, I am "Devansh Gupta", my Class Roll No. is 119/23
Vowels : 12
Consonants : 23
Digits : 5
Special Chars : 6
'''Q5: A small company has employee data stored as a dictionary:
python employees = {
a. 101: {"name": "Alice", "salary": 50000},
b. 102: {"name": "Bob", "salary": 60000},
c. 103: {"name": "Charlie", "salary": 55000}
d. }
Write a Python program to:
Add a new employee.
Update an employee’s salary.
Delete an employee record.
Display all employee details in a tabular format.'''
# Initial employee data
employees = {
101: {"name": "Alice", "salary": 50000},
102: {"name": "Bob", "salary": 60000},
103: {"name": "Charlie", "salary": 55000}
}
# 1. Add a new employee
print("\nAdd a New Employee:")
new_id = int(input("Enter new employee ID: "))
new_name = input("Enter employee name: ")
new_salary = int(input("Enter employee salary: "))
employees[new_id] = {"name": new_name, "salary": new_salary}
print("Employee added successfully.")
# 2. Update an employee's salary
print("\nUpdate Employee Salary:")
update_id = int(input("Enter employee ID to update salary: "))
if update_id in employees:
new_salary = int(input("Enter new salary: "))
employees[update_id]["salary"] = new_salary
print("Salary updated successfully.")
else:
[Link] 2/3
8/27/25, 2:39 PM [Link] - Colab
print("Employee ID not found.")
# 3. Delete an employee
print("\nDelete an Employee:")
delete_id = int(input("Enter employee ID to delete: "))
if delete_id in employees:
del employees[delete_id]
print("Employee deleted successfully.")
else:
print("Employee ID not found.")
# 4. Display all employee details
print("\nEmployee Details:")
print("{:<10} {:<15} {:<10}".format("ID", "Name", "Salary"))
print("-" * 35)
for emp_id, info in [Link]():
print("{:<10} {:<15} {:<10}".format(emp_id, info["name"], info["salary"]))
Add a New Employee:
Enter new employee ID: 104
Enter employee name: Devansh Gupta
Enter employee salary: 65000
Employee added successfully.
Update Employee Salary:
Enter employee ID to update salary: 104
Enter new salary: 70000
Salary updated successfully.
Delete an Employee:
Enter employee ID to delete: 103
Employee deleted successfully.
Employee Details:
ID Name Salary
-----------------------------------
101 Alice 50000
102 Bob 60000
104 Devansh Gupta 70000
[Link] 3/3