Buy Me a Coffee

You can assign a value to one or more variables as shown below:

v = 5

print(v) # 5

v = 10

print(v) # 10

*str type cannot be changed by accessing each character so use list() and join() to do that.

v = "Orange"

print(v, v[0], v[1], v[2], v[3], v[4], v[5]) # Orange O r a n g e

v = "Lemon"

print(v, v[0], v[1], v[2], v[3], v[4]) # Lemon L e m o n

v[2] = "M" # TypeError: 'str' object does not support item assignment

# ---------------------------------------------------------------------- #

v = "Lemon"

print(v, v[0], v[1], v[2], v[3], v[4]) # Lemon L e m o n

v = list(v) # Change `str` type to `list` type.

print(v, v[0], v[1], v[2], v[3], v[4]) # ['L', 'e', 'm', 'o', 'n'] L e m o n

v[2] = "M"

print(v, v[0], v[1], v[2], v[3], v[4]) # ['L', 'e', 'M', 'o', 'n'] L e M o n

v = ''.join(v) # Change `list` type to `str` type.

print(v, v[0], v[1], v[2], v[3], v[4]) # LeMon L e M o n
# Equivalent
v1 = v2 = v3 = 5 # v1 = 5
                 # v2 = v1
                 # v3 = v2
print(v1, v2, v3) # 5, 5, 5

v2 = 10

print(v1, v2, v3) # 5, 10, 5

*The reference of a list is stored in a variable so the variables v1, v2 and v3 have the same references of a list, that's why changing the list content of v2 also changes the other list content of v1 and v3.

# Equivalent
v1 = v2 = v3 = [0, 1, 2, 3, 4] # v1 = [0, 1, 2, 3, 4]
                               # v2 = v1
                               # v3 = v2
print(v1, v2, v3) # [0, 1, 2, 3, 4] [0, 1, 2, 3, 4] [0, 1, 2, 3, 4]

v2[2] = 20 # Changes the same list content by the same reference as v1 and v3.

print(v1, v2, v3) # [0, 1, 20, 3, 4] [0, 1, 20, 3, 4] [0, 1, 20, 3, 4]

v2 = list(v2) # v2 has the different reference of the different list
              # from v1 and v3.

v2[2] = 200 # Changes a different list content by the different reference
            # from v1 and v3.

print(v1, v2, v3) # [0, 1, 20, 3, 4] [0, 1, 200, 3, 4] [0, 1, 20, 3, 4]

v3 = 50 # Changes a list reference itself.

print(v1, v2, v3) # [0, 1, 20, 3, 4] [0, 1, 200, 3, 4] 50