Data Types in Python

Data Type: It specifies the type of data and associated operations of handling it. Different set of operations are applicable on different types of data. Example: We can add, subtract, multiply, divide two numbers. But we cant subtract, multiply, divide two strings ie textual data.

Data Types in Python: Following data types are supported in python.
1. Numeric (Integer, Float, Complex)
2. Boolean
3. Sequences/ Collections (List, Tuple, Set, Strings)
4. Mapping ( Dictionary)
5. Special Data Type None
We can check the type of data by using the built in function type() of python as:
type (argument)

1. Numeric Data Type: These are values in the form of numbers. Python supports Integer, Float and Complex data type.

a) Integer Data Type It is any combination of digits along with +/- sign. It includes both positive and negative numbers along with 0. It doesn’t include fractional parts.

>>> a=4398
>>> a
4398
>>> type(a)
<class 'int'>

>>> b=-34534
>>> b
-34534
>>> type(b)
<class 'int'>

>>> c=0
>>> c
0
>>> type(c)
<class 'int'>

b) Float Data Type: It includes both positive and negative real numbers. It also includes fractional parts. There are two forms to represent float literals: I) standard form and II) mantissa exponent form.

I) Standard Form: Any number with or without +/- sign having decimal point.

>>> a=54.645
>>> a
54.645
>>> type(a)
<class 'float'>

>>> b=-324.654
>>> b
-324.654
>>> type(b)
<class 'float'>

>>> c=9.
>>> c
9.0
>>> type(c)
<class 'float'>

>>> d=.7
>>> d
0.7
>>> type(d)
<class 'float'>

II) Mantissa Exponent Form: mantissa may have any number with or without +/- sign having decimal point, and exponent must be a positive or negative integer followed by e/E sign. Internally python stores the number in normalized form and using e sign for exponent.

>>> a=44.3432e34
>>> a
4.43432e+35
>>> type(a)
<class 'float'>

>>> b=0.00324E-45
>>> b
3.24e-48
>>> type(b)
<class 'float'>

>>> a=0.00324E-4.5
SyntaxError: invalid syntax

There must not be any space between mantissa, e and exponent part.

>>> a=44.3432 e 34
SyntaxError: invalid syntax

>>> a=44.3432 e34
SyntaxError: invalid syntax

c) Complex Data Type: It is of the form a + bj , here a represents the real part and b represents the complex part. Both j and J are allowed but internally Python stores j.

>>> a=8+4j
>>> a
(8+4j)
>>> type(a)
<class 'complex'>

>>> b=8+4J
>>> b
(8+4j)
>>> type(b)
<class 'complex'>

There must not be any space between b and j

>>> a=9+5 j
SyntaxError: invalid syntax

We can retrieve the real and imaginary parts of a complex number by using .real and .imag with the complex variable as

>>> a=9+5j

>>> a
(9+5j)

>>> a.real
9.0

>>> a.imag
5.0

2. Boolean Data Types: There are two Literals True and False which are known as Boolean Values.

>>> a=True
>>> a
True
>>> type(a)
<class 'bool'>

>>> b=False
>>> b
False
>>> type(b)
<class 'bool'>

3. Collections/Sequences: Python provides 4 different types of collections or sequences:

  • List : Comma separated list of items (may be of same / different type) enclosed in square brackets [ and ]
    Example: [“gargs” ,”academy”, 9467863365]
  • Tuple : Comma separated list of items (may be of same / different type) enclosed in paranthesis ( and )
    Example: (“gargs” ,”academy”, 9467863365)
  • Set : Comma separated list of unordered and unique items (may be of same / different type) enclosed in braces { and }
    Example: {“gargs” ,”academy”, 9467863365}
  • Strings:  Any combination of characters or text written in single, double, or triple quotes. Internally strings are stored in single quotes.
>>> list1=["gargs" ,"academy", 9467863365]
>>> list1
['gargs', 'academy', 9467863365]
>>> type(list1)
<class 'list'>

>>> tuple1=("gargs" ,"academy", 9467863365)
>>> tuple1
('gargs', 'academy', 9467863365)
>>> type(tuple1)
<class 'tuple'>

>>> set1={"gargs" ,"academy", 9467863365}
>>> set1
{'academy', 9467863365, 'gargs'}
>>> type(set1)
<class 'set'>

>>> str1='gargs academy'
>>> str1
'gargs academy'
>>> type(str1)
<class 'str'>

>>> str2="gargs academy"
>>> str2
'gargs academy'
>>> type(str2)
<class 'str'>

4. Mapping/ Dictionary: It is a comma separated list of key : value paired form of unordered items (may be of same / different type) enclosed in braces { and }. Example: {“gargs” : 1 , 2 : “academy”, “phone” : 9467863365}

>>> dict1= {"gargs" : 1 , 2 : "academy", "phone" : 9467863365}
>>> dict1
{'gargs': 1, 2: 'academy', 'phone': 9467863365}
>>> type(dict1)
<class 'dict'>

5. Special Data Type None: None is used to define a null or missing value.

>>> a=None

>>> a

>>> print(a)
None

>>> type(a)
<class 'NoneType'>
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.