Data Types in Python
Data types are one of the most important concepts in Python because they define what kind of information a variable can store and how that information can be used. Whether you are working with numbers, text, collections, logical values, or empty values, Python provides built-in data types for handling different kinds of data efficiently.
Understanding Python data types is essential for beginners because variables can hold different kinds of values, and each type supports its own operations. In this guide, we will explore numeric types, sequence types, dictionaries, sets, Boolean values, NoneType, mutable and immutable data types, and methods for checking the type of a value.
Table of Contents

What Are Data Types in Python?
A data type describes the kind of value stored in a Python variable. For example, a whole number such as 25 is an integer, while 3.14 is a floating-point number. Text is represented using strings, and collections of values can be stored using lists, tuples, sets, and dictionaries.
Python uses dynamic typing, which means you do not have to declare the data type of a variable before assigning a value to it. Python determines the type automatically based on the value assigned.
age = 25
name = "John"
price = 99.50
print(type(age))
print(type(name))
print(type(price))
In this example, Python identifies age as an integer, name as a string, and price as a floating-point value.
1. Numeric Data Types
Python provides several numeric data types for working with numbers. The three main numeric types are int, float, and complex.
Integer (int)
An integer represents a whole number without a decimal component. Integers can be positive, negative, or zero.
age = 25
temperature = -5
count = 0
print(age)
print(temperature)
print(count)
Float (float)
A float represents a number containing a decimal point. Floating-point values are commonly used for measurements, prices, percentages, and calculations requiring decimal values.
pi = 3.14159
price = 99.50
height = 5.8
print(pi)
print(price)
print(height)
Complex (complex)
Complex numbers contain a real part and an imaginary part. Python represents the imaginary component using the letter j.
z = 2 + 3j
print(z)
Numeric Calculation Example
a = 5
b = 2.5
c = a + b
print(c)
The result of this calculation is 7.5. Python automatically handles the numeric types involved in the operation.
2. Sequence Data Types
Sequence types store collections of values in an ordered manner. Common Python sequence types include strings, lists, tuples, and ranges.
String (str)
A string is a sequence of characters. Strings are used to store text such as names, messages, addresses, and descriptions.
greeting = "Hello, World!"
print(greeting)
Strings can be accessed using indexes, and Python provides many built-in operations for working with text.
List
A list is an ordered and mutable collection. It can contain multiple values, and those values do not necessarily have to be of the same type.
fruits = ["apple", "banana", "cherry"]
print(fruits)
print(fruits[0])
Because lists are mutable, their elements can be changed after the list has been created.
fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange"
print(fruits)
Tuple
A tuple is also an ordered collection, but unlike a list, it is immutable. Once a tuple is created, its existing elements cannot be modified.
coordinates = (10.0, 20.0)
print(coordinates)
Tuples are useful when you want to represent a fixed collection of related values.
Range
The range type represents a sequence of numbers. It is frequently used with loops.
r = range(1, 10)
for number in r:
print(number)
This example generates numbers starting from 1 up to, but not including, 10.
3. Mapping Type: Dictionary
A dictionary, represented by the dict type, stores data as key-value pairs. Each key is used to access its corresponding value.
student = {
"name": "John",
"age": 21,
"major": "CS"
}
print(student["name"])
The dictionary can also be modified after creation.
student["age"] = 22
print(student)
Dictionaries are useful when data needs to be organized using meaningful keys instead of numerical indexes.
4. Set Data Types
A set is a collection that stores unique values. Sets are unordered and mutable, which makes them useful when duplicate values need to be removed or when mathematical set operations are required.
colors = {"red", "green", "blue"}
print(colors)
Python also provides frozenset. A frozenset is similar to a set but is immutable.
vowels = frozenset(["a", "e", "i", "o", "u"])
print(vowels)
Set Operations
Sets support operations such as union, intersection, and difference. For example, the | operator can be used to find the union of two sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 | set2)
The result contains the unique values from both sets.
5. Boolean Data Type
The Boolean data type, written as bool, represents one of two logical values: True or False. Boolean values are commonly used in conditions and decision-making statements.
is_adult = True
is_student = False
print(is_adult and is_student)
The and operator returns True only when both conditions are true. In this example, the result is False.
6. NoneType
Python provides a special value called None, whose type is NoneType. It represents the absence of a value or a value that has not been assigned yet.
data = None
if data is None:
print("No data available")
None is different from values such as zero, an empty string, or False. It specifically represents the absence of a value.
Mutable vs Immutable Data Types
Another important concept in Python is whether a data type is mutable or immutable.
Mutable Data Types
Mutable objects can be changed after they are created. Common mutable data types include lists, dictionaries, and sets.
numbers = [1, 2, 3]
numbers.append(4)
print(numbers)
The original list is modified when a new value is added.
Immutable Data Types
Immutable objects cannot be changed after they are created. Common immutable types include integers, floats, strings, tuples, and frozensets.
name = "Python"
# A new string is created when the value is changed
name = name + " Programming"
print(name)
Understanding mutability becomes especially important when working with functions, objects, and memory references in Python.
Checking Data Types with type()
Python provides the built-in type() function for identifying the type of an object.
x = 42
print(type(x))
The output is:
<class 'int'>
You can use type() when debugging code or when you want to understand what kind of value is stored in a variable.
Using isinstance() to Check a Type
The isinstance() function checks whether an object belongs to a particular data type. It returns either True or False.
x = 42
print(isinstance(x, int))
The result is:
True
This approach is useful when a program needs to verify the type of a value before performing a particular operation.
Python Data Types at a Glance
| Data Type | Example | Main Characteristic |
|---|---|---|
| int | 25 | Whole numbers |
| float | 3.14 | Decimal numbers |
| complex | 2 + 3j | Real and imaginary values |
| str | "Python" | Text |
| list | [1, 2, 3] | Ordered and mutable |
| tuple | (1, 2, 3) | Ordered and immutable |
| range | range(1, 5) | Sequence of numbers |
| dict | {"name": "John"} | Key-value pairs |
| set | {1, 2, 3} | Unique values |
| frozenset | frozenset([1, 2]) | Immutable set |
| bool | True | Logical values |
| NoneType | None | Absence of a value |
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
YT:- DecodeIT
Conclusion
Python data types provide the foundation for storing and processing information in programs. Numeric types handle numbers, sequence types manage ordered collections, dictionaries organize key-value data, sets handle unique values, Boolean values represent logical conditions, and None represents the absence of a value.
It is equally important to understand the difference between mutable and immutable objects and to know how to inspect values using type() and isinstance(). Once these concepts are clear, working with variables, functions, collections, and more advanced Python features becomes much easier.
FAQs – Data Types in Python
1. What are data types in Python?
Data types define the kind of value stored by an object or variable, such as an integer, string, list, dictionary, or Boolean value.
2. What are the main numeric data types in Python?
The main numeric types are int, float, and complex.
3. What is the difference between a list and a tuple?
A list is mutable, meaning its contents can be changed. A tuple is immutable after it has been created.
4. What is a dictionary in Python?
A dictionary stores information as key-value pairs and allows values to be accessed through their keys.
5. What is the difference between mutable and immutable data types?
Mutable objects can be modified after creation, while immutable objects cannot be changed after they are created.
6. How can I check the type of a variable?
You can use the type() function, such as type(value), to determine the type of an object.
7. What does None mean in Python?
None represents the absence of a value and belongs to the NoneType data type.
8. What is isinstance() used for?
isinstance() checks whether an object is an instance of a specified type and returns True or False.