Python Data Types Explained With Practical Examples
When you write a Python program, you work with different kinds of values every day.
You might store a person's name, an age, a price, a list of products, or information returned by an API. Python needs to know what kind of value it is dealing with, and that's where data types come in.
If you're learning Python, data types are one of those topics that look simple at first but become important when you start working with functions, APIs, databases, Pandas, or data analysis.
Let's look at the most commonly used Python data types with simple examples.
- Numbers: "int" and "float"
Python uses "int" for whole numbers and "float" for numbers with decimal values.
age = 45
price = 1250.50
print(type(age))
print(type(price))
Output:
For example, an age would normally be an integer, while a product price might contain decimal values.
One thing worth knowing about floats is that they don't always represent decimal numbers exactly.
print(0.1 + 0.2)
You may get:
0.30000000000000004
This isn't a Python bug. It comes from the way floating-point numbers are represented internally.
It becomes especially important when you're working with calculations where precision matters.
- Strings
A string is a sequence of characters.
name = "Kailas"
print(name)
print(type(name))
Strings support indexing:
language = "Python"
print(language[0])
Output:
P
You can also take a part of a string using slicing:
print(language[0:3])
Output:
Pyt
One important point is that strings are immutable.
You can't change one character directly after the string has been created.
name = "Python"
name[0] = "J" # TypeError
- Lists
A list is useful when you need to keep multiple values together.
languages = ["Python", "SQL", "Java"]
print(languages)
One of the biggest advantages of a list is that it can be changed.
languages[1] = "C++"
print(languages)
Output:
['Python', 'C++', 'Java']
You can also add new values:
languages.append("Go")
This is why lists are commonly used when the collection of values may change during program execution.
- Tuples
Tuples also store multiple values, but they are immutable.
coordinates = (18.52, 73.85)
print(coordinates)
Once a tuple is created, you can't change one of its elements.
A simple way to remember the difference is:
List → mutable
Tuple → immutable
Neither is automatically better. The right choice depends on what you need to do with the data.
- Dictionaries
A dictionary stores information using key-value pairs.
student = {
"name": "Rahul",
"age": 25,
"city": "Pune"
}
print(student["name"])
Output:
Rahul
This is useful when the meaning of each value matters.
Compare:
student = ["Rahul", 25, "Pune"]
with:
student = {
"name": "Rahul",
"age": 25,
"city": "Pune"
}
The second version makes the data much easier to understand.
Dictionaries are widely used in Python applications, APIs, configuration files, and JSON data.
- Sets
A set is a collection that stores unique values.
numbers = {10, 20, 30, 10, 20}
print(numbers)
The duplicate values are removed.
Sets are particularly useful when you need operations such as union, intersection, or difference.
For example:
a = {1, 2, 3}
b = {3, 4, 5}
print(a.intersection(b))
Output:
{3}
If your main requirement is to work with unique values, a set can be very convenient.
- Boolean Values
Boolean values represent either "True" or "False".
For example:
age = 25
is_adult = age >= 18
print(is_adult)
Output:
True
You'll see Boolean values everywhere in Python, especially in conditions, filtering, validation, and data processing.
For example:
if is_adult:
print("Allowed")
- What is "None" in Python?
"None" is a special value that represents the absence of a value.
result = None
print(result)
print(type(result))
Output:
None
It's important not to confuse "None" with:
- "0"
- "False"
- """"
- an empty list
They represent different things.
You'll often encounter "None" when working with functions, databases, APIs, and optional values.
- Type Conversion
Sometimes the value you receive isn't in the type you need.
A common example is user input.
age = input("Enter your age: ")
print(type(age))
Even if the user enters:
45
the result from "input()" is a string.
If you want to perform numerical calculations, you can convert it:
age = int(age)
print(age + 5)
Python provides several common conversion functions:
int()
float()
str()
list()
tuple()
But conversion isn't always possible.
For example:
int("Python")
will raise a "ValueError" because ""Python"" cannot be converted into an integer.
This is something you'll frequently deal with when processing files, APIs, databases, and user input.
- Mutable vs Immutable Data Types
This is one of the Python concepts that is worth understanding properly.
Some objects can be changed after they are created. Others cannot.
Common mutable types include:
list
dict
set
Common immutable types include:
int
float
str
tuple
bool
For example, a list can be modified:
numbers = [10, 20, 30]
numbers.append(40)
print(numbers)
The same list now contains the additional value.
Strings behave differently:
name = "Python"
name[0] = "J"
The code above raises an error because strings are immutable.
This distinction becomes particularly important when you start learning about object references, copying, functions, and shallow vs deep copy.
A Practical Example
Imagine you're working on a small employee application.
You might have:
employee_id = 101
employee_name = "Amit"
salary = 55000.50
is_active = True
skills = ["Python", "SQL", "Power BI"]
employee = {
"id": employee_id,
"name": employee_name,
"salary": salary,
"active": is_active,
"skills": skills
}
Here, several Python data types are working together:
- "101" → "int"
- ""Amit"" → "str"
- "55000.50" → "float"
- "True" → "bool"
- "skills" → "list"
- "employee" → "dict"
This is much closer to how you'll actually encounter data types in a real Python application.
Why Data Types Matter Beyond Python Basics
Data types become even more important when you move into data analytics and data engineering.
For example, an API might return:
age = "45"
while your calculation expects:
age = 45
Or a column in a dataset might contain a mixture of numbers and strings.
Before performing calculations or transformations, you need to understand what you're actually working with.
That's why Python data types are not just an interview topic. They are part of everyday Python programming.
Final Thoughts
The most important Python data types to understand first are:
int
float
str
bool
list
tuple
set
dict
None
Don't try to memorize them only for an interview.
Try small examples and pay attention to what can be changed, how values are accessed, and what happens when you convert one type to another.
Once these basics are clear, topics like functions, APIs, Pandas, data cleaning, and data analysis become much easier to understand.
Preparing for a Python Interview?
If you want to go beyond these basic examples, I've put together a detailed Python Data Types Interview Questions and Answers guide on SankalanTech.
It covers questions around:
- Built-in data types
- Lists vs tuples
- Dictionaries
- Sets
- "int" vs "float"
- Strings
- "None"
- Type conversion
- Shallow vs deep copy
- Dynamic typing
Read the complete Python Data Types interview guide on SankalanTech:
https://www.sankalandtech.com/Tutorials/Python/interview-questions/python-data-types-guide.html
Use this article to understand the concepts with examples, and the detailed guide for deeper interview preparation.
Top comments (0)