+91 81900 98289

Assign Multiple Values

This Assign Multiple Values lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

One assignment can bind several names. The count of names on the left must match the count of values on the right.

The same value can be given to several names by chaining =. Later changes to one name do not change the others when the value is a number or text.

Example 1: Start here

Unpacking a list uses the same shape: first, second = [\"Python\", \"SQL\"].

name, city, score = "Meera", "Chennai", 82 # text print(name, score) # a comma prints each value, with a space between

Example 2: Unpack three names

The left side and the right side must have the same number of items.

name, city, score = "Asha", "Chennai", 82 # text print(name, city, score) # a comma prints each value, with a space between

Example 3: Give two names the same number

Both names start at 10. Changing one later does not change the other.

a = b = 10 # both names get the same value print(a, b) # a comma prints each value, with a space between

Example 4: Swap two names

Python swaps the two values in one assignment.

left, right = "SQL", "Python" # text left, right = right, left # swap the two values print(left, right) # a comma prints each value, with a space between
Unpacking a list uses the same shape: first, second = [\"Python\", \"SQL\"].
Call Now 81900 98289