How to Assign Multiple Values in Python

In this tutorial, we are going to learn about assigning multiple values in Python. This is going to be a step-by-step guide for beginners who have little or no prior knowledge of Python.

We will cover different methods to assign multiple variables including assigning multiple values to multiple variables and assigning the same value to multiple variables in Python.

1. Assigning Multiple Values to Multiple Variables

Python allows you to assign multiple values to multiple variables in a very simple and intuitive manner. Here’s an example:

In this case, “Hello” is assigned to x, “Python” is assigned to y, and “World” is assigned to z.

2. Assigning the Same Value to Multiple Variables

In Python, you can assign the same value to multiple variables at once. Here’s how it can be done:

In this case, “Python” is assigned to all the variables x, y, and z. The output for the same will be:

3. Assigning Multiple Values from the List

You can also assign multiple values from a list to multiple variables. This can be done in the following way:

Here, the values from the list are assigned to the variables x, y, and z in an iterative manner. The output will be:

4. Assigning Values from Tuple

You can similarly assign values to variables from a tuple of values. Here is an example:

Here, 4 will be assigned to x, 5 will be assigned to y, and 6 to z. The output for the same will be:

Full Code

Conclusion

Assigning multiple values to variables in Python can be achieved in many ways and the choice of method depends on the specific requirements of the code. This assignment operation aids in the readability of the code and helps in reducing the overall line of code in the software development process.