DEV Community

Ridwan Ahmed Arman
Ridwan Ahmed Arman

Posted on • Edited on

Revise Python in 15 days for ML (2025): Day 2 (Data Types)

Hey everyone! Welcome back to my 14-Day Python Revision Challenge for Machine Learning! šŸŽ‰ I’m on Day 2 today, and I’m super excited to share what I’ve learned. We’re diving into Data Types in Python—specifically numeric data, sequence data (like lists, tuples, ranges, arrays), sets, and dictionaries. I’ll break it all down in a simple, friendly way with examples, so it’s easy to follow. Let’s get started! 😊

Day 1 is here Day 1

Day 2: Python Data Types - The Building Blocks 🧱

Data types are like the different kinds of containers you use to store stuff in Python. Today, I explored a bunch of them, and I’ll explain each one step by step with examples.

1. Numeric Data - Numbers in Python šŸ”¢

Python handles different kinds of numbers: integers (whole numbers), floats (decimals), and complex numbers.

Integers, Floats, and Complex Numbers:

x = 10 # Integer (whole number) y = 10.8 # Float (decimal number) z = 10 + 2j # Complex number (has real and imaginary parts) print(type(x)) # Output: <class 'int'> print(type(y)) # Output: <class 'float'> print(type(z)) # Output: <class 'complex'> 
Enter fullscreen mode Exit fullscreen mode
  • type() tells you what kind of data you’re working with.
  • isinstance(y, int) checks if y is an integer. Here, it’s False because y is a float.

Converting Between Types

  • Trying to convert a complex number to an integer doesn’t work
z = int(z) # Error: You can’t convert a complex number to an integer 
Enter fullscreen mode Exit fullscreen mode

Working with Complex Numbers:

n = complex(input("Enter number: ")) # Input: 10 print(type(n)) # Output: <class 'complex'> print(n) # Output: (10+0j) print(n.real) # Output: 10.0 (real part) print(n.imag) # Output: 0.0 (imaginary part) 
Enter fullscreen mode Exit fullscreen mode
  • You can create a complex number from user input using complex().
  • n.real and n.imag let you access the real and imaginary parts.

2. Sequence Data - Ordered Collections šŸ“‹

Sequences are data types that store items in a specific order. Let’s look at lists, tuples, ranges, and arrays.

Lists

L = [1, 2, 3, 4, 5] # Lists use square brackets [] print(L) # Output: [1, 2, 3, 4, 5] print(L[1]) # Output: 2 (index 1) print(L[-1]) # Output: 5 (last item) 
Enter fullscreen mode Exit fullscreen mode

Lists are like a shopping list—you can change them anytime.
Access items using their index (position), starting from 0. Negative indices count from the end.

Tuples

T = (1, 2, 3, 4, 5) # Tuples use parentheses () print(T) # Output: (1, 2, 3, 4, 5) print(T[1]) # Output: 2 (index 1) print(T[-1]) # Output: 5 (last item) print(T[1:4]) # Output: (2, 3, 4) (slicing from index 1 to 3) print(type(T))# Output: <class 'tuple'> 
Enter fullscreen mode Exit fullscreen mode
  • Tuples are like a sealed envelope—once created, you can’t change them.
  • You can access items the same way as lists, including slicing (like T[1:4]).

Ranges:

x = range(5, 12, 2) # Start at 5, go up to (but not including) 12, step by 2 print(x) # Output: range(5, 12, 2) for i in x: print(i) # Output: 5, 7, 9, 11 (one per line) print(list(x)) # Output: [5, 7, 9, 11] 
Enter fullscreen mode Exit fullscreen mode
  • range(start, stop, step) generates a sequence of numbers.
  • You can loop over it with a for loop or convert it to a list

Arrays:

import array as ar a = ar.array('i', [1, 2, 3, 4, 5]) # 'i' means integer type print(a) # Output: array('i', [1, 2, 3, 4, 5]) b = ar.array('f', [1.5, 2, 3, 4, 5]) # 'f' means float type print(b) # Output: array('f', [1.5, 2.0, 3.0, 4.0, 5.0]) 
Enter fullscreen mode Exit fullscreen mode
  • Arrays are like lists but more strict—you need to specify the data type ('i' for integers, 'f' for floats).
  • They’re not used as often as lists, but good to know for ML when working with numerical data.

3. Sets - Unique Collections šŸ—³ļø

Sets store unique items—no duplicates allowed.

s = {1, 2, 3, 4, 5} print(s) # Output: {1, 2, 3, 4, 5} 
Enter fullscreen mode Exit fullscreen mode
  • Sets use curly braces {}.
  • If you try to add duplicates (like s = {1, 1, 2}), it’ll only keep one copy: {1, 2}.

4. Dictionaries - Key-Value Pairs šŸ“–

Dictionaries store data as key-value pairs, like a phonebook.

d = {"name": "ridwan", "age": 20} print(d) # Output: {'name': 'ridwan', 'age': 20} print(d["name"]) # Output: ridwan print(d.get("name")) # Output: ridwan print(d.keys()) # Output: dict_keys(['name', 'age']) print(d.values()) # Output: dict_values(['ridwan', 20]) print(d.items()) # Output: dict_items([('name', 'ridwan'), ('age', 20)]) 
Enter fullscreen mode Exit fullscreen mode
  • Dictionaries use curly braces {} with key: value pairs.
  • Access values using the key (d["name"]) or with d.get("name").
  • keys(), values(), and items() give you the keys, values, or both as pairs.

Quick Comparison: Lists, Tuples, and Sets šŸ¤”

Here’s a simple breakdown of the differences between lists, tuples, and sets:

  • Lists: Ordered, changeable, allows duplicates. Example: [1, 2, 2, 3].
  • Tuples: Ordered, unchangeable, allows duplicates. Example: (1, 2, 2, 3).
  • Sets: Unordered, changeable, no duplicates. Example: {1, 2, 3} (no repeats).

I’ll dive deeper into these three topics on another day, so stay tuned for more on that! šŸ˜„

Day 2 Wrap-Up 🌟

Today was all about understanding Python’s data types:

  • Numeric Data: Integers, floats, and complex numbers—great for math in ML.
  • Sequence Data: Lists, tuples, ranges, and arrays for ordered collections.
  • Sets: Perfect for unique items.
  • Dictionaries: Handy for key-value data like a mini-database.

Day 1 is here Day 1

I’m feeling more confident already! Tomorrow, I’ll probably tackle some control flow stuff like loops and conditionals. If you’re following along, try playing with these data types in your own Python editor—it really helps to experiment! Let me know how you’re doing in the comments. See you for Day 3! šŸš€

Top comments (0)