1class Person:
2 def __init__(self, _name, _age):
3 self.name = _name
4 self.age = _age
5
6 def sayHi(self):
7 print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
8
9p1 = Person('Bob', 25)
10p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
1class Employee(Object)
2 def __init__(self, name, age, salary):
3 self.name = name
4 self.age = age
5 self.salary = salary
6
7
8
9 def __str__(self)
10 return f"Employee {name} \nhes age {age} \nand make {salary}"
1class Student:
2 def __init__(self, id, name, age):
3 self.name = name
4 self.id = id
5 self.age = age
6
7 def greet(self):
8 print(f"Hello there.\nMy name is {self.name}")
9
10 def get_age(self):
11 print(f"I am {self.age}")
12
13 def __add__(self, other)
14 return Student(
15 self.name+" "+other.name,
16 self.id + " "+ other.id,
17 str(self.age) +" "+str(other.age))
18
19p1 = Student(1, "Jay", 19)
20p2 = Student(2, "Jean", 22)
21p3 = Student(3, "Shanna", 32)
22p4 = Student(4, "Kayla", 23)
23
24
25result = p1+p3
1# Python classes
2
3class Person():
4 # Class object attributes (attributes that not needed to be mentioned when creating new class of person)
5 alive = True
6
7 def __init__(self, name, age):
8 # In the __init__ method you can make attributes that will be mentioned when creating new class of person
9 self.name = name
10 self.age = age
11
12 def speak(self):
13 # In every method in class there will be self, and then other things (name, age, etc.)
14 print(f'Hello, my name is {self.name} and my age is {self.age}') # f'' is type of strings that let you use variable within the string
15
16person_one = Person('Sam', 23) # Sam is the name attribute, and 23 is the age attribute
17person_one.speak() # Prints Hello, my name is Sam and my age is 23
18
19==================================================================
20# Output:
21
22>>> 'Hello, my name is Sam and my age is 23'
1class IntellipaatClass:
2 a = 5
3 def function1(self):
4 print(‘Welcome to Intellipaat’)
5#accessing attributes using the class object of same name
6IntellipaatClass.function(1)
7print(IntellipaatClass.a)
8