Data Types
Python has several built-in data types. Understanding them is essential for effective programming.
Numeric Types:
- int: Whole numbers (42, -17, 0)
- float: Decimal numbers (3.14, -0.5)
- complex: Complex numbers (3+4j)
Text Type:
- str: Strings ("Hello", 'World')
Boolean Type:
- bool: True or False
Sequence Types:
- list: Mutable, ordered [1, 2, 3]
- tuple: Immutable, ordered (1, 2, 3)
- range: Sequence of numbers range(10)
Mapping Type:
- dict: Key-value pairs {"name": "Alice"}
Set Types:
- set: Unordered, unique {1, 2, 3}
- frozenset: Immutable set
None Type:
- None: Represents absence of value
Code Examples
Python Data Typespython
# Numeric types
integer = 42
floating = 3.14
complex_num = 3 + 4j
print(f"Integer: {integer}, type: {type(integer)}")
print(f"Float: {floating}, type: {type(floating)}")
print(f"Complex: {complex_num}, type: {type(complex_num)}")
# Strings
single = 'Hello'
double = "World"
multiline = """This is a
multiline string"""
f_string = f"Hello, {single}!"
print(f"F-string: {f_string}")
# Boolean
is_true = True
is_false = False
comparison = 5 > 3 # True
print(f"Comparison result: {comparison}")
# List (mutable)
colors = ["red", "green", "blue"]
colors.append("yellow")
print(f"List: {colors}")
# Tuple (immutable)
point = (10, 20)
print(f"Tuple: {point}")
# Dictionary
person = {"name": "Alice", "age": 30}
print(f"Dict: {person}")
print(f"Name: {person['name']}")
# Set (unique values)
unique_nums = {1, 2, 2, 3, 3, 3}
print(f"Set: {unique_nums}") # {1, 2, 3}
# None
result = None
print(f"None: {result}")Lists are mutable (can change), tuples are immutable (cannot change). Use tuples for fixed collections and lists for dynamic ones.
Quick Quiz
1. Which data type is immutable in Python?
2. What does a set do with duplicate values?
Was this lesson helpful?