data types and variable in python?


What is variable

variable is a identity of any memory location where data is stored , in program we recognize memory location by using variable names.

variable name is a user defined keyword (identifier) used for naming datatypes.

Variable is a name which is used to refer memory location. Variable also known as identifier and used to hold value.

But in Python

python is vary easy and smart language , there is no need to tell type of data to python program.

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.

n of the tutorial, we will give a brief introduction of the above data types. We will discuss each one of them in detail later in this tutorial.

Declaring Variable and Assigning Values

Python does not bound us to declare variable before using in the application. It allows us to create variable at required time.

We don t need to declare explicitly variable in Python. When we assign any value to the variable that variable is declared automatically.

The equal (=) operator is used to assign value to a variable.




print age

print name




Multiple Assignment

Python allows us to assign a value to multiple variables in a single statement which is also known as multiple assignment.

We can apply multiple assignments in two ways either by assigning a single value to multiple variables or assigning multiple values to multiple variables. Lets see given examples.

1. Assigning single value to multiple variables



print x

print y  

print z  






Identifier Naming

Variables are the example of identifiers. An Identifier is used to identify the literals used in the program. The rules to name an identifier are given below.

  • The first character of the variable must be an alphabet or underscore ( _ ).
  • All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore or digit (0-9).
  • Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
  • Identifier name must not be similar to any keyword defined in the language.
  • Identifier names are case sensitive for example my name, and MyName is not the same.
  • Examples of valid identifiers : a123, _n, n_9, etc.
  • Examples of invalid identifiers: 1a, n%4, n 9, etc.

What is Datatype 

In computer science and computer programming, a data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data.  This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored.
data type is a type of data. Some common data types include integers, floating point numbers, characters, strings, and arrays. They may also be more specific types, such as dates, timestamps, boolean values, and varchar (variable character) formats.

Image result for datatype in python



Data types in python :-

  • In python , there is no need to specify datatype when we are creating variable , Variables can hold values of different data types.
  • Python is a dynamically typed language hence we need not define the type of the variable while declaring it. The interpreter implicitly binds the value with its type.
  • using type() function we can easily get the type of variable base on stored values.



b="Hi Python by rlchats"  

c = 100.5 





Standard data types

A variable can hold different types of values. For example, a person s name must be stored as a string whereas its id must be stored as an integer.

Python provides various standard data types that define the storage method on each of them. The data types defined in Python are given below.

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary


Number stores numeric values. Python creates Number objects when a number is assigned to a variable. For example;

  1. a = 3 , b = 5  #a and b are number objects  

Python supports 4 types of numeric data.

  1. int (signed integers like 10, 2, 29, etc.)
  2. long (long integers used for a higher range of values like 908090800L, -0x1929292L, etc.)
  3. float (float is used to store floating point numbers like 1.9, 9.902, 15.2, etc.)
  4. complex (complex numbers like 2.14j, 2.0 + 2.3j, etc.)

Python allows us to use a lower-case L to be used with long integers. However, we must always use an upper-case L to avoid confusion.

A complex number contains an ordered pair, i.e., x + iy where x and y denote the real and imaginary parts respectively).


The string can be defined as the sequence of characters represented in the quotation marks. In python, we can use single, double, or triple quotes to define a string.

String handling in python is a straightforward task since there are various inbuilt functions and operators provided.

In the case of string handling, the operator + is used to concatenate two strings as the operation "hello"+" python" returns "hello python".

The operator * is known as repetition operator as the operation "Python " *2 returns "Python Python ".

The following example illustrates the string handling in python.

  1. str1 =  hello javatpoint  #string str1  
  2. str2 =   how are you  #string str2  
  3. print (str1[0:2]) #printing first two character using slice operator  
  4. print (str1[4]) #printing 4th character of the string  
  5. print (str1*2#printing the string twice  
  6. print (str1 + str2) #printing the concatenation of str1 and str2  


hello javatpointhello javatpoint
hello javatpoint how are you


Lists are similar to arrays in C. However; the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].


We can use slice [:] operators to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings.

Consider the following example.

  1. l  = [1"hi""python"2]  
  2. print (l[3:]);  
  3. print (l[0:2]);  
  4. print (l);  
  5. print (l + l);  
  6. print (l * 3);   


[1,  hi ]
[1,  hi ,  python , 2]
[1,  hi ,  python , 2, 1,  hi ,  python , 2]
[1,  hi ,  python , 2, 1,  hi ,  python , 2, 1,  hi ,  python , 2]


A tuple is similar to the list in many ways. Like lists, tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses ().

A tuple is a read-only data structure as we can t modify the size and value of the items of a tuple.

Let s see a simple example of the tuple.

  1. t  = ("hi""python"2)  
  2. print (t[1:]);  
  3. print (t[0:1]);  
  4. print (t);  
  5. print (t + t);  
  6. print (t * 3);   
  7. print (type(t))  
  8. t[2] = "hi";  


( python , 2)
( hi ,)
( hi ,  python , 2)
( hi ,  python , 2,  hi ,  python , 2)
( hi ,  python , 2,  hi ,  python , 2,  hi ,  python , 2)

Traceback (most recent call last):
  File "main.py", line 8, in 
    t[2] = "hi";
TypeError:  tuple  object does not support item assignment


Dictionary is an ordered set of a key-value pair of items. It is like an associative array or a hash table where each key stores a specific value. Key can hold any primitive data type whereas value is an arbitrary Python object.

The items in the dictionary are separated with the comma and enclosed in the curly braces {}.

Consider the following example.

  1. d = {1: Jimmy 2: Alex 3: john 4: mike };   
  2. print("1st name is "+d[1]);  
  3. print("2nd name is "+ d[4]);  
  4. print (d);  
  5. print (d.keys());  
  6. print (d.values());  

It is recomended to use lowercase letters for variable name. Rlchats and rlchatsboth are two different variables.

Related Topics