Skip to main content

Multi-assignment in Python

Multi-Assignment in Python


Hello guyz welcome back to thecodingproject after a loong loong  time 😌. This past couple of days I was very busy in my other projects and some personal stuff so could not get much time to post anything here but now I am back and I have another topic in #python to deal with.
Today we are going to explore what is “Multi-Assignment”. It’s better to get straight into an example. Let’s consider three variables k.l & m and a list [1,2,3]. Now when you do the following in the python prompt -
k,l,m = [1,2,3]
Print(k)
print(l)
print(m)

Output is :
1
2
3


Here each variable (k,l,m) are assigned to each element in the list in that order. That’s why when you type in k you get 1, l gets you 2 and m gets you 3. Instead when you do the following -

k,l,m = 1
You will get the following error-

Traceback (most recent call last):
 File "<input>", line 1, in <module>
TypeError: 'int' object is not iterable
The above error occurs because you now have three variable objects k,l & m but you want to assign only a single integer object 1 to all the three variables. So, python can’t iterate through a single object for all the three variables.

But when you do this instead-

k,l,m= 2,3,4
And then you type the following -

>>> k
2
>>> l
3
>>> m
4
The concept here is simple. In you example when you assign k,l & m to the list [1,2,3] then python iterates through the list and assigns the list elements to the variables in that order. In the last example when you assign k,l & m to the objects 2,3 & 4, python again iterates through the sequence of integer objects and assigns them to the variables. So, whenever you do such assignments python iterates through the sequence of objects and assigns the reference of the address of these objects to the variables one by one.


So, folks this was all for this week but more stuffs are coming your way next week, til then happy coding.

Subscribe to my youtube channel to watch new python programming tips and tutorials..

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.

Comments

Popular posts from this blog

What I learned from writing a guest post

                                                     Recently I wrote a python tutorial for Datacamp , while writing this tutorial I learned a couple of things which I am going to share with you right here in my blog post. May be you can use my experiences in your own endeavours as a writer. Idea The first step is idea generation. You can either search through any previous topics in your blog post if you have one or you can go through your past learning, experiences or any problem statement that you might have gone through and you will find that while doing that you can get tons of ideas to start your article. If you fall short of ideas then you can ask for help from may be your social media followings your friends or your family, for example I asked the people at Datacamp  themselves and they suggested me a couple of topics which got me st...

For Loop in python 3 : How to become a Python Jedi - Beginners Python Programming Tutorial - Part 7

I am back again with another part in the beginner python programming tutorial series. We will continue with "Loops" but this time we are going to learn how to use for loop in python 3.  Difference between For loop and While Loop A "For Loop" and "While Loop" have a common connection that both are looping statement, but major difference is that unlike while loop, a for loop gets executed a specified number of times. We can break down the For loop into the following components(Refer to the for loop flow chart below). for keyword variable name call to the range function colon operator clause or block of code Hovering over the flow-chart The above flow-chart is..well horrible I know, but I make my own drawings because I can't afford an artist. So, bear with me. In the above flowchart is a very basicy, laymanish representation of a for loop. The decision box contains the  For  statement along-with it's sou...

Expressions in Python : How to become a Python programming Jedi - A Python programming Tutorial Part 1

Long Long ago in a Galaxy far far away there was a planet named Quora. There was a growing agitation among the planet’s inhabitants to learn the way of the Python programming Force and become a master in the way of the Python programming Jedi, but very few knew that the secret art of the Jedi is embedded in a book named as TheCodingProject…. So, let’s begin the journey of learning the secret art of the Python programming Force and we shall together make the journey towards becoming the Python Jedi…. What is Python? As per Wikipedia “Python is a widely used high-level , general-purpose , interpreted , dynamic programming language .” Python is an open source programming language which was conceived in the late 1980s and its implementation began in December 1989 by Guido van Rossum (more on it here ). For us it’s sufficient to know that Python is : High level language - Python is a high-level programming language because it provides strong “:abstractio...