xxxxxxxxxx
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_name(self):
print(f"Hello, I am {self.name}")
p1 = Person("Sara", 18)
p1.say_name()
xxxxxxxxxx
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!
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 uneclasse():
def __init__(self):
pass
def something(self):
pass
xx = uneclasse()
xx.something()
xxxxxxxxxx
class ClassName(object): #"(object)" isn't mandatory unless this class inherit from another
def __init__(self, var1=0, var2):
#the name of the construct must be "__init__" or it won't work
#the arguments "self" is mandatory but you can add more if you want
self.age = var1
self.name = var2
#the construct will be execute when you declare an instance of this class
def otherFunction(self):
#the other one work like any basic fonction but in every methods,
#the first argument (here "self") return to the class in which you are
xxxxxxxxxx
A class is a block of code that holds various functions. Because they
are located inside a class they are named methods but mean the samne
thing. In addition variables that are stored inside a class are named
attributes. The point of a class is to call the class later allowing you
to access as many functions or (methods) as you would like with the same
class name. These methods are grouped together under one class name due
to them working in association with eachother in some way.
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 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)