Index
A data type in Python defines the type of value a variable can store. Python is dynamically typed, meaning you don’t need to specify the data type explicitly—it is determined automatically.
1. Numeric Data Types
Python supports three types of numeric values:
A. Integer (int)
- Stores whole numbers (positive, negative, or zero).
- No decimal point.
Example:
x = 10 y = -5 z = 0 print(type(x)) # Output: <class 'int'>
B. Floating-Point (float)
- Stores numbers with decimal points.
Example:
pi = 3.14 temperature = -10.5 print(type(pi)) # Output: <class 'float'>
C. Complex (complex)
- Used for complex numbers (
a + bjformat).
Example:
num = 2 + 3j print(type(num)) # Output: <class 'complex'>
2. Boolean (bool)
- Represents
TrueorFalsevalues. - Used in conditions and comparisons.
Example:
is_raining = True is_sunny = False print(type(is_raining)) # Output: <class 'bool'>
Booleans in conditions:
age = 18 print(age >= 18) # Output: True
3. Sequence Data Types
A. String (str)
- A sequence of characters enclosed in single (
') or double (") quotes.
Example:
name = "John" message = 'Hello, Python!' print(type(name)) # Output: <class 'str'>
String operations:
text = "Python" print(text.upper()) # Output: PYTHON print(text.lower()) # Output: python print(text[0]) # Output: P (First character) print(text[-1]) # Output: n (Last character)
B. List (list)
- Ordered, mutable (changeable), and can store multiple types of data.
Example:
fruits = ["apple", "banana", "cherry"] print(type(fruits)) # Output: <class 'list'>
List operations:
fruits.append("orange") # Add item
fruits[1] = "grape" # Modify item
print(fruits) # Output: ['apple', 'grape', 'cherry', 'orange']
C. Tuple (tuple)
- Ordered, immutable (cannot be changed after creation).
Example:
coordinates = (10, 20, 30) print(type(coordinates)) # Output: <class 'tuple'>
Tuples are immutable:
# coordinates[0] = 50 # ❌ This will cause an error
D. Range (range)
- Represents a sequence of numbers.
- Used in loops.
Example:
numbers = range(5) # Generates numbers from 0 to 4 print(list(numbers)) # Output: [0, 1, 2, 3, 4]
4. Dictionary (dict) – Key-Value Pairs
- Unordered collection of key-value pairs.
Example:
student = {"name": "Alice", "age": 20, "grade": "A"}
print(type(student)) # Output: <class 'dict'>
print(student["name"]) # Output: Alice
Modifying a dictionary:
student["age"] = 21 # Update value student["city"] = "New York" # Add new key-value pair
5. Set Data Types
A. Set (set)
- Unordered collection of unique elements.
Example:
colors = {"red", "blue", "green"}
print(type(colors)) # Output: <class 'set'>
Set operations:
colors.add("yellow") # Add an element
colors.remove("blue") # Remove an element
print(colors)
B. Frozen Set (frozenset)
- Immutable version of a set.
Example:
frozen_colors = frozenset(["red", "blue", "green"])
# frozen_colors.add("yellow") # ❌ This will cause an error
6. Type Conversion (Type Casting)
You can convert between data types using built-in functions.
Example:
# Convert integer to float a = 10 b = float(a) # b = 10.0 # Convert string to integer num_str = "100" num = int(num_str) # num = 100 # Convert list to tuple fruits = ["apple", "banana"] fruit_tuple = tuple(fruits)
7. Summary Table
| Data Type | Example |
|---|---|
Integer (int) | x = 10 |
Float (float) | pi = 3.14 |
Complex (complex) | num = 2 + 3j |
Boolean (bool) | is_raining = True |
String (str) | name = "Alice" |
List (list) | fruits = ["apple", "banana"] |
Tuple (tuple) | coordinates = (10, 20, 30) |
Range (range) | range(5) |
Dictionary (dict) | student = {"name": "Alice", "age": 20} |
Set (set) | colors = {"red", "blue"} |
Frozen Set (frozenset) | frozen_colors = frozenset(["red", "blue"]) |