In programming, we need to work with data or the collections of data. Similarly, in Python, we have variables and objects that store the data or a collection of data. Here, we determine the type of data with Python data types. Data types are the classification or categorization of data items that represent the kind of value we store as data.
Since Python is an Object-Oriented Programming (OOP) language, everything is considered as an object in Python. Therefore, Python data types are classes and variables are instances of these classes.
What are basic Data types in Python?
Python data types are broadly classified into five categories
- Numeric
- Boolean
- Dictionary
- Set
- Sequence
Before we discuss each data type, we shall first discuss how to determine the data type of an object.
How to determine the data type of an object?
We have a Python built-in function type()
to determine the data type of an object. This function takes the object as its argument and returns the data type of the object. Here, we will notice that it returns the data type as a class. As we have discussed earlier this is because Python data types are classes and an object is the instance of that class. For example,
mynum = 34
fnum = 16.35
mystr = "python"
print("Type of mynum:", type(mynum))
print("Type of fnum:", type(fnum))
print("Type of mystr:", type(mystr))
Output:
Type of mynum: <class 'int'>
Type of fnum: <class 'float'>
Type of mystr: <class 'str'>
Numeric data type
Numeric data type represents the data that has a numerical value. A numeric value can be an integer, floating-point number, or complex number. Let’s discuss each one of them.
Integer
An integer value is defined by the int class of Python. All the integers (positive or negative) come under this category and there is no definite range for integer values in Python. By default, Python interprets a sequence of digits without any decimal as an Integer value of base 10. However, we can prepend some prefixes to an integer value (of respective base) to define a number with a base other than 10. For example,
# normal integer value
a = 25
print(a)
print(type(a))
# integer in binary form (base 2)
bin_num = 0b101
print(bin_num)
print(type(bin_num))
# integer in octal form (base 8)
oct_num = 0o101
print(oct_num)
print(type(oct_num))
# integer in hexadecimal form (base 16)
hex_num = 0x101
print(hex_num)
print(type(hex_num))
Output:
25
<class 'int'>
5
<class 'int'>
65
<class 'int'>
257
<class 'int'>
Floating point number
The floating-point value is defined by the float class of Python. Any real number with floating-point representation comes under this category. These values are specified using a decimal point. Optionally, we can append character e or E followed by a positive or negative integer to specify the value in Scientific notation. For example,
a = 79.63
print(a)
print(type(a))
b = -0.032
print(b)
print(type(b))
# specifying value in scientific notation
c = 1.7e4
print(c)
print(type(c))
Output:
79.63
<class 'float'>
-0.032
<class 'float'>
17000.0
<class 'float'>
Complex number
A complex number is defined by the complex class of Python. All complex numbers come under this category. Complex numbers are defined as <real part> + <imaginary part>j. For example,
z1 = 6 + 3j
print(z1)
print(type(z1))
z2 = -3 - 8j
print(z2)
print(type(z2))
Output:
(6+3j)
<class 'complex'>
(-3-8j)
<class 'complex'>
Boolean
Boolean type is defined by the class bool of Python. There are two Boolean values True and False that are of Boolean type. Python expressions that include conditional statements return these values. In simpler words, we use Boolean values to represent the truth value of an expression. For example,
t = True
print("t:", t)
print(type(t))
f = False
print("f:", f)
print(type(f))
# expression 1
cond1 = (5 + 7 == 4)
print("Truth value of expression 1:", cond1)
print(type(cond1))
# expression 2
cond2 = (8 > 2)
print("Truth value of expression 2:", cond2)
print(type(cond2))
Output:
t: True
<class 'bool'>
f: False
<class 'bool'>
Truth value of expression 1: False
<class 'bool'>
Truth value of expression 2: True
<class 'bool'>
Sequence
A sequence is the ordered collection of Python objects. It allows us to store multiple values under a single Python variable. We have three sequences in Python which are string, list, and tuple. Let’s see each one of them.
String
A string is defined by class str of Python. A Python string is nothing but a sequence of characters. It’s an array of bytes representing Unicode characters. We define a string inside the quotation marks. Also, in Python there is no character data type, a character is just a string of length one. For example,
mystr1 = 'hello world'
print("String with single quote:", mystr1)
print(type(mystr1))
mystr2 = "hello jupiter"
print("\nString with double quotes:", mystr2)
print(type(mystr2))
# multiline string
mystr3 = '''Python is
Awesome'''
print("\nMultiline string with triple quotes:", mystr3)
print(type(mystr3))
# single character
char = 'a'
print("\nA single character:", char)
print(type(char))
Output:
String with single quote: hello world
<class 'str'>
String with double quotes: hello jupiter
<class 'str'>
Multiline string with triple quotes: Python is
Awesome
<class 'str'>
A single character: a
<class 'str'>
List
A list is defined by the class list of Python. A Python list is an ordered collection of data that stores the sequence of Python objects. We create a list by placing its elements inside square brackets. It may store multiple objects where each object can be of any Python data type. For example,
# empty list
empty = []
print("Empty list:", empty)
print(type(empty))
#list having elements of different data types
mylst = [1, 33.33, 'python', False, [7, 8, 9]]
print("\nList with different data types:", mylst)
print(type(mylst))
#multi-dimensional list or nested list
nested = [[1, 2, 3], [12.25, 63.07], ['pen', 'paper']]
print("\nMulti-dimensional list:", nested)
print(type(nested))
Output:
Empty list: []
<class 'list'>
List with different data types: [1, 33.33, 'python', False, [7, 8, 9]]
<class 'list'>
Multi-dimensional list: [[1, 2, 3], [12.25, 63.07], ['pen', 'paper']]
<class 'list'>
Tuple
A tuple is defined by the class tuple of Python. Like a list, a Python tuple is also an ordered collection of data that stores the sequence of Python objects. The difference between a list and a tuple is that a tuple is an immutable sequence. That means we cannot modify a tuple once defined. We create a tuple by placing its elements inside parenthesis. For example,
# empty tuple
empty = ()
print("Empty tuple:", empty)
print(type(empty))
# tuple having elements of different data types
mytup = (1, 33.33, 'python', False, [7, 8, 9])
print("\nTuple with different data types:", mytup)
print(type(mytup))
# multi-dimensional tuple or nested tuples
nested = ((1, 2, 3), (12.25, 63.07), ('pen', 'paper'))
print("\nNested tuples:", nested)
print(type(nested))
Output:
Empty tuple: ()
<class 'tuple'>
Tuple with different data types: (1, 33.33, 'python', False, [7, 8, 9])
<class 'tuple'>
Nested tuples: ((1, 2, 3), (12.25, 63.07), ('pen', 'paper'))
<class 'tuple'>
Dictionary
Dictionary data type is defined by the class dict of Python. A Python dictionary is a data type that stores data in form of key-value pair. It is a collection of python objects where each object comprises a Key and its corresponding Value. It’s a mapping data type that stores the elements mapped with their values. For example,
# empty dictionary
empty = {}
print("Empty dictionary:", empty)
print(type(empty))
# dictionary with hetergeneous data types
mydict = {'one': 1, 'two': 2, 100: 'century', 'list':[5, 6, 7]}
print("\nDictionary with hetergeneous data types:", mydict)
print(type(mydict))
Output:
Empty dictionary: {}
<class 'dict'>
Dictionary with hetergeneous data types: {'one': 1, 'two': 2, 100: 'century', 'list': [5, 6, 7]}
<class 'dict'>
Set
A set in python is an unordered collection of data where each data item must be immutable and unique. It may contain different data items of different data types provided they are immutable. A set is defined by the class set of Python. A set can be created by placing its elements inside curly braces or by using set()
method.
In addition, Python has one more data type under this category defined by the class frozenset called Frozen set. It’s an immutable version of the set. It is defined using the built-in method frozenset()
. Example,
# set of numbers
myset = {1, 2, 3, 4, 5}
print("Set of numbers:", myset)
print(type(myset))
# set using set() method
myset2 = set([10, 20, 30, 40, 50])
print("\nSet using set() method:", myset2)
print(type(myset2))
# frozen set of numbers
froz_set = frozenset({5, 10, 15, 20, 25})
print("\nFrozen set:", froz_set)
print(type(froz_set))
Output:
Set of numbers: {1, 2, 3, 4, 5}
<class 'set'>
Set using set() method: {40, 10, 50, 20, 30}
<class 'set'>
Frozen set: frozenset({20, 5, 25, 10, 15})
<class 'frozenset'>
Conclusion
In this article, we have read about Python data types. We have discussed about numeric types including integer, float, and complex number, sequence types that include strings, lists, and tuples. We have also discussed Boolean, dictionary, and set data types.