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'>
- 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
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)
- 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)
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'>
- 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]
- 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])
- 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}
- 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)])
- 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)