Data Types Overview
The different kinds of data Python understands. Think of data types like containers — each holds a different kind of information.
"Data types are the vocabulary of programming. Once you understand them, you can speak fluently to the computer."
— ShurAIWhat Are Data Types?
A data type is a category of data that tells Python what kind of information is being stored and what operations can be performed on it. Think of data types like different containers — a box for books, a jar for cookies, a bottle for water. Each container is designed for a specific purpose.
Python has several built-in data types. Understanding them is the foundation of all Python programming. Let's explore them one by one.
1️⃣ Text Type — String (str)
A string is used for storing text and words. Strings are created by enclosing text in single quotes, double quotes, or triple quotes.
name = "Alice"
message = 'Hello, World!'
poem = """This is a
multiline string
across three lines"""
print(name) # Output: Alice
print(message) # Output: Hello, World!
Strings are immutable — once created, they cannot be changed. They are like text carved in stone.
2️⃣ Numeric Types — Integers and Floats
Python has two main numeric types for storing numbers: integers and floats.
Integers (int) — Whole Numbers
Integers are whole numbers without decimal points. They can be positive, negative, or zero, and Python allows them to be as large as you need.
age = 25
score = -10
count = 0
big_number = 999999999999999
print(age) # Output: 25
print(type(age)) # Output: <class 'int'>
Floats (float) — Decimal Numbers
Floats are numbers with decimal points. Use them when you need precision beyond whole numbers.
height = 5.9
price = 19.99
temperature = -0.5
print(height) # Output: 5.9
print(type(price)) # Output: <class 'float'>
Complex Numbers (complex) — Advanced
Complex numbers have a real part and an imaginary part. They're used in advanced mathematics and scientific computing. For beginners, you can skip this for now.
z = 2 + 3j
print(z) # Output: (2+3j)
print(type(z)) # Output: <class 'complex'>
3️⃣ Sequence Types — Lists, Tuples, and Ranges
Sequence types are collections that store multiple items in a specific order. Let's explore each one.
Lists (list) — Ordered and Changeable
A list is an ordered collection of items that CAN be changed. Lists are enclosed in square brackets, and items are separated by commas.
fruits = ["apple", "banana", "orange"]
grades = [95, 87, 92]
mixed = [1, "hello", 3.14] # can mix types
print(fruits) # Output: ['apple', 'banana', 'orange']
print(fruits[0]) # Output: apple (first item)
print(type(fruits)) # Output: <class 'list'>
Lists are mutable, meaning you can add, remove, or change items after the list is created.
Tuples (tuple) — Ordered and Unchangeable
A tuple is like a list, but CANNOT be changed after creation. Tuples are enclosed in parentheses and are useful when you want data to stay constant.
coordinates = (10, 20)
rgb_color = (255, 0, 0) # red color
single_item = (42,) # note the comma for single item
print(coordinates) # Output: (10, 20)
print(coordinates[0]) # Output: 10
print(type(coordinates)) # Output: <class 'tuple'>
Tuples cannot be changed after creation. If you try to modify a tuple, Python will give an error.
Range (range) — Sequence of Numbers
A range is a sequence of numbers, commonly used with loops. It's created using the range() function.
numbers = range(5) # generates 0, 1, 2, 3, 4
for i in range(3):
print(i)
# Output:
# 0
# 1
# 2
4️⃣ Mapping Type — Dictionary (dict)
A dictionary stores data as key-value pairs. It's like a real dictionary where you look up a word and get its definition. Dictionaries are enclosed in curly braces.
person = {
"name": "Bob",
"age": 30,
"city": "NYC",
"is_student": False
}
print(person) # Output: all key-value pairs
print(person["name"]) # Output: Bob
print(person["age"]) # Output: 30
print(type(person)) # Output: <class 'dict'>
Dictionaries are mutable. You can add, remove, or change key-value pairs after creation.
5️⃣ Set Types — Sets and Frozensets
Sets are unordered collections of unique items. If you add duplicate items, they are automatically removed.
Sets (set) — Unordered and Unique
colors = {"red", "green", "blue"}
numbers = {1, 2, 2, 3} # duplicate 2 removed
print(colors) # Output: {'red', 'green', 'blue'} (order may vary)
print(numbers) # Output: {1, 2, 3}
print(type(numbers)) # Output: <class 'set'>
Frozensets (frozenset) — Immutable Sets
Frozensets are like sets, but immutable — they cannot be changed after creation.
frozen = frozenset([1, 2, 3])
print(frozen) # Output: frozenset({1, 2, 3})
print(type(frozen)) # Output: <class 'frozenset'>
6️⃣ Boolean Type — True or False
A boolean represents one of two values: True or False. Booleans are used extensively in decision-making and comparisons.
is_student = True
is_raining = False
print(is_student) # Output: True
print(type(is_student)) # Output: <class 'bool'>
# Booleans from comparisons
result = 5 > 3 # Evaluates to True
print(result) # Output: True
7️⃣ Special Type — None
The None type represents the absence of a value. It's used when a variable has no value or when a function returns nothing.
result = None
print(result) # Output: None
print(type(result)) # Output: <class 'NoneType'>
8️⃣ Binary Types — Bytes and Bytearrays
Binary types are used for working with raw byte data. They are less common for beginners but important for advanced work.
data = b"Hello" # bytes
arr = bytearray(b"Hello") # bytearray
print(data) # Output: b'Hello'
print(type(data)) # Output: <class 'bytes'>
print(type(arr)) # Output: <class 'bytearray'>
Mutable vs. Immutable — Important Concept
Mutable means the data can be changed after creation. Immutable means it cannot be changed.
list, dict, set, bytearraystr, tuple, int, float, frozenset, bytesType Conversion — Converting Between Types
Sometimes you need to convert one data type to another. Python provides functions to do this.
# Convert string to integer
age_str = "25"
age_int = int(age_str) # becomes 25
print(age_int) # Output: 25
# Convert integer to string
number = 42
text = str(number) # becomes "42"
print(text) # Output: 42
# Convert to float
price = float("19.99")
print(price) # Output: 19.99
Quick Reference — Data Types Summary Table
| Type | Name | Example | Mutable? |
|---|---|---|---|
| Text | str |
"hello" |
No |
| Number | int |
25 |
No |
| Number | float |
3.14 |
No |
| Collection | list |
[1, 2, 3] |
Yes |
| Collection | tuple |
(1, 2, 3) |
No |
| Collection | dict |
{"key": "value"} |
Yes |
| Collection | set |
{1, 2, 3} |
Yes |
| Boolean | bool |
True |
No |
| Special | None |
None |
No |
"Master the data types, and you master Python. They are the building blocks of every program you will ever write."
— ShurAI🧠 Quick Check — Question 1
What is the correct data type for storing "Hello, Python"?
🧠 Quick Check — Question 2
Which of the following is a mutable data type?
🧠 Quick Check — Question 3
What will type([1, 2, 3]) return?
🧠 Quick Check — Question 4
Which data type is used to store key-value pairs?
🧠 Quick Check — Question 5
What does range(3) generate?