πŸ”Ή 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?


Source: DEV Community.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.