Skip to main content

Command Palette

Search for a command to run...

Python Data Types Explained with Examples

A Complete Beginner’s Guide to Understanding and Using Python Data Types Effectively

Published
3 min read
Python Data Types Explained with Examples

🐍 Python Data Types – A Complete Guide

Python is one of the most popular and powerful programming languages, known for its simplicity and readability. In Python, a data type defines what kind of value a variable can hold.

Every value in Python has a specific data type, and Python automatically assigns it — this is called Dynamic Typing.


🧩 What is a Data Type?

A Data Type determines:

  • What type of data a variable will hold (number, text, list, etc.)

  • What operations can be performed on that data.

Example:

x = 10       # Integer
y = "Hello"  # String
z = 3.14     # Float

Here, Python automatically understands the type of x, y, and z.


🔢 Python’s Built-in Data Types

Python provides several built-in data types. Let’s explore the most important ones.


1. Numeric Types

a) int (Integer)

Used to store whole numbers.

a = 25
b = -10
print(type(a))  # <class 'int'>

b) float (Floating Point Number)

Used to store decimal numbers.

x = 3.14
y = -2.5

c) complex

Used for complex numbers (with real and imaginary parts).

z = 2 + 3j
print(z.real)  # 2.0
print(z.imag)  # 3.0

2. Sequence Types

a) str (String)

A sequence of characters.

name = "Python"
print(name[0])  # P
print(len(name))  # 6

Strings are immutable (cannot be changed once created).

b) list

An ordered, mutable collection of items.

fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"
print(fruits)  # ['apple', 'mango', 'cherry']

c) tuple

Similar to a list, but immutable.

numbers = (10, 20, 30)
print(numbers[0])  # 10

d) range

Represents a sequence of numbers, often used in loops.

for i in range(5):
    print(i)

3. Mapping Type

dict (Dictionary)

Stores data in key-value pairs.

student = {"name": "Rafsan", "age": 20, "dept": "CSE"}
print(student["name"])  # Rafsan

4. Set Types

a) set

An unordered collection of unique values.

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

b) frozenset

An immutable version of a set.

fset = frozenset([1, 2, 3])

5. Boolean Type

bool

Represents logical values: True or False.

x = 10
y = 5
print(x > y)  # True

6. Binary Types

bytes, bytearray, memoryview

Used to work with binary data like files, images, or network streams.

data = b"Hello"
print(type(data))  # <class 'bytes'>

⚙️ Type Conversion (Type Casting)

You can convert one data type into another in Python.

x = 10
y = float(x)   # int → float
z = str(x)     # int → string
print(y, z)

🧠 Checking Data Type

The type() function is used to check the type of a variable.

name = "Rafsan"
print(type(name))

📚 Summary Table

CategoryData TypeMutableExample
Numericint, float, complexNo10, 3.14, 2+3j
Sequencestr, list, tuple, rangestr/list: No/Yes"Hi", [1,2], (1,2)
MappingdictYes{"key": "value"}
Setset, frozensetYes/No{1,2,3}, frozenset({1,2})
BooleanboolNoTrue, False
Binarybytes, bytearray, memoryviewNo/Yesb"data"

🏁 Conclusion

Data types are the foundation of Python programming. Understanding them is like mastering half of the language. Each type has its own properties and use cases. Choosing the right data type makes your code cleaner, faster, and less error-prone.