class Person: def __init__(self, _name, _age): self.name = _name self.age = _age def sayHi(self): print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!') p1 = Person('Bob', 25) p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
Here is what the above code is Doing:
1. We create a class named Person.
2. We define the __init__() method. It takes two parameters: self and _name.
3. We create two instance variables: name and age.
4. We create a method named sayHi(). It takes one parameter: self.
5. We create an object named p1, which is an instance of Person.
6. We call the sayHi() method.