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.

The type decides "what you can do"
5 + 3(int)8(addition)'hi' + '!'(str)'hi!'(concatenation)++

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.

TypeTypical useExample
intCalculating prices or item counts5, 100, -3
floatTax-included amounts or discount rates3.14, 0.5
boolTracking login state or stock availabilityTrue, False
strStoring usernames or comments'hello'
listA shopping cart or an ordered list of search results[1, 2, 3]
tupleFixed pairs like latitude/longitude(1, 2, 3)
dictHolding user info (id / name / email){'a': 1}
setDeduplicating 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.

Classes and instances
class Point(blueprint)Point(3, 4)(call)p(instance)usecreate
# 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.

QUIZ

Knowledge Check

Answer each question one by one.

Q1Which of these best describes the point of "thinking about types" in Python?

Q2Which of the following is a mutable (changeable) type?

Q3Which keyword do you use to define your own new type in Python?