Q1Which of these best describes the point of "thinking about types" in Python?
Python Types Roundup — Why Types Matter and the Door to Classes
Recap Python's basic data types with diagrams, then take your first step toward OOP by defining your own type.
What exactly is a "type"?
So far you've met a lot of types — int, float, str, bool, list, dict, tuple, set, and more. Picking the right type for the job is what matters most in real-world code.
As the example shows, even the same + operator runs different operations depending on the type.
In other words, choosing a different type for a different purpose is how you change what a variable can do.
All the types you've met so far, at a glance
Let's organize the types from Python Basics by what you use them for. Whether you're dealing with "number crunching," "text," or "a group of multiple values," the right type falls out naturally.
| Type | Typical use | Example |
|---|---|---|
| int | Calculating prices or item counts | 5, 100, -3 |
| float | Tax-included amounts or discount rates | 3.14, 0.5 |
| bool | Tracking login state or stock availability | True, False |
| str | Storing usernames or comments | 'hello' |
| list | A shopping cart or an ordered list of search results | [1, 2, 3] |
| tuple | Fixed pairs like latitude/longitude | (1, 2, 3) |
| dict | Holding user info (id / name / email) | {'a': 1} |
| set | Deduplicating tags or membership checks | {1, 2, 3} |
Also keep in mind whether a type is changeable (mutable) or unchangeable (immutable). list / dict / set are mutable; everything else is immutable.
You can define your own types with class
Everything covered so far — int / list / dict and friends — is a type Python provides out of the box (a built-in type). In real projects, lots of concepts show up that built-ins can't quite represent on their own, like "User," "Product," or "Order." Python lets you define your own types with class — that's the doorway into object-oriented programming (OOP). We'll revisit OOP in depth in future articles.
# Define a new type called Point (a coordinate)
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(3, 4) # create a Point instance
print(p.x) # 3
A full OOP course is coming in another series
Classes, inheritance, polymorphism, and the rest of object-oriented programming are covered in a dedicated separate series. For now, it's enough to know that on top of the built-in types, you can also build your own.
Knowledge Check
Answer each question one by one.
Q2Which of the following is a mutable (changeable) type?
Q3Which keyword do you use to define your own new type in Python?