DEV Community

M.T.Ramkrushna
M.T.Ramkrushna

Posted on

PYTHON ESSENTIALS FOR AI/ML (Data Types)

πŸš€ 1. Basic Data Types

πŸ”Ή int β†’ Whole numbers

age = 25
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Real-life analogy

Switch ON/OFF.

ML use-case

Checking if model is in training or evaluation mode.


πŸ”Ή str β†’ Text

name = "Ali"
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

βœ” 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)
Enter fullscreen mode Exit fullscreen mode

βœ” ML use-case

Storing multiple predictions:

preds = [0.1, 0.4, 0.8, 0.3]
Enter fullscreen mode Exit fullscreen mode

πŸš€ 3. Tuple β†’ Ordered, NOT changeable (immutable), allows duplicates

point = (3, 5)
Enter fullscreen mode Exit fullscreen mode

βœ” 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)
Enter fullscreen mode Exit fullscreen mode

βœ” Why use tuple?

  • Faster than list
  • Safe from accidental modification

πŸš€ 4. Set β†’ Unordered, unique elements only, no duplicates

unique_ids = {101, 102, 103}
Enter fullscreen mode Exit fullscreen mode

βœ” 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"])
Enter fullscreen mode Exit fullscreen mode

Output:

{'cat', 'dog', 'mouse'}
Enter fullscreen mode Exit fullscreen mode

πŸš€ 5. Dictionary (dict) β†’ Key–value pairs

student = {"name": "Ali", "age": 22}
Enter fullscreen mode Exit fullscreen mode

βœ” 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"
Enter fullscreen mode Exit fullscreen mode

βœ” ML use-case

Hyperparameters:

config = {
    "learning_rate": 0.001,
    "batch_size": 32,
    "optimizer": "adam"
}
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ 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]
Enter fullscreen mode Exit fullscreen mode

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)