make new class python code example
Example 1: how to make a class in python
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()
Example 2: classes in python
class Person():
alive = True
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print(f'Hello, my name is {self.name} and my age is {self.age}')
person_one = Person('Sam', 23)
person_one.speak()
==================================================================
>>> 'Hello, my name is Sam and my age is 23'