# (Details of the student submitting the exam answers)
# [Student's Full Name]
# [Student ID]
students = [
("ID2023JM", "Javohir", "Muminov"),
("ID2023NQ", "Nigora", "Qosimova"),
("ID2023MU", "Mirzo", "Usmonov"),
("ID2023DK", "Dilnoza", "Karimova"),
("ID2023BM", "Behzod", "Mardonov")
]
# Part 1
# Task 1
# Demonstrate how you would access and print the
# full name and the corresponding ID of
# the third student in the list:
third_student = students[2]
print(f"Full name: {third_student[1]} {third_student[2]} \nID: {third_student[0]}")
# Task 2
# This information is detailed for student's reference to review
# ! You don't necessarily had to cover this many words in your exam solution
'''
1. Mutability:
- List: Lists are mutable, meaning you can change, add, or remove elements after the list is created.
- Tuple: Tuples are immutable, meaning once a tuple is created, you cannot change, add, or remove elements.
2.Syntax:
- List: Lists are enclosed in square brackets [].
- Tuple: Tuples are enclosed in parentheses ().
1. Lists when mutability is necessary:
If you need to modify the collection of items over time, use a list. For example,
if you're keeping track of items in a shopping cart and need to add or remove items dynamically,
a list is a better choice:
shopping_cart = ["apple", "banana", "orange"]
shopping_cart.append("grapes")
2. Use Tuples for Immutable Collections:
If the collection of items should not change throughout the program's execution, use a tuple. For example,
if you're defining coordinates of a point in 2D space, a tuple is suitable.
point = (3, 5)
Performance Considerations:
Tuples are generally faster than lists because they are immutable. If you have a collection of data that doesn't need to
be changed, using a tuple might provide a slight performance boost.
For large datasets where performance is critical and the data doesn't need to change, using tuples can be advantageous.
Data Integrity and Safety:
If you want to ensure that certain data remains unchanged throughout the execution of your program to prevent bugs or
unintended modifications, using tuples can provide a level of safety.
For example, if you're passing data between different parts of your program and want to ensure that it remains intact,
using tuples can help enforce this.
Here's an example illustrating the difference between a list and a tuple:
'''
# Using a list for a shopping cart:
shopping_cart = ["apple", "banana", "orange"]
shopping_cart.append("grapes")
print(shopping_cart) # Output: ["apple", "banana", "orange", "grapes"]
# Using a tuple for coordinates
point = (3, 5)
# Trying to modify the tuple will result in an error
# point[0] = 4 # This will raise a TypeError'''
# Task 3
'''
1. Key-Value Pairing:
Using dictionaries makes the code more readable and self-explanatory,
as you can access values by their meaningful keys rather than relying on positional indices.
2. Flexibility and Extensibility:
You can easily add or remove fields without restructuring the entire data structure.
Example you could provide: library of books using lists and dictionaries
'''
# Part 2
# Task 1
students = {
"ID2023JM": {
"first_name": "name 1",
"last_name": "last name 1",
"scores": {
"math": 85,
"science": 90,
"history": 78,
}
},
"ID2023HM": {
"first_name": "Javokhir",
"last_name": "last name 1",
"scores": {
"math": 76,
"science": 55,
"history": 78,
}
}
}
# Demonstrate how you would access and print the exam scores of a specific student in the dictionary.
# Use the details of the students provided below.
# ID: "ID2023JM"
# student_id = input("Enter the ID of the student: ")
student_id = "ID2023JM"
print(f"Student ID: {students[student_id]}"
f"Full name: {students[student_id]['first_name']} {students[student_id]['last_name']}"
f"Scores: "
f"Math: {students[student_id]['scores']['math']}"
f"Science: {students[student_id]['scores']['science']}"
f"History: {students[student_id]['scores']['history']}")
# Task 2
# Find the collection of unique scores and print
unique_scores = set()
for student_info in students.values():
unique_scores.update(student_info["scores"].values())
print(f"Unique scores: \n{unique_scores}")
# Task 3
# (1) iterates over subjects for each student
# in the student database.
# For each student,
# (4) print a message indicating
# whether they passed all subjects:
# (3) scored 60 or above in each subject
# or if they need improvement
# (2) scored below 60 in at least one subject
# You can use print message to see the item each time
# for key in students:
for student in students:
# print(student)
scores = students[student]["scores"]
count = 0
for subject in scores:
# print("subject is: ", subject)
# OR
# print(scores[subject])
if scores[subject] >= 60:
count += 1
if count < 3:
print(f"The student {student} needs an improvement")
else:
print(f"The student {student} passed all subjects")
# Option 2:
for student in students:
passed_all = True
scores = students[student]["scores"]
for subject in scores:
if scores[subject] < 60:
passed_all = False
break
if passed_all:
print(f"{student} passed all subjects.")
else:
print(f"{student} needs improvement.")
# Task 4
# (1) iterate over the student database dictionary
# (3) print each student's name along with
# (2) their average exam score
subject_count = 3
for studentID in students:
scores = students[studentID]['scores']
total = 0
for subject in scores:
total += scores[subject]
average = total / subject_count
print(f"Student's name: {students[studentID]['first_name']} "
f"{students[studentID]['last_name']}"
f"Student's average score: {average:.2f}"
)
# Task 5
'''
pop()
1. deletes one item at a time
2. deletes and returns element
3. if index is not provided, deletes the last element
'''
fruits = ["apple", "pineapple", "orange"]
deleted_fruit = fruits.pop()
print("I deleted the last item: ", deleted_fruit)
'''
del
1. deletes the element and doesn't return
2. can delete the entire list at once
'''
del fruits[0]
print("I deleted the first item: ", fruits)
del fruits
print("I deleted the list and it's not accessible anymore: ")
# Task 6
# Provide a code example demonstrating the removal of a student by name
# name_to_delete = input("Enter a name of the student you want to remove")
name_to_delete = "Javokhir"
ID_to_delete = ''
for studentID in students:
if students[studentID]["first_name"] == name_to_delete:
ID_to_delete = studentID
break
del students[ID_to_delete]
print(students)
# ! this code removes first Javokhir found in DB