Data Types

Nerd Cafe | نرد کافه

Introduction

In Python, everything is an object, and every object has a data type. A data type defines:

  • What kind of value it stores.

  • What operations can be performed on it.

Example:

  • A str stores text.

  • An int stores whole numbers.

  • A float stores decimals.

  • A list stores collections of items.

Understanding data types is critical in machine learning because ML models expect specific data formats — usually numbers for training, strings for labels, or structured collections like lists/arrays.

1. Numeric Types

int (Integer)

Whole numbers (positive, negative, or zero).

a = 10
b = -3
c = 0
print(type(a))  # <class 'int'>

float (Floating Point)

Decimal numbers.

complex (Complex Numbers)

Numbers with real and imaginary parts.

ML Note:

  • Use int and float for training data and features.

  • complex is rarely used in ML, except in advanced fields like signal processing.

2. Text Type

str (String)

Stores text data.

ML Use Cases:

  • Labels ("cat", "dog")

  • File paths ("dataset/images/cat.jpg")

  • Column names in pandas

⚠️ ML models cannot directly use strings. Convert them with Label Encoding or One-Hot Encoding.

3. Sequence Types

list

Ordered, changeable, allows duplicates.

ML Use Case:

Store a row of features before converting to NumPy arrays or pandas DataFrames.

tuple

Ordered, immutable (unchangeable).

ML Use Case:

Represent fixed data such as image shape:

range

Generates a sequence of numbers.

ML Use Case:

Iterating over epochs, batches, or samples.

4. Mapping Type

dict (Dictionary)

Stores key-value pairs.

ML Use Cases:

  • Model configurations:

Label mapping:

5. Boolean Type

bool

Represents True or False.

ML Use Case:

Used in conditions (e.g., stopping training if accuracy reaches a threshold).

6. Binary Types

Used for handling raw binary data (e.g., images, serialized models).

  • bytes → immutable

  • bytearray → mutable

  • memoryview → view of binary data

7. Type Checking

ML Tip:

Validate data types before feeding them into ML models.

8. Type Casting (Conversion)

ML Example:

When reading CSV files, numbers are often loaded as strings. Convert them to int or float.

9. Summary Table

Data Type
Example
ML Usage

int

5

ID, count, label

float

3.14

Feature value, weight

str

"cat"

Label, file path, text

bool

True

Training flag, condition

list

[1, 2]

Features, dataset samples

tuple

(224, 224, 3)

Image shape, fixed data

dict

{"lr": 0.01}

Configurations, label mappings

10. Video Tutorial

Support Our Work

If you find this post helpful and would like to support my work, you can send a donation via TRC-20 (USDT). Your contributions help us keep creating and sharing more valuable content.

Thank you for your generosity! 🙏

Keywords

int, float, complex, str, list, tuple, range, dict, set, bool, bytes, bytearray, memoryview, type, isinstance, type casting, data conversion, immutable, mutable, nerd cafe , نرد کافه

Channel Overview

🌐 Website: www.nerd-cafe.ir

📺 YouTube: @nerd-cafe

🎥 Aparat: nerd_cafe

📌 Pinterest: nerd_cafe

📱 Telegram: @nerd_cafe

📝 Blog: Nerd Café on Virgool

💻 GitHub: nerd-cafe

Last updated