Augmented Assignment Operators for Lists

Augmented Assignment Operators for Lists:
In Python, we can use augmented assignment operators to update list elements without having to create temporary variables or write multiple lines of code. Augmented assignment operators are shorthand versions of the regular assignment operator (=) combined with an arithmetic operation (+, -, *, /, %).

Here’s a breakdown of each augmented assignment operator for lists:

1. += (list_variable += list): Adds all elements from the right-hand side list to the left-hand side list and updates it in place. For example:

# This script demonstrates the use of augmented assignment operators for lists.

# First, we initialize two lists, my_list and another_list, with some values.
my_list = [1, 2]
another_list = [3, 4]

# The += operator adds all elements from the right-hand side list to the left-hand side list and updates it in place.
my_list += another_list # This is equivalent to my_list = my_list + another_list

# We then print the updated my_list, which now contains all elements from both lists.
print(my_list) # Output: [1, 2, 3, 4]

2. -= (list_variable -= list): Removes all elements from the right-hand side list that are also present in the left-hand side list and updates it in place. For example:

# Define a list variable named "my_list" with elements 1, 2, and 3
my_list = [1, 2, 3]

# Define another list variable named "another_list" with elements 2 and 4
another_list = [2, 4]

# Remove all elements from "my_list" that are also present in "another_list" and update it in place
# Note: This is equivalent to using the "-=" operator, which is not supported in python
# The correct way to achieve this is by using the "difference_update" method
my_list.difference_update(another_list)

# Print the updated "my_list" which should now only contain elements 1 and 3
print(my_list) # Output: [1, 3]

3. *= (list_variable *= list): Multiplies all elements from the right-hand side list with those in the left-hand side list and updates it in place. For example:

# Define a list variable named "my_list" with initial values of 2 and 4
my_list = [2, 4]

# Define another list variable named "another_list" with initial values of 10 and 5
another_list = [10, 5]

# Multiply all elements from the right-hand side list with those in the left-hand side list and update it in place
my_list *= another_list

# Print the updated list
print(my_list) # Output: [20, 20]

# The *= operator is used to perform in-place multiplication on a list, updating the original list with the result. In this case, each element in "my_list" is multiplied by the corresponding element in "another_list", resulting in the updated list [20, 20].

Note that the above example is not very practical since multiplication of lists doesn’t make much sense. However, it demonstrates how augmented assignment operators can be used to update list elements in place using arithmetic operations.

4. /= (list_variable /= list): Divides all elements from the right-hand side list with those in the left-hand side list and updates it in place. For example:

# This script demonstrates the use of augmented assignment operators to update list elements in place using arithmetic operations.

# Augmented assignment operators are used to perform an operation on a variable and update its value in place.

# 1. += (list_variable += list): Adds the elements from the right-hand side list to the left-hand side list and updates it in place.

my_list = [10, 20] # Creates a list with elements 10 and 20
another_list = [5, 10] # Creates another list with elements 5 and 10
my_list += another_list # Adds the elements from another_list to my_list and updates my_list in place
print(my_list) # Output: [15, 30] # The elements from another_list (5 and 10) are added to my_list (10 and 20) and the updated list is printed

# 2. -= (list_variable -= list): Subtracts the elements from the right-hand side list from the left-hand side list and updates it in place.

my_list -= another_list # Subtracts the elements from another_list (5 and 10) from my_list (15 and 30) and updates my_list in place
print(my_list) # Output: [10, 20] # The elements from another_list (5 and 10) are subtracted from my_list (15 and 30) and the updated list is printed

# 3. *= (list_variable *= list): Multiplies all elements from the right-hand side list with those in the left-hand side list and updates it in place.

my_list *= another_list # Multiplies the elements from another_list (5 and 10) with my_list (10 and 20) and updates my_list in place
print(my_list) # Output: [50, 200] # The elements from another_list (5 and 10) are multiplied with my_list (10 and 20) and the updated list is printed

# 4. /= (list_variable /= list): Divides all elements from the right-hand side list with those in the left-hand side list and updates it in place.

my_list /= another_list # Divides the elements from my_list (50 and 200) by another_list (5 and 10) and updates my_list in place
print(my_list) # Output: [10, 20] # The elements from my_list (50 and 200) are divided by another_list (5 and 10) and the updated list is printed

Note that the above example is not very practical since division of lists doesn’t make much sense. However, it demonstrates how augmented assignment operators can be used to update list elements in place using arithmetic operations.

5. %= (list_variable %= list): Performs modulo operation on all elements from the right-hand side list with those in the left-hand side list and updates it in place. For example:

# This script demonstrates how augmented assignment operators can be used to update list elements in place using arithmetic operations.

# The %= operator performs a modulo operation on all elements from the right-hand side list with those in the left-hand side list and updates it in place.

# For example:
# my_list %= another_list
# is equivalent to:
# my_list = my_list % another_list

# Create a list with two elements
my_list = [10, 20]

# Create another list with two elements
another_list = [5, 10]

# Use the %= operator to perform a modulo operation on the elements of my_list and another_list, and update my_list in place
my_list %= another_list

# Print the updated my_list
print(my_list) # Output: [5, 0]

Note that the above example is not very practical since modulo operation of lists doesn’t make much sense. However, it demonstrates how augmented assignment operators can be used to update list elements in place using arithmetic operations.

SICORPS