Variable in Python 3

What is a Variable in Python

Variable in Python is a quantity whose value can change during execution of a program. Python is an object oriented programming language. So everything in Python is represented as an object. In Python Variable is not a named memory location. It is a reference to value object.

Rules to name a variable in Python

  1. Variable name must start with an alphabet(a to z , A to B) or underscore ( _ )
  2. It can contain digits(0-9) , alphabets and underscore(  _ ) after first character
  3. Python is a case sensitive language so Capital and small letter are considered different from each other. For example: variable names FName and fname are different
  4. There is no limit of length but we should keep length of variable such that it is easily readable.
  5. We can’t use keywords as variable names.
  6. It can’t contain space.
  7. Name of variable can’t start with a digit.

Valid Variable Names

Invalid Variable Names

Fathername Father’sname [Special character ( ‘ ) not allowed
Itemnumber Item Number [Space not allowed]
Rs100 100Rs [Can’t start with a digit]
IF if (Keyword can’t be used as variable name]
$6 [Special character $ not allowed]

Creating a Variable in Python

It is very easy to create a variable in Python. We just need to specify name of variable and assign some value to it.

Example1:

var=10

Here var is the name of variable referencing to data value 10.

The data type of variable is automatically considered to be integer

Example 2:

b=5.5

Here b is the name of variable referencing to data value 5.5.

The data type of variable is automatically considered to be float.

Example 3:

sname=“Lovejot”

Here sname is the name of variable referencing to data value “Lovejot”.

The data type of variable is automatically considered to be string.

Example 4:

num=2+3j

Here num is the name of variable referencing to data value 2+3j .

The data type of variable is automatically considered to be complex.

Example 5:

result=True

Here result is the name of variable referencing to data value True .

The data type of variable is automatically considered to be bool.

Attributes of a Variable

All data and values are called objects in Python. Every Python object has three attributes

  •  Value  of object
  •  Type of object
  •  Id of object

a. Value of object

It is the data item contained in an object. For a literal, value is the literal itself. For a variable, value is the data item it is referencing.

Example 1:

>> var=10
>>> print(var)
10

print(var)  shows the value referenced  by variable named var i.e. 10.

Example 2:

>>> print (10)
10

print(10)  shows the value referenced  by literal 10 itself.

b. Type of object

The type of object specifies the operations that can be performed on an object. Predefined function type() returns the data type of an object.

Example 1:

>>> var=10
>>> type(var)
<class ‘int’>

type(var) returns the data type value referenced by a variable. In the above example, data type of value referenced by variable named var  is int.

Example 2:

>>> var=10.5
>>> type(var)
<class ‘float’>

float is the data type of value 10.5 referenced by variable var .

Example 3:

>>> var=”lovejot”
>>> type(var)
<class ‘str’>

str is the data type of value “lovejot” refernced by variable var. str stands for string

Example 4:

>>> var=3+4j
>>> type(var)
<class ‘complex’>

complex  is the data type of variable var referring to data value  3+4j which is a complex number.

Example 5:

>>> var=True
>>> type(var)
<class ‘bool’>

bool is the data type of variable var referring to boolean value True. bool stands for boolean.

c. Id of object

It is generally the memory location of an object We can use predefined function id() to display memory address of an object.

Example 1:

>>> var=10
>>> id(var)
19783268   

Memory address of data value 10 is returned as 19783268 which  is referenced by variable var.

>> id(10)
19783268  

Memory address of data value 10 is returned which is same as previous example i.e. 19783268
Note: Output in both the cases is same as memory address of data value is returned.

Assigning values to variables in Python

We can assign values to variables by using assignment operator (=). Values can be assigned to variables in multiple ways:

a. Assigning a value to a single variable:

We can assign a value to  a single by assigning the value to variable by using assignment operator = as

var=10

Above statement will assign the value 10 to the variable named var.

b. Assigning same value to multiple variables:

We can assign same value to multiple variables as follows:

a=b=c=10

Above statement will assign the value 10 to three variables a,b and c.

c. Assigning multiple values to multiple variables:

We can assign multiple values to to multiple variables in same line as follows:

a,b,c=10,15,20

Above statement will assign the value 10 to variable  a, 15 to variable b and  20 to variable c.

Values are assigned order wise. First variable is assigned first  value, second variable is assigned second value and third variable is assigned third value.

Swapping of variables

We can interchange values of variables in single line in a very simple way as follows:

Example:

 a,b=10,15

Above statement will assign the value 10 to variable  a and 15 to variable b.

a,b=b,a

Above statement will interchange values of a and b. i.e. value of variable a would become 15 and value of variable b would become 10.

Variable Definition

Variable in Python 3  is created only when we assign some value to it. A variable is not created if we don’t assign any value to it.

Example:

>> print (var)

Above statement will give error as given below because    #variable named var doesn’t exist.

Traceback (most recent call last):
File “<pyshell#0>”, line 1, in <module>
print (var)
NameError: name ‘var’ is not defined

Dynamic typing

In Python we can assign different types of values to same variable in different parts of same program.

Example 1:

>>> var=10  
>> print (var)
10

  #Above statements will show value refered by variable named   var i.e. 10

Example 2:

>>> var=“Lovejot”  
>>> print (var)
Lovejot

 #Above statements  will show value refered by  variable named  var i.e. Lovejot

While implementing Dynamic typing we must be careful about the operations we want to perform on variables.

>>> var=‘Lovejot’  
>> print (var/2)

Above statement will give error as variable named var contains a string value. Division on strings is not possible.

Keywords

Keywords are reserved words in Python. They are predefined in Python. We can’t change their meaning. We can’t use them for naming variables.

There are 33 keywords in Python as:

False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not  
class from or  
continue global pass  

Reserved words are case-sensitive and must be used exactly as shown. They are all entirely lowercase, except for False, None, and True.

Note: We can see list of keywords by typing help("keywords") command at Python shell.

>>> help (“keywords”)

Leave a Reply

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