Click here to Skip to main content
15,617,173 members
Articles / Programming Languages / Python
Technical Blog
Posted 7 Oct 2013

Tagged as


5 bookmarked

Python Basics: Understanding the Variables

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Oct 2013CPOL6 min read
An introduction to some Python data types and data structures.


This article is very basic and it provides an introduction to some Python data types and data structures. In this article we will also learn how to use them and the differences among them. Not only that, we also have a look at key points that one should always remember before choosing data types. Basically in this article we will learn about Numbers, Strings and Lists. Before starting this let us first understand about variables in Python.

Preparing the Machine 

Before starting anything you need to install the Python interpreter in your machine There are many ways of doing it and I’m explaining one of them. 

Python in Visual Studio (recommended)

To install Python in Visual Studio use the following procedure:

  1. Download Python tools from this URL: Python tools

  2. Choose your version and click on the download button.

  3. After completion of the download, close all your Visual Studio processes.
  4. Start the installer and install it the same as other software.

  5. After the installation is completed open Visual Studio.
  6. Click on “New project” and click on “Other languages” then choose “Python” and “Python application template”. 

  7. Write your code and press F5 to debug it.


Python Shell (Required) 

  1. Download the shell from here.

  2. Start the installer and install it as same as other software.

  3. To run the shell just launch IDLE (Python GUI).

  4. Just type some code and you are done.

  5. For this article, you are required to write the code in a Python file. For that just create a new text document and save it with a .py extension.

  6. To run that file just click “Run” -> “Run module” or press F5.


Variables (Names) in Python

No matter what language you are working in, variables are something that you will always encounter. So it’s better to master them before proceeding. Variables are named memory locations whose values can be altered throughout our program life cycle. They are required to hold some intermediate processing data in main memory. These memory locations are overwritten each time the value of the variable is changed. Now if you focus on named memory location then it means a location in memory and the memory has some address associated with it. So to access the memory you need the address of that cell. The memory is divided into cells and each cell has some address associated with it. To reach that cell you need an address since you need the address of the place you want to visit. So it’s really a very difficult task to memorize a 32-bit or 64-bit length address. To overcome this problem we name the memory location and let the computer remember the address. These names are called variable names or identifiers. In Python we use variables by names and we assign the values to these names. From now on in this article I’m referring to variables as the same as Python names but keep in mind that they are not the same as other programming languages. Each name in Python is a reference to an actual object. Check out the following illustration of that concept.


To declare a variable in C++ or any static type language you need to explicitly mention the type of variable; that is, what type the variable will hold, like integer, float, double and so on.  Python is desgned to simplify the programmer’s life. It’s a dynamic and strongly typed language . The words look bigger but they are not. Dynamic means simply forget about data types when declaring a variable. Python will deduce the type automatically (it’s magic). Strongly typed means mixing of types is strictly prohibited. If you try to assign a list to an integer type then the Python interpreter will give you a bad smile that that is an error. So be careful when assigning names to each other. Now returning to declaring a variable in Python, look at the following pictures.


Now we know how to declare a variable in Python. Now let’s try to understand that statement. Whenever you write any variable in Python it is referred to as an object in memory and that actually stores your value. So it behaves as an integer type in the case of an integer value.

Now if you want to replicate the object then use the simple assignment like “myVar2 = myVar”. That statement will create one more name from which you can refer to the value of “myVar”. The important point here is that both myVar and myVar2 are different objects containing the same value.


Now let us have a look at one more advantage of Dynamic Type. Now try changing the myVar type from integer into string. To do this you just need to assign the myVar a string value. What this statement will do is, the name myVar that was used for an integer type object will now change to String type object. The older integer object is no longer accessible. It is masked by the new type. To simplify this concept just keep these lines in your mind.

  • Numeric objects are immutable. Immutable means that once created their value never changes.
  • Strings are immutable.

So in a nutshell we can say that the objects are created on the basis of the values not on the basis of names. If their is no name attached to a value then it will be collected by the garbage collector. If you are creating ten names having the same value then that means you are creating ten copies of the address of the same object.

Using the variable

To check the preceding concept just try to run the following code and see the output:

  1. myVar=5

  2. print(“\nID of myVAR having value 5 is “+str(id(myVar)))

  3. myVar2=myVar

  4. print(“\nID of myVAR2 having value of myVar is “+str(id(myVar2)))

  5. myVar3=myVar4=myVar5=5

  6. print(“\nID of myVAR3,4,5 having value of 5 is “+str(id(myVar3))+“,”+str(id(myVar4))+“,”+str(id(myVar5)))

  7. myVar = “hello”

  8. print(“\nID of myVAR after change to hello is “+str(id(myVar)))


The first line creates an object in memory with the value of 5 and myVar is the name associated with the value 5. Line 2 prints the address of the memory where 5 is stored. “id()” is used to get the address of object. If the id is the same then the names are associated with the same object otherwise they are bound to a different object. “str()” converts the integer into a string. The “print()” function prints the string.

Line 3 creates one more name or variable with the name myVar2 having a value equal to myVar. Since the value is the same, so the object will also be. That’s why we are getting the same id of myVar and myVar2.

Line 5 demonstrates the same thing. It simply says that you can add any number of names on an object. All names have the same “id” because they are referring to the same value.

Line 7 shows how we change the name from one object to another. myVar was associated with the object that had a value equal to five but now it is associated with “hello”. Since the hello value doesn’t exist in memory it is created and then myVar points to it. Now the id of myVar is different.



That’s all for this article. In this article I just covered the basics so that you do not encounter any difficulties in my next articles. I will be posting many other articles on Python and working with it. So stay tuned and don’t forget to comment. If you like the article then you can share it because sharing is caring but mentioning the original source of the article is a must.

Thanks for reading.

Filed under: CodeProject, Python


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Questionnext article Pin
deshmukhsc7-Oct-13 20:50
deshmukhsc7-Oct-13 20:50 
AnswerRe: next article Pin
Arpit Jain8-Oct-13 7:51
Arpit Jain8-Oct-13 7:51 
You can refer to this article now, Python Basics: Understanding The Flow Control Statements[^]
Arpit Jain

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.