Data types define the kind of value a variable can store. Python has several built-in data types categorized as:
✅ Numeric Types: int, float, complex
✅ Text Type: str (string)
✅ Sequence Types: list, tuple, range
✅ Set Types: set, frozenset
✅ Mapping Type: dict (dictionary)
✅ Boolean Type: bool
✅ Binary Types: bytes, bytearray, memoryview
1. Numeric Data Types
1.1 Integer (int)
Used for whole numbers (positive, negative, or zero).
age = 25 year = 2024 balance = -1000 print(type(age)) # Output: <class 'int'>
1.2 Float (float)
Used for decimal numbers.
price = 49.99 pi = 3.14159 print(type(price)) # Output: <class 'float'>
1.3 Complex (complex)
Used for complex numbers (a + bj).
z = 3 + 4j print(type(z)) # Output: <class 'complex'>
2. String Data Type (str)
A string is a collection of characters enclosed in single (') or double (") quotes.
name = "Alice" greeting = 'Hello, World!' print(type(name)) # Output: <class 'str'>
Multiline Strings
Use triple quotes (""" """ or ''' ''') for multiline strings.
message = """Hello, This is a multiline string."""
String Operations
text = "Python" print(text.upper()) # Convert to uppercase print(text.lower()) # Convert to lowercase print(len(text)) # Get string length print(text[0]) # Access first character (P)
3. Boolean Data Type (bool)
Boolean values are True or False (case-sensitive).
is_active = True is_closed = False print(type(is_active)) # Output: <class 'bool'>
4. List (list) – Ordered, Mutable Collection
A list is an ordered collection that allows modifications.
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Add item
fruits.remove("banana") # Remove item
print(fruits[0]) # Access first item
5. Tuple (tuple) – Ordered, Immutable Collection
A tuple is similar to a list but cannot be changed after creation.
coordinates = (10, 20, 30) print(coordinates[1]) # Output: 20 # coordinates[0] = 50 # ❌ Error: Tuples are immutable
6. Dictionary (dict) – Key-Value Pairs
A dictionary stores data in key-value pairs.
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
print(person["name"]) # Output: Alice
person["age"] = 26 # Update value
7. Set (set) – Unordered, Unique Items
A set contains unique, unordered items.
numbers = {1, 2, 3, 4, 4} # Duplicate 4 is ignored
numbers.add(5)
numbers.remove(3)
print(numbers)
Checking Data Type with type()
x = 10 print(type(x)) # Output: <class 'int'> y = "Hello" print(type(y)) # Output: <class 'str'>