xxxxxxxxxx
class employee:
name = ''
id = 0
department = ''
def pay(self):
print(self.name, self.id, self.department)
s = employee()
s.name = 'burhan'
s.id = 0o2234523
s.department = 'Development'
s.pay()
b = employee()
b.name = 'Abdullah'
b.id =12423432857
b.department = 'Software Engineer'
b.pay()
c = employee()
c.name = 'Bilal'
c.id =12423432857
c.department = 'Software Engineer'
c.pay()
xxxxxxxxxx
class Mammal:
def __init__(self, name):
self.name = name
def walk(self):
print(self.name + " is going for a walk")
class Dog(Mammal):
def bark(self):
print("bark!")
class Cat(Mammal):
def meow(self):
print("meow!")
dog1 = Dog("Spot")
dog1.walk()
dog1.bark()
cat1 = Cat("Juniper")
cat1.walk()
cat1.meow()
xxxxxxxxxx
# plz suscribe to my youtube channel -->
# https://www.youtube.com/channel/UC-sfqidn2fKZslHWnm5qe-A
class Fan:
def __init__(self, company, color, number_of_wings):
self.company = company
self.color = color
self.number_of_wings = number_of_wings
def PrintDetails(self):
print('This is the brand of',self.company,'its color is', self.color,' and it has',self.number_of_wings,'petals')
def switch_on(self):
print("fan started")
def switch_off(self):
print("fan stopped")
def speed_up(self):
print("speed increased by 1 unit")
def speed_down(self):
print("speed decreased by 1 unit")
usha_fan = Fan('usha','skin',5)
fan = Fan('bajaj','wite', 4)
print('these are the details of this fan')
usha_fan.PrintDetails()
print()
usha_fan.switch_on()
xxxxxxxxxx
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
xxxxxxxxxx
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
xxxxxxxxxx
class awwab(object):
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print("Hello, my name is",self.name,"and I am",self.age,"years old!")
awwabasad = awwab("Awwab Asad", 11)
print(awwabasad.speak())
xxxxxxxxxx
class IntellipaatClass:
a = 5
def function1(self):
print(‘Welcome to Intellipaat’)
#accessing attributes using the class object of same name
IntellipaatClass.function(1)
print(IntellipaatClass.a)
xxxxxxxxxx
class LuckyLemur():
def __init__(self, description):
self.description = description
def reveal_description(self):
print(f'Lucky Lemur is {self.description}')
lucky_lemur = LuckyLemur('Pro')
lucky_lemur.reveal_description()
xxxxxxxxxx
class Foo:
def __init__(self):
self.definition = Foo!
def hi():
# Some other code here :)
# Classes require an __init__ if you want to assign attributes. (self) defines what describes the attribs.
xxxxxxxxxx
class Greet:
def __init__(self, names):
self.names = names
def say_hello(self):
for name in self.names:
print("Hello " + name)
xxxxxxxxxx
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def greet(self, person_to_greet):
# person_to_greet will be another Person object
print(f"Hey {person_to_greet.name}, nice to meet you I'm {self.name}")
def ask_age(self, ask_from):
print(f"{self.name}: {ask_from.name}, How old are you?")
print(f"{ask_from.name}: i am {ask_from.age}")
# Creating a person object
tom = Person("Tom", 50, "Male")
# we can also create an object with keyword arguments
jack = Person(name="Jack", age=19, gender="Male")
# Here we call the greet method of tom, and we pass the Jack Person Object Created above
tom.greet(jack)
# if we call the greet method of jack and pass the Tom person object, then jack greets tom
jack.greet(tom)
# Here Jack will ask age of tom, and tom will reply with his age
jack.ask_age(tom)