Data types in Python

Preview
You must first complete Literals in Python before viewing this Lesson

Data types in Python

Data type specifies the type of data as well as the range of values that can be stored in the data object . Different Data types in Python 3 are as follows:

  • Numeric Data Types
  • Boolean
  • String
  • List
  • Tuple
  • Dictionary



Numeric Data types in Python

Numeric data types are categorized into three types

  • Integer Data type
  • Floating point Data Type
  • Complex Data Type

a. Integer Data Type

Integer data type is one of the most popular Data types in Python 3. It specifies that a variable or literal points to a numeric value without the decimal point. Integer data type accepts positive as well as negative values.

It is represented as keyword int in Python.

Rules for using integer data type are

  • Decimal point is not allowed.
  • Comma is not allowed.
  • There is not specified range of values in Python 3. It depends upon the largest memory available in computer.

Example:

Using Python IDLEUsing Spyder
>>> type(10)
<class ‘int’>
In [1]: type(10)
<class ‘int’>

Data value 10 is automatically considered int by Python.

Data type of literal 10 is returned as int when we use predefined function type() of Python to view data type of value 10.


b. Floating Point Data Type in Python 3

Floating point data type specifies that a variable or literal points to a numeric value with decimal point. Integer data type accepts positive as well as negative values.

It is represented as keyword float in Python.

Rules of float data type are:

  • Decimal point should be used.
  • Comma is not allowed.
  • They can also accept values in standard form as well as exponent form. The exponent can be an integer value as well as floating point value.
  • Precision is 15 digits.
  • float can accept values between 2.2250738585072014e-308 and 1.7976931348623157e+308. We can we minimum and maximum range of float by using following commands at Python shell as given below:

>>>import sys

>>>print(sys.float_info.max)
1.7976931348623157e+308

>>> print(sys.float_info.min)

2.2250738585072014e-308

Examples:

Using Python IDLEUsing Spyder
>>> type(10.5)
<class ‘float’>
In [2]: type(10.5)
<class ‘float’>
>>> type(2.5e4)
<class ‘float’>
In [3]: type(2.5e4)
<class ‘float’>

Data type of literals 10.5 and 2.5e4 is returned as float when we use predefined function type() of Python to view data type of values.


c. Complex Data Type in Python

Complex data type specifies that a variable or literal points to a complex value in Python. Complex value is of the form A+Bj.

Here A and B are floating point values. A is termed as real part and B is termed as imaginary part. j represents square root of -1. Which is an imaginary number.

It is represented as keyword complex in Python.

Rules of complex data type are:

  • Real part can contain integer as well as floating point value
  • Complex part can also contain integer as well as floating point value.
  • Both real and imaginary part can contain negative as well as positive values.

Examples:

Using Python IDLE

Using Spyder

>>> type(2+3j)
<class ‘complex’>
In [4]: type(2+3j)
<class ‘complex’>
>>> type(2.5+4.6j)
<class ‘complex’>
In [5]: type(2.5+4.6j)
<class ‘complex’>
>>> type(2.5e5+3.5e2j)
<class ‘complex’>
In [6]: type(2.5e5+3.5e2j)
<class ‘complex’>
>>> type(-2-3j)
<class ‘complex’>
In [7]: type(-2-3j)
<class ‘complex’>

Data type of literals 2+3j , 2.5+4.6j, 2.5e5 + 3.5e2j and -2-3j is returned as complex when we use predefined function type() to view data type of values.


d. Boolean Data Type in Python

Boolean data type is another one of the most usable Data types in Python 3. Boolean data type supports only two values True and False.

It is represented as keyword bool in Python.

Examples:

Using Python IDLEUsing Spyder
>>> type(True)
<class ‘bool’>
In [7]: type(True)
<class ‘bool’>
>>> type(False)
<class ‘bool’>
In [8]: type(False)
<class ‘bool’>

Data type of literals True and False is returned as bool when we use predefined function type() to view data type of values.


e. String Data Type in Python

In Python, string is the collection of multiple characters within a pair of single quotes(’) or double quotes(“).

It is represented as keyword str in Python.

Rules of string data type are:

  • String value can contain alphabets, digits, special characters, unicode characters as well as spaces. Unicode characters include symbols of all languages of the world.
  • They can also contain escape sequences.
  • They can contain values in unicode as well as raw format. Unicode values start with alphabet ‘u’ and raw values start with alphabet ‘r’ .

Example:

Using Python IDLEUsing Spyder
>>> print(type(‘lovejot’))
<class ‘str’>
In [9]: print(type(‘lovejot’))
<class ‘str’>
>>> print(type(“Bhardwaj”))
<class ‘str’>
In [10]: print(type(“Bhardwaj”))
<class ‘str’>
>>> print(type(u’python’))
<class ‘str’>
#String value is represented in unicode format by using prefix u with string value ‘python’
In [11]:print(type(u’python’))
<class ‘str’>
>>> print(type(r”string”))
<class ‘str’>
#String value is represented in raw format by using prefix r with string value ‘”string”.
In [12]: print(type(r”string”))
<class ‘str’>

Data type of literals ‘lovejot‘ ,”Bhardwaj“, u’python’ and r”string” is returned as str when we use predefined function type() to view data type of values.


f. List Data Type in Python

List data type is used to create a group of values separated by commas within the pair of square brackets. It is represented as keyword list in Python.

Rules of list data type are:

  • List can contain a set of values separated by commas within pair of square brackets.
  • These values can be of any data type i.e. it can contain a mixture of values having different data types. For example few values in a list can be of int type, few can be of float type and rest can be of string or boolean type.
  • Elements of a list can be modified.

Examples:

Using Python IDLEUsing Spyder

>>> L1=[1,2,3,4,5]

>>> print(type(L1))
<class ‘list’>

In [13]: L1=[1,2,3,4,5]

In [14]: print(type(L1))
<class ‘list’>

>>> L2=[1,2.5,3,’Lovejot’]

>>> print(type(L2))
<class ‘list’>

In [15]: L2=[1,2.5,3,’Lovejot’]

In [16]: print(type(L2))
<class ‘list’>

Data type of lists L1 and L2 is returned as list when we use predefined function type() to view data type of lists.


g. Tuple Data Type in Python 3

Tuple data type is used to create a group of values separated by commas within the pair of parenthesis. It is represented as keyword tuple in Python 3.

Rules of tuple data type are:

  • Tuple can contain a set of values separated by commas within pair of parenthesis.
  • Values contained in a tuple can be of any data type i.e. it can contain a mixture of values having different data types. For example few values in a tuple can be of int type, few can be of float type and rest can be of string or boolean type.
  • Elements of a list can’t be modified.

Examples:

Using Python IDLEUsing Spyder

>>> T1=(1,2,3,4,5)

>>> print(type(T1))
<class ‘tuple’>

In [17]: T1=(1,2,3,4,5)

In [18]: print(type(T1))
<class ‘tuple’>

>>> T2=(‘A’,1,True,’Lovejot’)

>>> print(type(T2))
<class ‘tuple’>

In [19]: T2=(‘A’,1,True,’Lovejot’)

In [20]: print(type(T2))
<class ‘tuple’>

Data type of tuples T1 and T2 is returned as tuple when we use predefined function type() to view data type.

h. Dictionary Data type in Python 3

Dictionary data type is used to create group of values in the form of key:value pairs separated by commas within the pair of braces (Curly brackets).

It is represented as keyword dict in Python 3.

Rules of dictionary data type are:

  • Dictionary can contain a set of values in the form of key value pairs.as key:value. Example {1:’Aman’} . here 1 is the key and ‘Aman’ is the value.
  • All values must be enclosed within set of braces(curly brackets).
  • Keys as well as values contained in a dictionary can be of any data type .
  • Keys as well as values.in a dictionary can be modified.

Examples:

Using Python IDLEUsing Spyder
>>> D1={1:’a’, 2:’e’, 3:’i’, 4:’o’, 5:’u’}
>>> print(type(D1))
<class ‘dict’>
In [21]: D1={1:’a’, 2:’e’, 3:’i’, 4:’o’, 5:’u’}
In [22]: print(type(D1))
<class ‘dict’>
>>> D2={‘a’:1, ‘e’:2, ‘i’:3, ‘o’:4, ‘u’:5}
>>> print(type(D2))
<class ‘dict’>
In [23]: D2={‘a’:1, ‘e’:2, ‘i’:3, ‘o’:4, ‘u’:5}
In [24]: print(type(D2))
<class ‘dict’>

Data type of dictionaries D1 and D2 is returned as dict when we use predefined function type() to view data type.

Lesson tags: boolean data type in python, complex data type in python, data types in python, dictionary in python, integer data type in python, list in python, python data types, range of data types in python, real data type in python, tuple in python
Back to: Python Programming Tutorial