๐Ÿ”น 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.