Python Data Types: Complete Guide with Examples

Introduction

Python data types is essential because they influence how data is stored, manipulated, and processed within a program. Choosing the appropriate data type helps improve code efficiency, readability, and performance.

In this tutorial, you will learn what Python data types are, the different built-in data types, examples, real-life applications, common mistakes, and interview questions.

What are Python Data Types?

A data type is a classification that specifies what kind of value a variable can hold.

Python provides several built-in data types that allow developers to store different forms of data.

For example:


name = "John"
age = 25
price = 99.99

Here:

  • “John” is a string (str)
  • 25 is an integer (int)
  • 99.99 is a floating-point number (float)

Note: Python automatically determines the data type when a value is assigned to a variable.

Why are Data Types Important?

Data types help Python understand how data should be stored and processed.

Benefits of Data Types:

  • Improve program efficiency
  • Reduce programming errors
  • Enable type-specific operations
  • Organize data properly
  • Improve readability and maintainability

Without data types, Python would not know how to handle different kinds of values.

Checking Data Types

Python provides the type() function to determine the data type of a variable.

Example:


name = "John"
print(type(name))

Output:

<class ‘str’>

Another example:


age = 25
print(type(age))

Output:

<class ‘int’>

Categories of Python Data Types

Python data types can be divided into several categories:

  1. Numeric Types
  2. Text Type
  3. Sequence Types
  4. Set Types
  5. Mapping Type
  6. Boolean Type
  7. Binary Types
  8. None Type

Let’s explore each category.

1. Numeric Data Types

Numeric data types are used to store numbers.

Integer (int)

Integers represent whole numbers.

Example


age = 25
print(age)

Output:

25

Integers can be positive or negative.


x = -100

Float (float)

Floats represent decimal numbers.

Example:


price = 99.99
print(price)

Output:

99.99

Floats are commonly used for measurements and calculations.

Complex (complex)

Complex numbers contain real and imaginary parts.

Example:


num = 3 + 4j
print(num)

Output:

(3+4j)

Complex numbers are mainly used in scientific and mathematical applications.

2. String Data Type (str)

Strings are used to store text data.

Example:


name = "John"
print(name)

Output:

John

Strings can be enclosed in:

1. Double Quotes


name = "John"

2. Single Quotes


name = 'John'

3. Multi-Line Strings


message = """
Welcome
to
Python
"""

Strings support various operations such as concatenation and slicing.

3. List Data Type (list)

Lists are ordered and mutable collections.

Example:


fruits = ["Apple", "Banana", "Mango"]
print(fruits)

Output:

[‘Apple’, ‘Banana’, ‘Mango’]

Access Elements:


print(fruits[0])

Output:

Apple

Lists can be modified after creation.

4. Tuple Data Type (tuple)

Tuples are ordered but immutable collections.

Example:


colors = ("Red", "Green", "Blue")
print(colors)

Output:

(‘Red’, ‘Green’, ‘Blue’)

Unlike lists, tuple elements cannot be modified.

5. Range Data Type (range)

The range() function generates a sequence of numbers.

Example:


numbers = range(5)
print(list(numbers))

Output:

[0, 1, 2, 3, 4]

Ranges are commonly used in loops.

6. Dictionary Data Type (dict)

Dictionaries store data as key-value pairs.

Example:


student = {
    "name": "John",
    "age": 20
}
print(student)

Output:

{‘name’: ‘John’, ‘age’: 20}

Access values:


print(student["name"])

Output:

John

Dictionaries are widely used for structured data.

7. Set Data Type (set)

Sets store unique values.

Example:


numbers = {1, 2, 3, 4}
print(numbers)

Output:

{1, 2, 3, 4}

Sets automatically remove duplicates.


numbers = {1, 1, 2, 2, 3}
print(numbers)

Output:

{1, 2, 3}

8. Frozen Set Data Type (frozenset)

A frozen set is an immutable version of a set.

Example:


numbers = frozenset([1, 2, 3])
print(numbers)

Output:

frozenset({1, 2, 3})

9. Boolean Data Type (bool)

Boolean values represent truth values.

Example:


is_logged_in = True
print(is_logged_in)

Output:

True

Possible values:


True
False

Booleans are commonly used in conditions and loops.

10. Binary Data Types

Python supports binary data types.

bytes


data = b"Hello"
print(data)

Output:

b’Hello’

bytearray


data = bytearray(5)
print(data)

Output:

bytearray(b’\x00\x00\x00\x00\x00′)

memoryview


data = memoryview(bytes(5))
print(data)

Output:

<memory at …>

Binary types are useful when working with files and network communication.

11. None Data Type

The None type represents the absence of a value.

Example:


result = None
print(result)

Output:

None

None is often used as a placeholder.

Real-Life Example

Suppose you are creating an online shopping application.


product_name = "Laptop"
price = 75000.50
quantity = 5
is_available = True

Here:

Variable Data Type
product_name String
price Float
quantity Integer
is_available Boolean

Display values:


print(product_name)
print(price)
print(quantity)
print(is_available)

Output:

Laptop
75000.5
5
True

Real-world applications use multiple data types together.

Summary of Python Data Types

Data Type Example
int 10
float 10.5
complex 3+4j
str “Python”
list [1,2,3]
tuple (1,2,3)
range range(5)
dict {“name”:”John”}
set {1,2,3}
frozenset frozenset({1,2,3})
bool True
bytes b”Hello”
bytearray bytearray(5)
memoryview memoryview(bytes(5))
NoneType None

Common Mistakes

1. Confusing Strings and Numbers

Incorrect:


age = "25"
print(age + 5)

Output:

TypeError

2. Modifying Tuples

Incorrect:


colors = ("Red", "Blue")
colors[0] = "Green"

Output:

TypeError

Tuples are immutable.

3. Using Dictionary Keys Incorrectly

Incorrect:


student = {"name": "John"}
print(student["age"])

Output:

KeyError

4. Forgetting List Index Limits

Incorrect:


fruits = ["Apple"]
print(fruits[5])

Output:

IndexError

5. Misunderstanding None

Incorrect:


value = None
print(value + 10)

Output:

TypeError

Conclusion

Python data types are fundamental building blocks that determine how information is stored and processed in a program. Python provides a rich collection of built-in data types, including numeric types, strings, lists, tuples, dictionaries, sets, Boolean values, binary types, and the None type.

Understanding data types is essential because they influence how operations are performed and how memory is managed. By learning when and how to use different data types, you can write more efficient, readable, and reliable Python programs.

Python Data Types – Interview Questions

Q 1: What are data types in Python?
Ans: Data types define the type of value a variable can store.
Q 2: Name some built-in data types in Python.
Ans: int, float, str, list, tuple, and dict.
Q 3: How do you check the data type of a variable?
Ans: By using the type() function.
Q 4: Is Python dynamically typed?
Ans: Yes, Python determines data types at runtime.
Q 5: Can a variable change its data type?
Ans: Yes, a variable can store different data types at different times.

Python Data Types – Objective Questions (MCQs)

Q1. Which of the following is a valid data type in Python?






Q2. What will be the data type of the variable x in the following code?






Q3. Which of the following is an example of a list in Python?






Q4. What will be the output of the following code?

x = True
print(type(x))






Q5. Which of the following statements about Python data types is true?






Related Python Tutorials