Creating Code For Simplicity but Logic For Need
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 two of them.
Python in Visual Studio (recommended)
To install Python in Visual Studio use the following procedure:
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.
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:
print(“\nID of myVAR having value 5 is “+str(id(myVar)))
print(“\nID of myVAR2 having value of myVar is “+str(id(myVar2)))
print(“\nID of myVAR3,4,5 having value of 5 is “+str(id(myVar3))+“,”+str(id(myVar4))+“,”+str(id(myVar5)))
myVar = “hello”
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.