PyLearn Learn Python. Build Skills.
Lesson 07 · Foundations

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.

35–45 minutes Beginner Python Foundations
0%
01

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.

02

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.

Different types
name = "Ali"
age = 25
height = 5.9
is_student = True

print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
03

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.

Same operator, different behavior
number_result = 10 + 20

text_result = "Hello " + "Python"

print(number_result)
print(text_result)
Output
30
Hello Python
Types affect behavior.

Understanding data types helps you predict what Python will do when values are combined, compared or modified.

04

Checking Data Types

Python provides the built-in type() function for checking the type of an object.

Using type()
value = 100

print(type(value))

value = "Python"

print(type(value))
Output
<class 'int'>
<class 'str'>
05

Numeric Data Types

Python provides three built-in numeric types: int, float and complex.

Numeric types
whole_number = 100
decimal_number = 12.5
complex_number = 3 + 4j

print(type(whole_number))
print(type(decimal_number))
print(type(complex_number))
Output
<class 'int'>
<class 'float'>
<class 'complex'>
06

Integers

An integer, represented by int, is a whole number without a decimal component.

Integers can be positive, negative or zero.

Integer examples
age = 25
temperature = -5
score = 0

print(age)
print(temperature)
print(score)
Output
25
-5
0

Python integers can represent arbitrarily large whole numbers, subject to available memory.

07

Floating-Point Numbers

A float represents a number with a decimal component.

Float examples
price = 99.99
temperature = -2.5
rating = 4.8

print(type(price))
print(type(temperature))
print(type(rating))
Output
<class 'float'>
<class 'float'>
<class 'float'>
Floating-point precision

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.

08

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.

Complex number
number = 3 + 4j

print(number)
print(number.real)
print(number.imag)
Output
(3+4j)
3.0
4.0

Complex numbers are commonly used in scientific, engineering and mathematical applications.

09

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.

String examples
name = "Ali"
city = 'Multan'
message = """Welcome to PyLearn"""

print(name)
print(city)
print(message)
Output
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.

10

Booleans

The Boolean type, written as bool, has two values: True and False.

Boolean values
is_logged_in = True
has_permission = False

print(is_logged_in)
print(has_permission)

print(type(is_logged_in))
Output
True
False
<class 'bool'>

Boolean values are especially important when working with conditions and decision-making.

11

Lists

A list is an ordered collection that can contain multiple values.

Lists are created using square brackets [].

Creating a list
fruits = ["apple", "banana", "orange"]

print(fruits)
print(fruits[0])
Output
['apple', 'banana', 'orange']
apple

Lists are mutable, meaning their contents can be changed after creation.

Modifying a list
fruits = ["apple", "banana"]

fruits.append("orange")

print(fruits)
Output
['apple', 'banana', 'orange']
12

Tuples

A tuple is an ordered collection that is generally used for values that should not be changed.

Tuples are commonly written using parentheses.

Creating a tuple
coordinates = (10, 20)

print(coordinates)
print(coordinates[0])
Output
(10, 20)
10

Tuples are immutable, so their existing elements cannot be changed after the tuple has been created.

13

Sets

A set is an unordered collection of unique elements.

Sets are useful when you need to remove duplicates or perform mathematical set operations.

Set example
numbers = {1, 2, 3, 3, 4}

print(numbers)

The duplicate 3 is represented only once in the set.

Remember:

Sets do not provide sequence-style indexing like lists and tuples.

14

Dictionaries

A dictionary, represented by dict, stores data as key-value pairs.

Dictionary example
student = {
    "name": "Ali",
    "age": 21,
    "city": "Multan"
}

print(student["name"])
print(student["age"])
Output
Ali
21

Dictionaries are extremely useful for representing structured information.

Updating dictionary data
student = {
    "name": "Ali",
    "age": 21
}

student["age"] = 22

print(student)
Output
{'name': 'Ali', 'age': 22}
15

None

None represents the absence of a value. Its type is NoneType.

None example
result = None

print(result)
print(type(result))
Output
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.

16

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
Mutable list
numbers = [1, 2, 3]

numbers.append(4)

print(numbers)
Output
[1, 2, 3, 4]
Immutable string
text = "Python"

text = text + " Programming"

print(text)
Output
Python Programming

In the string example, Python creates a new string rather than changing the original string object in place.

17

Using type() in Real Programs

The type() function is especially useful when learning Python and debugging unexpected values.

Inspecting values
values = [
    42,
    3.14,
    "Python",
    True,
    [1, 2, 3],
    (1, 2),
    {1, 2},
    {"name": "Ali"},
    None
]

for value in values:
    print(type(value))
Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
<class 'NoneType'>
18

Common Mistakes

1. Confusing Numbers and Strings

Different types
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.

19

Practice Exercises

Exercise 1

Identify the Data Types

Create the following values and use type() to identify their types:

  • 100
  • 25.5
  • "Python"
  • True
  • [1, 2, 3]
Exercise 2

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.

Exercise 3

List vs Tuple

Create a list of three favorite foods and a tuple containing three coordinates. Print both.

Exercise 4

Remove Duplicates

Create a list containing duplicate numbers. Convert it to a set and observe what happens to the duplicates.

20

Mini Challenge

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
Example structure
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)
21

Quick Knowledge Check

1

Which data type represents whole numbers?

2

Which data type is used for text?

3

Which collection stores key-value pairs?

4

Which collection automatically keeps only unique elements?

5

What does None represent?

6

Which of the following is mutable?

22

Lesson Summary

Python provides several built-in data types for representing different kinds of information.

  • int represents whole numbers.
  • float represents floating-point numbers.
  • complex represents complex numbers.
  • str represents text.
  • bool represents True or False.
  • list stores an ordered, mutable collection.
  • tuple stores an ordered, immutable collection.
  • set stores unique elements.
  • dict stores key-value pairs.
  • None represents the absence of a value.
  • type() can be used to inspect an object's type.
Key takeaway

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.