One of the first things that you learn when you start to program is learning how to declare a variable and assign to it a value.
For example, you are told to assign a string and then print it in a console:
Now, if we want to assign another variable in there, our intuition can be to just add a new line, and assign it there:
There is a quicker way to do those assignments in Python, which you can use especially when you want to assign multiple values.
Here it is:
You can use the same when you also get values returned from methods, as you can see from the following example:
Remember that the number of variables should be equal to the number of elements in the list that you are getting elements from.
If you do not use the same number of variables, then you are going to get errors. For example, let us assume that we have the following example, where we are assigning one variable to two values:
When we try to print the result, we are going to get an error:
a, b, c = 1, 2, 3, 4
ValueError: too many values to unpack
That’s basically it for this article.
I hope you find this useful.