Variables

Python Variables

Python variables are the reserved memory locations used to store values with in a Python Program. This means that when you create a variable you reserve some space in the memory.

Based on the data type of a variable, memory space is allocated to it. Therefore, by assigning different data types to Python variables, you can store integers, decimals or characters in these variables.

Memory Addresses

Data items belonging to different data types are stored in computer’s memory. Computer’s memory locations are having a number or address, internally represented in binary form. Data is also stored in binary form as the computer works on the principle of binary representation. In the following diagram, a string May and a number 18 is shown as stored in memory locations.

memory

If you know the assembly language, you will covert these data items and the memory address, and give a machine language instruction. However, it is not easy for everybody. Language translator such as Python interpreter performs this type of conversion. It stores the object in a randomly chosen memory location. Python’s built-in id() function returns the address where the object is stored.

>>>"May"
May
>>>id("May")2167264641264>>>1818>>>id(18)140714055169352

Once the data is stored in the memory, it should be accessed repeatedly for performing a certain process. Obviously, fetching the data from its ID is cumbersome. High level languages like Python make it possible to give a suitable alias or a label to refer to the memory location.

In the above example, let us label the location of May as month, and location in which 18 is stored as age. Python uses the assignment operator (=) to bind an object with the label.

>>> month="May">>> age=18

The data object (May) and its name (month) have the same id(). The id() of 18 and age are also same.

>>>id(month)2167264641264>>>id(age)140714055169352

The label is an identifier. It is usually called as a variable. A Python variable is a symbolic name that is a reference or pointer to an object.

Creating Python Variables

Python variables do not need explicit declaration to reserve memory space or you can say to create a variable. A Python variable is created automatically when you assign a value to it. The equal sign (=) is used to assign values to variables.

The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. For example −

Example to Create Python Variables

This example creates different types (an integer, a float, and a string) of variables.

counter =100# Creates an integer variable
miles   =1000.0# Creates a floating point variable
name    ="Zara Ali"# Creates a string variable

Printing Python Variables

Once we create a Python variable and assign a value to it, we can print it using print() function. Following is the extension of previous example and shows how to print different variables in Python:

Example to Print Python Variables

This example prints variables.

counter =100# Creates an integer variable
miles   =1000.0# Creates a floating point variable
name    ="Zara Ali"# Creates a string variableprint(counter)print(miles)print(name)

Here, 100, 1000.0 and “Zara Ali” are the values assigned to countermiles, and name variables, respectively. When running the above Python program, this produces the following result −

100
1000.0
Zara Ali

Deleting Python Variables

You can delete the reference to a number object by using the del statement. The syntax of the del statement is −

del var1[,var2[,var3[....,varN]]]]

You can delete a single object or multiple objects by using the del statement. For example −

del var
del var_a, var_b

Example

Following examples shows how we can delete a variable and if we try to use a deleted variable then Python interpreter will throw an error:

counter =100print(counter)del counter
print(counter)

This will produce the following result:

100
Traceback (most recent call last):
  File "main.py", line 7, in <module>
print (counter)
NameError: name 'counter' is not defined

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *