OOPs in Python
# OOP ----> Object Oreiented Programming
# makes program easier to manage and help in real world programming
# class
# object(instance)
# method (functions defined inside class)
string = "Adrika Bhadauria"
string2 = "Anika Singh"
string3 = str(15)
print(string.split(" "))
# class is defined using class keyword
# __init__ is a dunder method
# __intit__ is also called constructor
class Person: # Class
def __init__(self, first_name, last_name, age): # Method
# Instance Variables
self.first_name = first_name
self.last_name = last_name
self.age = age
def user_info(self): # Method
print(f"{self.first_name} {self.last_name} is {self.age} years old.")
p1 = Person("Adrika", "Bhadauria", 14) # Object
print(p1.first_name)
print(p1.last_name)
print(p1.age)
p1.user_info()
p2 = Person("Anika", "Singh", 3) # Object
print(p2.first_name)
print(p2.last_name)
print(p2.age)
p2.user_info()
Post a Comment