Learn Python in 30 Days — Day 24: Intro to Classes & Objects

Day 24: Intro to Classes & Objects

Welcome to Day 24 of the Learn Python in 30 Days series!

Today we’re not just writing small programs we’re starting to structure code properly. And classes are the tool that lets you do that cleanly.

If you’re building games, tools, apps, or anything that has “things” in it, players, enemies, items, devices, then classes help represent those things in a neat, organised way.

Let’s break it down with simple, clear examples.

All example files for this series are available on my GitHub: Learn-Python-in-30-Days

What Are Classes and Objects?

A class is a blueprint.
An object is something created from that blueprint.

For example:

  • A Player class, contains data like name, health, score

  • A Player object, a specific player, like Matty, Bob, Alice

Here’s the simplest possible class:

class Player: pass

This does nothing yet, but it shows the basic structure.

Creating a Simple Class

Let’s build a very basic class and create an object from it.

class Player: def __init__(self): print("A new player has been created!") p1 = Player()

What happens here?

  • The class has a special method called __init__().

  • This runs automatically when you create an object.

  • p1 = Player() triggers the print statement.

It’s a simple first step, but it shows how Python “builds” objects.

Try it yourself, hopefully you'll see something like that shown below: -

You can also download this example from my GitHub simpleclass.py and run it yourself.

Storing Data Using __init__()

We almost never want empty objects.
We want them to store information such as names, scores, stats, anything.

Let’s add attributes.

class Player: def __init__(self, name, health): self.name = name self.health = health

Creating objects

hero = Player("Matty", 100) villain = Player("DarkBot", 80) print(hero.name) # Matty print(villain.health) # 80

Explanation

  • self.name and self.health are now saved inside the object.

  • Each object gets its own set of values.

  • You can create as many players as you want without rewriting code.

Try it yourself, hopefully you'll see something like that shown below: -

You can also download this example from my GitHub simpledata.py and run it yourself.

Adding Methods (Functions Inside Classes)

Methods allow objects to do things.

Let’s add an action for our player.

class Player: def __init__(self, name, health): self.name = name self.health = health def take_damage(self, amount): self.health -= amount print(f"{self.name} took {amount} damage! Now at {self.health} HP.")

Using the method

hero = Player("Matty", 100) hero.take_damage(25) # Matty took 25 damage! Now at 75 HP.

Important Notes

  • Methods must include self as their first argument.

  • self is the object calling the method.

  • Without self, Python wouldn’t know which object’s data to change.

Try it yourself, hopefully you'll see something like that shown below: -

You can also download this example from my GitHub addingmethods.py and run it yourself.

A Class With Multiple Methods

Let’s build something more realistic.

class Player: def __init__(self, name): self.name = name self.score = 0 def add_score(self, amount): self.score += amount print(f"{self.name}'s score is now {self.score}") def reset_score(self): self.score = 0 print(f"{self.name}'s score has been reset.")

Using the class

p1 = Player("Alice") p2 = Player("Bob") p1.add_score(10) # Alice's score is now 10 p2.add_score(5) # Bob's score is now 5 p1.reset_score() # Alice's score has been reset.

Why this matters

Now your code is:

  • tidy

  • reusable

  • expandable

This is exactly the kind of structure you’ll use in the final project of the series (text adventure game) where you’ll build a Player and Room class — shown in your full 30-day roadmap .

Try it yourself, hopefully you'll see something like that shown below: -


You can also download this example from my GitHub multimethods.py and run it yourself.

A Full Working Example

This example uses everything we learned today.

class Player: def __init__(self, name): self.name = name self.health = 100 self.inventory = [] def take_damage(self, amount): self.health -= amount print(f"{self.name} now has {self.health} HP.") def heal(self, amount): self.health += amount print(f"{self.name} heals to {self.health} HP.") def add_item(self, item): self.inventory.append(item) print(f"{self.name} picked up {item}.")

Using it

hero = Player("Matty") hero.take_damage(30) hero.heal(10) hero.add_item("Sword") hero.add_item("Health Potion") print(hero.inventory)

Try it yourself, hopefully you'll see something like that shown below: -

You can also download this example from my GitHub example.py and run it yourself.

Next Up — Day 25 – Program Structure & Planning

Tomorrow, you’ll look into how to break big problems into smaller functions/files.

All example files for this series are available on my GitHub: Learn-Python-in-30-Days

You can see the full series here Learn Python in 30 Days series!

Hope you have enjoyed this post, thanks Matty

Comments

Popular posts from this blog

6502 - Part 2 Reset and Clock Circuit

Math Behind Logic Gates

Building a 6502 NOP Test