My QA Projects

QA Projects I was involded.

View on GitHub

Examples - Replacing

1. Replacing Items in a List

my_list = [1, 2, 3, 4, 5]

# Replace an item at a specific index
my_list[2] = 10
print("List after replacing an item:")
print(my_list)  # Output: [1, 2, 10, 4, 5]

# Replace multiple items using slicing
my_list[1:4] = [20, 30, 40]
print("\nList after replacing multiple items:")
print(my_list)  # Output: [1, 20, 30, 40, 5]

2. Replacing Items in a Set

# Original set
my_set = {1, 2, 3, 4, 5}

# Remove an item and add a new item
my_set.remove(3)
my_set.add(10)
print("Set after replacing an item:")
print(my_set)  # Output: {1, 2, 4, 5, 10}

3. Replacing Items in a Dictionary

# Original dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}

# Replace value for a specific key
my_dict['b'] = 20
print("Dictionary after replacing an item:")
print(my_dict)  # Output: {'a': 1, 'b': 20, 'c': 3}

# Add a new key-value pair
my_dict['d'] = 4
print("\nDictionary after adding a new item:")
print(my_dict)  # Output: {'a': 1, 'b': 20, 'c': 3, 'd': 4}

# Remove an item by key
del my_dict['a']
print("\nDictionary after deleting an item:")
print(my_dict)  # Output: {'b': 20, 'c': 3, 'd': 4}

4. Replacing Items in a tuple

# List of dictionaries representing students' information
students = [
    {'id': 1, 'name': 'Alice', 'grade': 'A'},
    {'id': 2, 'name': 'Bob', 'grade': 'B'},
    {'id': 3, 'name': 'Charlie', 'grade': 'C'}
]

# Function to replace/update student information based on ID
def update_student_info(student_list, student_id, new_name, new_grade):
    for student in student_list:
        if student['id'] == student_id:
            student['name'] = new_name
            student['grade'] = new_grade
            break  # Assuming IDs are unique, we can stop iterating once we find the student

# Update student with ID 2 to change their name and grade
update_student_info(students, 2, 'Barbara', 'A')

# Print updated list of students
print("Updated list of students:")
for student in students:
    print(student)

5. Replacing Items in a tuple

# Original tuple
my_tuple = (1, 2, 3, 4, 5)

# Creating a new tuple with a different item at a specific index
new_tuple = my_tuple[:2] + (10,) + my_tuple[3:]
print("New tuple after 'replacing' an item:")
print(new_tuple)  # Output: (1, 2, 10, 4, 5)

# Another example: replacing multiple items
new_tuple = my_tuple[:1] + (20, 30) + my_tuple[3:]
print("\nNew tuple after 'replacing' multiple items:")
print(new_tuple)  # Output: (1, 20, 30, 4, 5)