انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

class: make a new type

Section 12: Classes and objects (class)Lesson 12.1 · ⏱ 12 min

Until now we made separate variables and functions. A class puts them together: the thing's data (its properties) and what it does (its functions). From one class we make many objects, just as we make many robots from one design.

  • __init__ runs once when the object is made, and gives it its properties.
  • self means “this object itself”: self.battery is this robot's battery.
  • r1 = Robot("Robo", 50) makes a new object.
  • r1.move(20) asks r1 to move, and r2 is not affected.
class Robot:
    def __init__(self, name, battery):
        self.name = name
        self.battery = battery

    def move(self, steps):
        self.battery -= steps
        print(self.name, "moved", steps, "steps. Battery:", self.battery)

    def charge(self):
        self.battery = 100
        print(self.name, "is charged!")

r1 = Robot("Robo", 50)
r2 = Robot("Bibo", 80)
r1.move(20)
r2.move(5)
r1.charge()
print(r1.battery, r2.battery)
🖥️ The output in Robo Python:
Robo moved 20 steps. Battery: 30
Bibo moved 5 steps. Battery: 75
Robo is charged!
100 75
🐍 Open the code in Robo Python ↗
Two robots from one class, each with its own battery
Two robots from one class, each with its own battery

📌 Another example: the circle class

class Circle keeps the radius, and works out the area.

🐍 The circle class
class Circle:
    def __init__(self, r):
        self.r = r

    def area(self):
        return round(3.14 * self.r * self.r, 2)

c = Circle(5)
print(c.area())
🖥️ The output in Robo Python:
78.5
🐍 Open the code in Robo Python ↗
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

Test yourself

1. After r1.move(20), did r2's battery change?
  1. Add a function say(self, text) that prints the robot's name, then the text.
  2. Make move print “Battery empty!” and not move if the battery is less than the steps.

🎯 Challenge yourself

Easy

A Dog class with a name, and a bark function that prints: Rex says woof.

👀 Show the solution
🐍 Solution
class Dog:
    def __init__(self, name):
        self.name = name

    def bark(self):
        print(self.name, "says woof")

Dog("Rex").bark()
🖥️ The output in Robo Python:
Rex says woof
🐍 Open the code in Robo Python ↗

Result: Rex says woof

Medium

A Rectangle class with area and perimeter.

👀 Show the solution
🐍 Solution
class Rectangle:
    def __init__(self, w, h):
        self.w = w
        self.h = h

    def area(self):
        return self.w * self.h

    def perimeter(self):
        return 2 * (self.w + self.h)

r = Rectangle(4, 6)
print(r.area(), r.perimeter())
🖥️ The output in Robo Python:
24 20
🐍 Open the code in Robo Python ↗

Result: 24 20

Hard

A BankAccount class: deposit and withdraw, and it doesn't withdraw more than it has.

👀 Show the solution
🐍 Solution
class BankAccount:
    def __init__(self):
        self.money = 0

    def deposit(self, n):
        self.money = self.money + n

    def withdraw(self, n):
        if n > self.money:
            print("not enough money")
        else:
            self.money = self.money - n

a = BankAccount()
a.deposit(100)
a.withdraw(30)
a.withdraw(500)
print(a.money)
🖥️ The output in Robo Python:
not enough money
70
🐍 Open the code in Robo Python ↗

Result: not enough money, then 70

Finished the lesson? Mark it so your progress counts.