Python Data Types
Learn how Python represents different kinds of information. Understand numbers, strings, booleans, collections, None, and the difference between mutable and immutable objects.
What You Will Learn
Data Types
Understand why Python uses different types to represent different kinds of information.
Numbers
Work with integers, floating-point numbers and complex numbers.
Text & Logic
Learn strings and Boolean values.
Collections
Understand lists, tuples, sets and dictionaries.
What Are Data Types?
A data type describes the kind of value an object represents.
For example, a person's age is normally represented by an integer, while their name is represented by a string.
name = "Ali"
age = 25
height = 5.9
is_student = True
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Why Data Types Matter
Data types determine what kind of operations can be performed on a value.
For example, numbers can be mathematically added, while strings can be joined together.
number_result = 10 + 20
text_result = "Hello " + "Python"
print(number_result)
print(text_result)
30 Hello Python
Understanding data types helps you predict what Python will do when values are combined, compared or modified.
Checking Data Types
Python provides the built-in type() function
for checking the type of an object.
value = 100
print(type(value))
value = "Python"
print(type(value))
<class 'int'> <class 'str'>
Numeric Data Types
Python provides three built-in numeric types: int, float and complex.
whole_number = 100
decimal_number = 12.5
complex_number = 3 + 4j
print(type(whole_number))
print(type(decimal_number))
print(type(complex_number))
<class 'int'> <class 'float'> <class 'complex'>
Integers
An integer, represented by
int, is a whole number without a decimal
component.
Integers can be positive, negative or zero.
age = 25
temperature = -5
score = 0
print(age)
print(temperature)
print(score)
25 -5 0
Python integers can represent arbitrarily large whole numbers, subject to available memory.
Floating-Point Numbers
A float represents a number with a decimal component.
price = 99.99
temperature = -2.5
rating = 4.8
print(type(price))
print(type(temperature))
print(type(rating))
<class 'float'> <class 'float'> <class 'float'>
Floating-point numbers are stored using a binary representation, so some decimal values cannot be represented with perfect precision. This matters when working with financial or highly precise calculations.
Complex Numbers
Python also supports complex numbers. A complex number contains a real part and an imaginary part.
Python uses j to represent the imaginary
component.
number = 3 + 4j
print(number)
print(number.real)
print(number.imag)
(3+4j) 3.0 4.0
Complex numbers are commonly used in scientific, engineering and mathematical applications.
Strings
A string, represented by
str, is a sequence of characters used to
represent text.
Strings can be written using single quotes, double quotes or triple quotes.
name = "Ali"
city = 'Multan'
message = """Welcome to PyLearn"""
print(name)
print(city)
print(message)
Ali Multan Welcome to PyLearn
Strings are immutable, which means the existing string object cannot be changed in place. Operations that appear to modify a string generally create a new string.
Booleans
The Boolean type, written as bool, has two
values:
True and False.
is_logged_in = True
has_permission = False
print(is_logged_in)
print(has_permission)
print(type(is_logged_in))
True False <class 'bool'>
Boolean values are especially important when working with conditions and decision-making.
Lists
A list is an ordered collection that can contain multiple values.
Lists are created using square brackets
[].
fruits = ["apple", "banana", "orange"]
print(fruits)
print(fruits[0])
['apple', 'banana', 'orange'] apple
Lists are mutable, meaning their contents can be changed after creation.
fruits = ["apple", "banana"]
fruits.append("orange")
print(fruits)
['apple', 'banana', 'orange']
Tuples
A tuple is an ordered collection that is generally used for values that should not be changed.
Tuples are commonly written using parentheses.
coordinates = (10, 20)
print(coordinates)
print(coordinates[0])
(10, 20) 10
Tuples are immutable, so their existing elements cannot be changed after the tuple has been created.
Sets
A set is an unordered collection of unique elements.
Sets are useful when you need to remove duplicates or perform mathematical set operations.
numbers = {1, 2, 3, 3, 4}
print(numbers)
The duplicate 3 is represented only once
in the set.
Sets do not provide sequence-style indexing like lists and tuples.
Dictionaries
A dictionary, represented by
dict, stores data as
key-value pairs.
student = {
"name": "Ali",
"age": 21,
"city": "Multan"
}
print(student["name"])
print(student["age"])
Ali 21
Dictionaries are extremely useful for representing structured information.
student = {
"name": "Ali",
"age": 21
}
student["age"] = 22
print(student)
{'name': 'Ali', 'age': 22}
None
None represents the absence of a value.
Its type is NoneType.
result = None
print(result)
print(type(result))
None <class 'NoneType'>
None is often used to indicate that a
value has not been provided yet or that a function
intentionally has no meaningful return value.
Mutable vs Immutable Types
One important concept in Python is whether an object is mutable or immutable.
Mutable Objects
Mutable objects can be changed after they are created.
Common mutable types include:
- Lists
- Dictionaries
- Sets
Immutable Objects
Immutable objects cannot be changed after they are created.
Common immutable types include:
- Integers
- Floats
- Booleans
- Strings
- Tuples
numbers = [1, 2, 3]
numbers.append(4)
print(numbers)
[1, 2, 3, 4]
text = "Python"
text = text + " Programming"
print(text)
Python Programming
In the string example, Python creates a new string rather than changing the original string object in place.
Using type() in Real Programs
The type() function is especially useful
when learning Python and debugging unexpected values.
values = [
42,
3.14,
"Python",
True,
[1, 2, 3],
(1, 2),
{1, 2},
{"name": "Ali"},
None
]
for value in values:
print(type(value))
<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'list'> <class 'tuple'> <class 'set'> <class 'dict'> <class 'NoneType'>
Common Mistakes
1. Confusing Numbers and Strings
age = 25
age_text = "25"
print(type(age))
print(type(age_text))
Although both values visually contain
25, one is an integer and the other is
a string.
2. Using the Wrong Collection
Choose a collection based on what your program needs. For example, use a list when order and duplicates matter, and a set when uniqueness is important.
3. Confusing None With False
None means the absence of a value.
False is a Boolean value representing
logical falsity. They are different objects and should
not be treated as interchangeable.
4. Expecting Strings to Change In Place
Strings are immutable. String operations produce new string values rather than modifying the existing string object.
Practice Exercises
Identify the Data Types
Create the following values and use
type() to identify their types:
10025.5"Python"True[1, 2, 3]
Create a Student Record
Create a dictionary containing a student's name, age, marks and whether they passed.
Use an appropriate data type for every value.
List vs Tuple
Create a list of three favorite foods and a tuple containing three coordinates. Print both.
Remove Duplicates
Create a list containing duplicate numbers. Convert it to a set and observe what happens to the duplicates.
Mini Challenge
Build a Personal Profile
Create a Python program representing a person's profile using several different data types.
Your profile should include:
- Name as a string
- Age as an integer
- Height as a float
- Student status as a Boolean
- Favorite subjects as a list
- Birth coordinates as a tuple
- Unique skills as a set
- Additional details as a dictionary
name = "Ali"
age = 22
height = 5.9
is_student = True
subjects = ["Python", "SQL", "Excel"]
coordinates = (30.2, 71.4)
skills = {"Python", "SQL", "Power BI"}
profile = {
"city": "Multan",
"course": "Data Analytics"
}
print(name)
print(age)
print(height)
print(is_student)
print(subjects)
print(coordinates)
print(skills)
print(profile)
Quick Knowledge Check
Which data type represents whole numbers?
Which data type is used for text?
Which collection stores key-value pairs?
Which collection automatically keeps only unique elements?
What does None represent?
Which of the following is mutable?
Lesson Summary
Python provides several built-in data types for representing different kinds of information.
-
intrepresents whole numbers. -
floatrepresents floating-point numbers. -
complexrepresents complex numbers. -
strrepresents text. -
boolrepresentsTrueorFalse. -
liststores an ordered, mutable collection. -
tuplestores an ordered, immutable collection. -
setstores unique elements. -
dictstores key-value pairs. -
Nonerepresents the absence of a value. -
type()can be used to inspect an object's type.
Data types are fundamental to Python. Once you understand what type of data you are working with, it becomes much easier to choose the correct operations and data structures.
You've reached the end!
Complete the exercises and knowledge check, then mark this lesson as complete.