π 1. Basic Data Types
πΉ int β Whole numbers
age = 25
Real-life analogy
Counting cricket runs: 1, 2, 4, 6.
ML use-case
Storing epochs, batch size, etc.
πΉ float β Decimal numbers
learning_rate = 0.001
Real-life analogy
Measuring petrol in liters: 1.5 L.
ML use-case
Model metrics: accuracy = 92.57, loss = 0.024
πΉ bool β True/False
is_training = True
Real-life analogy
Switch ON/OFF.
ML use-case
Checking if model is in training or evaluation mode.
πΉ str β Text
name = "Ali"
Real-life analogy
Your WhatsApp message.
ML use-case
Text datasets for NLP models.
π 2. List β Ordered, changeable (mutable), allows duplicates
fruits = ["apple", "banana", "mango"]
β Properties
- Ordered
- Mutable (you can change elements)
- Allows duplicates
β Real-life analogy
A shopping list β you can rearrange, add, remove items.
β Operations
fruits.append("orange")
fruits[1] = "grapes"
print(fruits)
β ML use-case
Storing multiple predictions:
preds = [0.1, 0.4, 0.8, 0.3]
π 3. Tuple β Ordered, NOT changeable (immutable), allows duplicates
point = (3, 5)
β Properties
- Ordered
- Immutable
- Fast
- Allows duplicates
β Real-life analogy
Your Aadhaar details β once fixed, cannot modify easily.
β ML use-case
Coordinates in feature space (x, y), image pixel shape:
shape = (224, 224, 3)
β Why use tuple?
- Faster than list
- Safe from accidental modification
π 4. Set β Unordered, unique elements only, no duplicates
unique_ids = {101, 102, 103}
β Properties
- Unordered
- Only unique items
- Super fast for membership test (
in)
β Real-life analogy
Your Instagram followers list automatically removes duplicates.
β ML use-case
Getting unique labels/class names:
labels = set(["cat", "dog", "cat", "mouse"])
Output:
{'cat', 'dog', 'mouse'}
π 5. Dictionary (dict) β Keyβvalue pairs
student = {"name": "Ali", "age": 22}
β Properties
- Keyβvalue mapping
- Fast lookup
- Ordered (Python 3.7+)
β Real-life analogy
A contact list:
- "Maa" β 9876543210
- "Papa" β 1234567890
β Operations
student["age"] = 23
student["city"] = "Nashik"
β ML use-case
Hyperparameters:
config = {
"learning_rate": 0.001,
"batch_size": 32,
"optimizer": "adam"
}
π₯ Side-by-side summary table
| Type | Ordered | Mutable | Allows duplicates | Example |
|---|---|---|---|---|
| list | β | β | β | [1, 2, 3] |
| tuple | β | β | β | (1, 2, 3) |
| set | β | β | β | {1, 2, 3} |
| dict | β (keys) | β | Keys unique | {"a":1} |
π§ Mini Exercises (Try these)
Q1
Create a list of 5 movies and replace the 3rd one.
Q2
Convert this list to a set:
nums = [1, 2, 2, 3, 4, 4, 5]
Q3
Create a dictionary for a model config with keys:
- model_name
- epochs
- lr
- optimizer
Q4
Why would you use a tuple instead of a list?
Top comments (0)