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]

Output is :

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
>>> l
>>> m
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.


Popular posts from this blog

How To Install Tensorflow On Windows

Why I am writing this tutorial ?
Recently I have started learning machine learning specifically DeepLearning for one of my pet projects, and during this quest of mine I wanted to learn TensorFlow library which is developed by Google. Learning is one thing but first I needed to install the library, but there was this problem which I faced during the installation. I am using Anaconda as my package manager and trying to install the Tensorflow library on my work computer which is behind a proxy, installing directly from the anaconda cloud while behind a proxy always displayed a proxy error.

How did I resolve the issue? Well this is exactly what I am going to write down in the below tutorial. The Steps To Follow
Before we get all cosy with this short tutorial on Tensorflow installation, let me tell you that
this tutorial is on the assumption that you have already installed Anaconda on your machine and you know at least a few basic things or two about python.
If you want to learn basic pyt…

Collatz sequence python : Monday fun with Python

Hello everyone. Today is Monday..well not a big deal and I know everyone knows today is a Monday. Like you I'm not a big fan of Monday, although nowadays I'm waiting eagerly for this day of the week as I get to watch a Game of Thrones episode on this day and I'm sure most of you can reciprocate the same feeling. The only thing I hate the most is the long wait till Monday night till I return to home from office and get to watch that episode for which I wait for one whole week and here I am in office waiting for the day to get over soon.

The plan To kill the time I do one of the thing that I love the most. Yes, you guessed right? I code. So, I decided to fool around some python programming code to kill the boredom. Without much ado let's code :) I decided to device a python program that would help me to devise a "collatz sequence" In case you are thinking what a "collatz sequence" is then dive into this link here.

For more understanding on python check…
Encountering The Python
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).
What kind of programing language?To give a better idea let's look at the below mentioned summary-
High level language- Python is a high-level language because it provides strong “abstraction”.Interpreted - Python is an Interpreted language because it does not run on a compiler.Dynamically typed language - Python is dynamically typed because in python every variable is bound to an object.Why should I learn Python?Python is a very powerful programming language, secondly it's easier to learn and since it's interpreted language the code is easier to understand. Lastly there is a huge community supporting Python so if you get stuck anywhere lot's of help are a…