Data types in Python
Introduction
If you’re coding in the Python programming language, it can quickly become overwhelming with all of the data types available. It is incredibly important to understand these data structures, as they make coding more efficient and help ensure that your programs run correctly. With so many options, it can be daunting to figure out which type best suits a given situation. In this blog post, we’ll explore the different kinds of data types available in python — from strings and integers to dictionaries and sets — explaining what each one is used for and how they compare to one another. Whether you're new to coding or an experienced programmer looking for a refresher course, this article has something for everyone!
What is a Data Type in Python
A data type in python is a classification of data that determines which characteristics and capabilities are associated with it. It can be used to identify data ranging from string variables, numerical data, lists, tuples, and dictionaries. Knowing the data type enables the interpreter to allocate memory for any data element and then allow relevant operations applicable to that data type. These data types are an essential part of the python programming language since they provide the framework that all programs follow. A clear understanding of these data types gives the user more control when writing the programs, allowing them to choose the right data type for each data element according to the program requirements and expectations.
Types of Data Types in Python
Python has various built-in data types which will be discussed within this article:
- Numeric - int, float, complex
- String - str
- Sequence - list, tuple, range
- Binary - bytes, bytearray, memoryview
- Mapping - dict
- Boolean - bool
- Set - set, frozenset
- None - NoneType
Python Numeric Data Type
- int (signed integers)
- long (long integers, they can also be represented by octal and hexadecimal)
- float (floating point real values)
- complex (complex numbers)
int | long | float | complex |
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
-0490 | 535633629843L | -90. | -.6545+0J |
-0x260 | -052318172735L | -32.54e100 | 3e+26J |
0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
Example
# integer variable.
a=150
print("The type of variable having value", a, " is ", type(a))
# float variable.
b=20.846
print("The type of variable having value", b, " is ", type(b))
# complex variable.
c=18+3j
print("The type of variable having value", c, " is ", type(c))
Output
The type of variable having value 150 is <class 'int'>
The type of variable having value 20.846 is <class 'float'>
The type of variable having value (18+3j) is <class 'complex'>
Python String Data Type
The string data type in python is very useful and versatile. It allows the developer to process string values that contain both alphanumeric characters and symbols. Working with string data types can be done using various string methods within the Python programming language, giving the user the freedom to manipulate string data sets in a variety of ways. Additionally, string literals can also be used within a string variable, allowing for even more efficient manipulation of string data by allowing predefined sets of variables and specialized syntax under the same variable name. Subsets of strings of the python language can be taken by using the slice operator ([ ] and [:] ) with all the indexes that are starting at 0 at the beginning of the string and then working their way from -1 at the end.
Example
str = 'Hello World!'
print (str) # Prints complete string
print (str[0]) # Prints first character of the string
print (str[2:5]) # Prints characters starting from 3rd to 5th
print (str[2:]) # Prints string stating from 3rd character
print (str * 2) # Prints string two times
print (str + "TEST") # Prints concatenated string
Output
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
Python List Data Type
Python list data type is an essential tool for programming. It holds multiple values in an organized and efficient way, allowing programmers to edit, store and access complex sets of data with a single instruction. It is one of the most versatile data structures that python has to offer, as the list can store anything from strings to complex objects. Python list data type is a very important feature for high-level programming applications because it allows the user to keep track of multiple pieces of information at once. The values that are stored in a Python list can be accessed by using the slice operator ([ ] and [:]) with indexes starting at 0 at the beginning of the list and working their way to end -1. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator.
Example
list = [ 'abcd', 786 , 2.23, ‘Scholar-Hat’, 70.2 ]
tinylist = [123, 'Scholar-Hat']
print (list) # Prints complete list
print (list[0]) # Prints first element of the list
print (list[1:3]) # Prints elements starting from 2nd till 3rd
print (list[2:]) # Prints elements starting from 3rd element
print (tinylist * 2) # Prints list two times
print (list + tinylist) # Prints concatenated lists
Output
['abcd', 786, 2.23, 'Scholar-Hat', 70.2]
abcd
[786, 2.23]
[2.23, ‘Scholar-Hat', 70.2]
[123, 'Scholar-Hat', 123, 'john']
['abcd', 786, 2.23, 'Scholar-Hat', 70.2, 123, 'Scholar-Hat']
Tuple data type in python
Tuple data type in python is a powerful and flexible type of data structure. Tuple objects are sequences of immutable Python objects that can hold a group of elements together. Tuple data type is incredibly versatile since its elements can be of any data type, including numbers, strings, lists, dictionaries, and even other tuples. Tuple operation is simpler than those for lists since they cannot be altered multiple times. Tuple data type is different from list data type in python because Lists data types are enclosed in brackets ( [ ] ) and their elements and size can be changed, but the tuples data types are enclosed in parentheses ( ( ) ) and cannot be updated.
Example
tuple = ( 'abcd', 786 , 2.23, 'Scholar-Hat', 70.2 )
tinytuple = (123, 'Scholar-Hat')
print (tuple) # Prints the complete tuple
print (tuple[0]) # Prints first element of the tuple
print (tuple[1:3]) # Prints elements of the tuple starting from 2nd till 3rd
print (tuple[2:]) # Prints elements of the tuple starting from 3rd element
print (tinytuple * 2) # Prints the contents of the tuple twice
print (tuple + tinytuple) # Prints concatenated tuples
Output
('abcd', 786, 2.23, 'Scholar-Hat', 70.2)
abcd
(786, 2.23)
(2.23, 'Scholar-Hat', 70.2)
(123, ‘Scholar-Hat', 123, 'Scholar-Hat')
('abcd', 786, 2.23, 'Scholar-Hat', 70.2, 123, 'Scholar-Hat')
Range data type in python
Range data type in python is an in-built feature of the python programming language. It returns a sequence of numbers that starts from 0 and increments to 1 until it reaches a specified number. The developer uses the range() function with various loops, such as for and while loop for generating a sequence of numbers.
The description of the parameters used in python language:
- start: In this parameter, it uses an integer number to specify starting position, (Its optional, Default: 0)
- stop: In this parameter, it uses an integer number to specify starting position (It's mandatory)
- step: In this parameter, it uses an integer number to specify increment, (Its optional, Default: 1)
Example
for i in range(1, 5):
print(i)
Output
1
2
3
4
Dictionary data type in python
Python’s Dictionary data type is a powerful and versatile tool that allows users to quickly store, access, and manipulate data. By using a key-value pair model, Dictionary data types in python enable coders to easily remember and store information without the need for complicated variable names. Dictionary data types are also simple to use as Python automates most of the mundane setup tasks involved in creating Dictionary objects. Dictionaries in python language are enclosed by curly braces ({ }) and their values can be assigned and accessed using square braces ([]).
Example
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'Scholar-Hat','code':6734, 'dept': 'sales'}
print (dict['one']) # Prints value for 'one' key
print (dict[2]) # Prints value for 2 key
print (tinydict) # Prints complete dictionary
print (tinydict.keys()) # Prints all the keys
print (tinydict.values()) # Prints all the values
Output
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'Scholar-Hat'}
['dept', 'code', 'name']
['sales', 6734, 'Scholar-Hat']
Boolean data type in python
Boolean data type in Python is a powerful way to store and assign boolean (true or false) values in that particular code. It can be used to control program flow, perform boolean operations, and assign boolean variables. Boolean data types provide easy to understand answers, usually, the answer is either true or false; 0 or 1 if it’s stored as an integer. In the python programming language boolean data type is one of the built-in data types which represents one of the two values either True or False. Python bool() function allows the developer to evaluate the value of any expression and returns either True or False based on the expression.
Example
a = True
# display the value of a
print(a)
# display the data type of a
print(type(a))
Output
true
<class 'bool'>
Data type conversion in python
When manipulating data in Python, data type conversion is a necessity. However, data type conversion is not always an easy task. In Python, there are many different data types including strings, integers, floats, and boolean. To successfully convert data from one data type to another, the user must use the proper data type conversion techniques built-in in Python. By using the built-in functions and methods available in Python, the user as well as the developer can convert data from one data type to another quickly and effectively.
Example
a = str(1) # a will be "1"
b = str(2.2) # b will be "2.2"
c = str("3.3") # c will be "3.3"
print (a)
print (b)
print (c)
Output
1
2.2
3.3
Summary
Python has five standard data types − integers, floating point numbers, strings, lists, and dictionaries. In addition, Python also provides some special data types such as files, frozen sets, and a few more. Data structures are very important in programming. Different languages have different ways of representing data structures. Thus knowing the difference between them is very essential while coding in any language. This was all about the basics of data types in python. You must be now aware of the different types of data that can be represented using python. Stay tuned for our next blog where we will discuss how you can manipulate these data types to perform various operations on them!
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.