🐸

The Concept

Python Basicspython-architect-17-the-concept
Reward: 100 XP
|

Welcome to the Architect's core skill: Object-Oriented Programming (OOP).

Until now, we used built-in objects (strings, lists). Now, we will create our own types of objects.

Blueprints (Classes) vs Houses (Objects)

Think of a Class as a blueprint. It's just a drawing on paper. Think of an Object as the actual house built from that blueprint.

You can build infinite houses from one blueprint!

# The Blueprint (Class)
class Cat:
  pass

# The Actual Cats (Objects)
kitty = Cat()
luna = Cat()
1
Draw the Blueprint

Define a class named Robot. Don't forget the colon :! Inside, just write pass for now (it means "do nothing").

2
Build Bot 1

Create an instance of your robot: bot1 = Robot().

3
Build Bot 2

Create another instance: bot2 = Robot(). They are two separate robots built from the same plan!

Notice how we use CapitalLetters for Class names (like Robot), but lowercase for variables (like bot1). This is a Python tradition.

Suggested Solution
Expand
Solution:
# 1. The Blueprint
class Robot:
  pass

# 2. Build the Bots
bot1 = Robot()
bot2 = Robot()

# 3. Verify
print(bot1)
print(bot2)
Advanced Tips
Want more? Click to expand

F. Everything is an Object

  • In Python, everything is an object.
  • "hello" is an object of class str.
  • 42 is an object of class int.
  • [1, 2, 3] is an object of class list.
  • Now you can make your own!
Loading...
Terminal
Terminal
Ready to run...