Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Python Basics: Understanding the Variables

0.00/5 (No votes)
7 Oct 2013 1  
An introduction to some Python data types and data structures.

Introduction

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

    663668/a12p1.jpg
  2. Choose your version and click on the download button.

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

    663668/a12p9.jpg
  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”. 

    663668/a12p4.jpg
     
  7. Write your code and press F5 to debug it.

    663668/a12p6.jpg

Python Shell (Required) 

  1. Download the shell from here.

    663668/a12p7.jpg
  2. Start the installer and install it as same as other software.

    663668/a12p3.jpg
     
  3. To run the shell just launch IDLE (Python GUI).

    663668/a12p8.jpg
  4. Just type some code and you are done.

    663668/a12p10.jpg
  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.

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

    663668/a12p12.jpg 
     

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.

663668/a12p14.jpg

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.

663668/a12p15.jpg

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.

663668/a12p16.jpg

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)))

663668/a12p13.jpg

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.

663668/a12p18.jpg

Summary

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here