All Posts

Python Multiple Assignments

Python Multiple Assignments

In Python, you can use multiple assignments to assign values to multiple variables in a single line. This can make your code more concise and readable.

Multiple Assignments

Python allows us to assign the same value to multiple variables.

For Example

Consider the following statement:

a=b=c=5

This statement will assign value 5 to all three variables in a single statement.

In the normal approach, we use different statements to assign values.

# Assigning values in different statements
a = 1
b = 2
c = 3

We can also assign different values to multiple variables. Assigning multiple values in a single statement.

For example:

Consider the following statement:

# Multiple assignment
a, b, c = 1, 2, 3

This statement will assign value 1 to a variable, value 2 to b variable, and 3  to the c variable.

Unpacking

This feature is also used for unpacking lists and tuples.

# Unpacking a list
numbers = [1, 2, 3]
x, y, z = numbers

 

Python Multiple Assignments

It’s a powerful feature that can enhance the readability of your code when used appropriately.

—

Python Tutorials

Python Tutorial on this website can be found at:

https://www.testingdocs.com/python-tutorials/

More information on Python is available at the official website:

https://www.python.org