In the previous tutorial we have learned writing “Hello World!” program in python and using print command and operators, Now we are going to learn what is variable and different variable types in python.

First of all,

What is variable ?

In computer programming, a variable is a storage location or memory location and an associated name or an identifier which contains some known or unknown quantity or information, a value. In short variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, characters or more complex data types in these variables.

In programming languages every variable must have a unique data type. Ex. if a variable is of type integer, solely integers can be saved in the variable. In Java or C, every variable has to be declared before it can be used. Declaring a variable means binding it to a data type, but declaration of variables is not required in Python. If there is need of a variable, you think of a name and start using it as a variable, so python variables do not have to be explicitly declared. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables. For example:

num = 34  # An integer assigned to variable "num"

length = 56.3 # An floating point integer assigned to variable "length"

name = "john" # An string assigned to variable "name"

Python Keywords:

As we have seen above, we are assigning 34, 56.3, and john to variable name num, length and name, but there are few keywords in python which we cannot use as variable name. when we use this keywords as variable name python raise syntax error. There are thirty one keywords which are listed below.

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

 

Following are the example of syntax error when we use keywords as variable name.

>>> as = 56.3
  File "<stdin>", line 1
    as = 56.3
     ^
SyntaxError: invalid syntax
try = "john"
  File "<stdin>", line 1
    try = "john"
        ^
SyntaxError: invalid syntax
>>> raise = 34
  File "<stdin>", line 1
    raise = 34
          ^
SyntaxError: invalid syntax

Changing Data type and Memory Location:

In python the type of a variable can change during the execution of code. Look at the following example to  illustrate this concept.

a = 75              # data type is set to integer
a = 75 + 0.55       # data type is changed to float
a = "Eighty"        # and now data type changed to string

Python automatically takes care of the physical representation for the different data types, i.e. an integer values will be stored in a different memory location than a float or a string.

Python Choose memory location variable “a” and saves value 75 i.e integer. in second assignment python will find another assignment ans saves value “75 + 0.55” i.e float. On third assignment python will again find another location for “a” ans saves value “Eighty” i.e string. To prove this we will use identity function id() i.e every instance and variable has it’s own unique identity, as shown in statement below:

>>> a = 75      
>>> id(a)
144355176
>>> a = 75.55
>>> id(a)
144393916
>>> a = "Eighty"
>>> id(a)
3075241888L

Python variable data types:

Python has five data types,

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Number data types stores numeric or integers value, as shown in example below:

>>> x = 5
>>> y = 55
>>> z = 555

Strings in Python are identified as a set of characters in between quotation marks. Python allows for either pairs of single ( ” ) or double quotes ( “” ), as shown in example below:

>>> my_name = "John"
>>> my_job = "Software Developer"
>>> my_address = '3909 Witmer Road, Niagara Falls, NY 14305'

Lists are the most versatile data type of python. A list contains items separated by commas and enclosed within square brackets ( [] ). Following are the examples of list:

>>> name_list = ["John", "Jerry", "Jimmy", "Jackie"]
>>> name_list
['Jhon', 'Jerry', 'Jimmy', 'Jackie']
>>> color_list = ["Red", "Yellow", "Blue", "Black"]
>>> color_list
['Red', 'Yellow', 'Blue', 'Black']

 

Python tuples are another data type which is similar to list, items are separated by commas and enclosed with round brackets ( () ). Following are the examples of tuples:

 

>>> name_tuple = ("John", "55", "Jerry", "5", "Jimmy", "545")
>>> name_tuple
('John', '55', 'Jerry', '5', 'Jimmy', '545')
>>> some_tuple = ("xyz", "3jf6", "4fffff", "Black", "doom", "fire", "0909")
>>> some_tuple
('xyz', '3jf6', '4fffff', 'Black', 'doom', 'fire', '0909')

Python’s dictionaries are kind of hash table type. It consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object. Dictionaries are enclosed by curly braces ( { } ), For example:

>>> name_dict = {"John": 55, "Jerry": 545, "Jimmy": 999}
>>> name_dict
{'John': 55, 'Jerry': 545, 'Jimmy': 999}
>>> some_dict = {"name_list": ["Jhon", "Jerry", "Jimmy", "Jackie"], "name_tuple": ("John", "55", "Jerry", "5", "Jimmy", "545")}
>>> some_dict
{'name_tuple': ('John', '55', 'Jerry', '5', 'Jimmy', '545'), 'name_list': ['Jhon', 'Jerry', 'Jimmy', 'Jackie']}

This is all about python variable and data types in next tutorial we’ll this data types in detail.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.