Python Data Types - RecentBlogger


fig: 1

Data types are an essential concept in the python programming language.
Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data.

The classification of data items or to put the data value into some sort of data category is called Data Types. It helps us to understand what kind of operations to be performed. In Python, everything is an object. And the data types represent the classes.  The objects or instances of these classes are called variables. Now, let us check out different kinds of data types in Python.

Python has the following data types:

Text Type: string
Binary Types: memory view, byte array, bytes
Boolean Type: boolean
Numeric Types: complex, float, integer
Mapping Type: dictionary
Set Types: frozenset, set
Sequence Types: range, tuple, list

1. Numeric

In Python, the numeric values can be integer, floating-point, or even complex numbers. All these can be denoted as int, float, and complex class respectively in Python. 

  • Integer 
            This value is represented by int class. It can have positive or negative whole numbers. There is no maximum limit on the value of an integer. 

           Eg: a = 6
                >>>  <class 'int'>

           Eg: a , b = 3, 6 
                c = a+b
                >>> <class 'int'>
                >>> <class 'int'>

  • Float
            Represented by float class. A real number with floating-point representation. Specified by a decimal point. It is accurate up to 15 decimal places.
            Eg: a = 6.00
                   >>>  <class 'float'>
  • Complex Numbers
            Represented by complex classes, specified as (real part) + (imaginary part) j i.e.., a+bj

            Eg: c = 3+6j
                   >>>  <class 'complex'>

2. Sequence Type

Sequence is the ordered collection of similar or different data types. It allows to store multiple values in an organised and efficient way. 

  • String
            Strings are arrays of bytes representing Unicode characters. A string is a collection of one or more characters put together in a single, or double quote. It is represented by an str class.

            Eg: string_one = "Welcome to the blog post"
                   >>> <class 'str'>

  • Tuple
            A tuple is an ordered collection of Python objects. Tuples are immutable i.e. they cannot be altered or modified after it is created. Represented by tuple class.
Tuples are created by placing a sequence of values separated by a 'comma'. It can contain any number of elements and of any data type.

            Eg: tuple_one = ('Python', 'Programming')
                   print(f' "This is a tuple with string as a data type: ")
                   >>> This is a tuple with string as a data type:
                   >>> ('Python', 'Programming')

  • List
            Lists are just like arrays, which are ordered collections of data. It is flexible as the items in the list do not need to be the same type. Lists in Python can be created using [ ], square brackets.

            Eg: list_one = ["Code", "in", "Python"]
                   print("List containing multiple values: ")
                   >>> Python

3. Dictionary

Dictionary is an unordered collection of data values. It holds {"key":"value"}, with { } curly braces with separated by 'comma'. Which makes the dictionary more optimized. Values in the dictionary can be of any data type or duplicate values, whereas keys cannot be repeated. Dictionary can also be created by the inbuilt function dict().

            Eg: dict_one = {1:"Code",  2:"in", 3:"Python"}
                   print("Dictionary with keys as integer data type")
                   >>> Dictionary with keys as integer data type
                   >>> {1:"Code",  2:"in", 3:"Python"}

4. Set

Set is an unordered collection of data types that are iterable, mutable, and have no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. Set can be created by using the built-in set() function. Operations like intersection and union can be performed on two sets. 

            Eg: set_one = set("Programming")
                   >>> {'P','o','a','i','n'}
            Eg: a = {2,2,3,6,7,7,8,8,9}
                   >>> {3,4,9}

The slicing operation does not work on set, because it is not a collection of ordered items.

5. Boolean

Data type with one of the two built-in values, either TRUE or FALSE. It can either be TRUE or FASLE. It is denoted by class bool.

            Eg: bool_one = True
                    >>>  <class 'bool'>


If you are here, then you are probably learning Python. Hope you found this article helpful. Also, kindly check out my other articles on Python and Python-related. 


Post a Comment