Python Variable

Variable is a name which is used to refer memory location. Variable also known as identifier and used to hold value.In Python, we don't need to specify the type of variable because Python is a type infer language and smart enough to get variable type. 

Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore.It is recomended to use lowercase letters for variable name. Rahul and rahul both are two different variables.

Variable Assignment

Think of a variable as a name attached to a particular object. In Python, variables need not be declared or defined in advance, as is the case in many other programming languages. To create a variable, you just assign it a value and then start using it. Assignment is done with a single equals sign (=):



>>>n=300

This is read or interpreted as “n is assigned the value 300.” Once this is done, n can be used in a statement or expression, and its value will be substituted:





>>> print(n)
300


Just as a literal value can be displayed directly from the interpreter prompt in a REPL session without the need for print(), so can a variable:


>>>n
300

Later, if you change the value of n and use it again, the new value will be substituted instead





>>> n = 1000
>>> print(n)
1000

>>> n
1000

Python also allows chained assignment, which makes it possible to assign the same value to several variables simultaneously:


>>> a = b = c = 300
>>> print(a, b, c)
300 300 300

The chained assignment above assigns 300 to the variables a, b, and c simultaneously.


advertisement


Variable Types in Python

In many programming languages, variables are statically typed. That means a variable is initially declared to have a specific data type, and any value assigned to it during its lifetime must always have that type

Variables in Python are not subject to this restriction. In Python, a variable may be assigned a value of one type and then later re-assigned a value of a different type:




>>> var = 23.5
>>> print(var)
23.5

>>> var = "Now I'm a string"
>>> print(var)
Now I'm a string


Object References

What is actually happening when you make a variable assignment? This is an important question in Python, because the answer differs some what from what you’d find in many other programming languages. 

Python is a highly object-oriented language. In fact, virtually every item of data in a Python program is an object of a specific type or class. (This point will be reiterated many times over the course of these tutorials.)

Consider this code:




>>> print(300)
    300


When presented with the statement print(300), the interpreter does the following:

  • Creates an integer object 
  • Gives it the value 300
  • Displays it to the console

You can see that an integer object is created using the built-in type() function:



>>>type(300)
< class 'int'>

A Python variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name. But the data itself is still contained within the object. 
For example:


>>>n = 300

This assignment creates an integer object with the value 300 and assigns the variable n to point to that object.

The following code verifies that n points to an integer object:


>>> print(n)
300
>>> type(n)
< class 'int'>

Now consider the following statement:



m=n

What happens when it is executed? Python does not create another object. It simply creates a new symbolic name or reference, m, which points to the same object that n points to.

Next, suppose you do this:


    >>>m=400

Now Python creates a new integer object with the value 400, and m becomes a reference to it.

Lastly, suppose this statement is executed next:



>>>n="foo"

Now Python creates a string object with the value "foo" and makes n reference that.

There is no longer any reference to the integer object 300. It is orphaned, and there is no way to access it. 

When the number of references to an object drops to zero, it is no longer accessible. At that point, its lifetime is over. Python will eventually notice that it is inaccessible and reclaim the allocated memory so it can be used for something else. In computer lingo, this process is referred to as garbage collection.

Advertisement

Variable Names

The examples you have seen so far have used short, terse variable names like m and n. But variable names can be more verbose. In fact, it is usually beneficial if they are because it makes the purpose of the variable more evident at first glance. 

Officially, variable names in Python can be any length and can consist of uppercase and lowercase letters (A-Z, a-z), digits (0-9), and the underscore character (_). An additional restriction is that, although a variable name can contain digits, the first character of a variable name cannot be a digit.
  • Note: One of the additions to Python 3 was full Unicode support, which allows for Unicode characters in a variable name as well. You will learn about Unicode in greater depth in a future tutorial.

For example, all of the following are valid variable names:



>>> name = "Bob"
>>> Age = 54
>>> has_W2 = True
>>> print(name, Age, has_W2)
Bob 54 True

But this one is not, because a variable name can’t begin with a digit:



>>> 1099_filed = False
SyntaxError: invalid token

Note that case is significant. Lowercase and uppercase letters are not the same. Use of the underscore character is significant as well. Each of the following defines a different variable:



>>> age = 1
>>> Age = 2
>>> aGe = 3
>>> AGE = 4
>>> a_g_e = 5
>>> _age = 6
>>> age_ = 7
>>> _AGE_ = 8
>>> print(age, Age, aGe, AGE, a_g_e, _age, age_, _AGE_)
1 2 3 4 5 6 7 8

There is nothing stopping you from creating two different variables in the same program called age and Age, or for that matter agE. But it is probably ill-advised. It would certainly be likely to confuse anyone trying to read your code, and even you yourself, after you’d been away from it awhile.

Advertisement