Python – Data Types

Python Data Types defines the different types of a variable. Since everything is an object in Python, data types are literally classes and also the variables are instances of the classes.

In any programming language, different operations are often performed over differing types of information which are common with other datatype while some are often very specific thereto particular datatype.

1. Followings are the list of built in datatype in python:

  • Text/String (str)
  • Numeric (int, float, complex)
  • List (list, tuple, range)
  • Map (dict)
  • Set (set, frozenset)
  • Boolean (bool)
  • Binary (bytes, bytearray, memoryview)

2. Let’s understand above python datatypes in detail:

2.1 str datatypes in python

The string are often defined because the sequence of characters enclosed in single, double, or triple quotes.


a = 'A'
b = "B"
c = """
print(a) # prints A
print(b) # prints B
print(c) # prints C

print(a + b) # This expression will concatenate and prints AB

print(a*2) # This expression is also called as repetition operator in Python and print 2A

name = str('onlineTutorials') # This expression is also called Constructor

sumOfItems = str(3000) # This expression is used for type conversion from int to string

2.2 int, float, complex datatypes in python

These are number datatypes. They are created when a number is assigned to a variable in python.

  • int holds signed integers of non-limited length.
  • float holds floating precision numbers, and they are accurate up to 15 decimal places.
  • complex – A complex number contains the real and imaginary part.


a = 3 # int data Types
a = int(3) # int data Types

a = 10.5 # float data Types
a = float(10.5) # float data Types

a = 99+5i # complex data Types
a = complex(99+5i) # complex data Types

2.3 list, tuple, range datatypes in python

List is considered as ordered sequence of data written using some brackets ([ ]) and commas(,).
The concatenation operator i.e. (+) and repetition operator i.e. astrik (*) will work similar as works similar the str data type.

A list can have data of multiple types.

  • Slice [:]
  • range
  • tuple

Slice [:] this operator is being used to access the data in python the list.

A range is being considered as sublist, this is being used to take data out of a list using this operator.

A tuple is very similar to the list – except tuple is a non-editable (i.e. read only) data structure, and we can’t restructure the size and value of the items of a tuple. Also, these items are enveloped in parentheses (, ).


anyMixList = [1, "one", 2, "two"]
print (anyMixList); # prints [1, 'one', 2, 'two']
print (anyMixList + anyMixList); # prints [1, 'one', 2, 'two', 1, 'one', 2, 'two']
print (anyMixList * 2); # prints [1, 'one', 2, 'two', 1, 'one', 2, 'two']

alphList = ["a", "b", "c", "d", "e", "f", "g", "h"]

print (alphList[3:]); # Here range will prints ['d', 'e', 'f', 'g', 'h'] it will start from index 3 to last index
print (alphList[0:2]); # Here range will prints ['a', 'b'] it will start from 0 to range index -1.

someListTuple = (1, "one", 2, "two")
print (someListTuple[0:2]); # range - prints (1, 'one')

someListTuple[0] = 0 # TypeError: 'tuple' object does not support item assignment

2.4. dict datatypes in python

A python dict or dictionary is an ordered set of a key-value pair of items in dictionary.
A key in dict or dictonary can hold any primitive data type whereas value is an arbitrary Python object.

The entries within the dict or dictionary are separated with the comma and enclosed within the curly braces i.e{, }.


charactersMap = {1:'one', 2:'two', 3:'three', 4:'four'};

print (charactersMap); # prints {1:'one', 2:'two', 3:'three', 4:'four'}
print ("1st entry is " + charactersMap[1]); # prints 1st entry is one
print (charactersMap.keys()); # prints dict_keys([1, 2, 3, 4])
print (charactersMap.values()); # prints dict_values(['one', 'two', 'three', 'four'])

2.5 set, frozenset datatypes in python

The set in python are often defined because the unordered collection of varied items enclosed within the curly braces.

The weather of the set cannot be duplicate.
The weather of the python set must be immutable.
Unlike list, there’s no index for set elements. It means we are able to only loop through the weather of the set.

The frozen sets are the immutable in nature. So it means neither we can remove or add any item into this.


numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

print(numbers) #This will prints {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

print(type(numbers)) #This will prints <class 'set'>

print("We are Looping from the set of elements ... ")
for i in numbers:
print(i) # prints 0 1 2 3 4 5 6 7 8 9 in new lines

numbers.remove(0) #This functionality allowed in normal set

print(numbers) # {1, 2, 3, 4, 5, 6, 7, 8, 9}

frozenSetOfNumbers = frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})

frozenSetOfNumbers.remove(0) # AttributeError: 'frozenset' object has no attribute 'remove'

2.6. bool datatypes in python

bool values are the 2 constant objects False and True. They will use to represent truth values in Python. In numeric contexts, they will behave just like the integers 0 and 1, respectively.


a = True
b = False

print(a) #True
print(b) #False

print(bool(0)) #False
print(bool(1)) #True

2.7. bytes, bytearray, memoryview datatypes in python

bytes and bytearray are used for manipulating binary data.

The memoryview uses the buffer protocol to access the memory of other binary objects with no need to create a replica.

Bytes objects are immutable sequences of single bytes. we must always use them only working with ASCII compatible data.

The syntax for bytes is very similar to string literals, except that b prefix is added.

bytearray objects are always created by calling the constructor bytearray() and these are immutable objects.


a = b'char_data' # here we have added b as prefix to char data
a = b"char_data" # here we have added b as prefix to char data

d = bytearray(5)

e = memoryview(bytes(5))

print(a) # b'char_data'
print(d) # bytearray(b'\x00\x00\x00\x00\x00')
print(e) # <memory at 0x014CE328>

3. type() function in python

The type() function are often used to the info sort of any object.

a = 5
print(type(a)) # <class 'int'>

b = ''
print(type(y)) # <class 'str'>

Learn More:

%d bloggers like this: