xxxxxxxxxx
class Person:
# class static variable
person_type = "Human"
# class constructor
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
# initialized class method
def get_full_name(self):
return f"{self.first_name} {self.last_name}"
# initialized class method
def introduce(self):
return f"Hi. I'm {self.first_name} {self.last_name}. I'm {self.age} years old."
# class static method
@staticmethod
def class_name(self):
return 'Person'
# class method
@classmethod
def create_anonymous(cls):
return Person('John', 'Doe', 25)
# dunder method - return class as a string when typecast to a string
def __str__(self):
return f"str firstname: {self.first_name} lastname: {self.last_name} age: {self.age}"
# dunder method - return class a string then typecast to representative
def __repr__(self):
return f"repr firstname: {self.first_name} lastname: {self.last_name} age: {self.age}"
# dunder method - return sum of ages when using the + operator on two Person classes
def __add__(self, other):
return self.age + other.age
# create a person class
bob = Person(first_name="John", last_name="Doe", age=41)
# print static method
print(Person.class_name(Person))
# print new class person
print(Person.create_anonymous().get_full_name())
# print class static method
print(Person.person_type)
# print string representation of class
print(bob)
# print representation of class
print(repr(bob))
# add Person classes ages using dunder method
print(bob + bob)
xxxxxxxxxx
class Person:#set name of class to call it
def __init__(self, name, age):#func set ver
self.name = name#set name
self.age = age#set age
def myfunc(self):#func inside of class
print("Hello my name is " + self.name)# code that the func dose
p1 = Person("barry", 50)# setting a ver fo rthe class
p1.myfunc() #call the func and whitch ver you want it to be with
xxxxxxxxxx
class Box(object): #(object) ending not required
def __init__(self, color, width, height): # Constructor: These parameters will be used upon class calling(Except self)
self.color = color # self refers to global variables that can only be used throughout the class
self.width = width
self.height = height
self.area = width * height
def writeAboutBox(self): # self is almost always required for a function in a class, unless you don't want to use any of the global class variables
print(f"I'm a box with the area of {self.area}, and a color of: {self.color}!")
greenSquare = Box("green", 10, 10) #Creates new square
greenSquare.writeAboutBox() # Calls writeAboutBox function of greenSquare object
xxxxxxxxxx
class Animal(object): # Doesn't need params but put it there anyways.
def __init__(self, species, price):
self.species = species # Sets species name
self.price = price # Sets price of it
def overview(self): # A function that uses the params of the __init__ function
print(f"This species is called a {self.species} and the price for it is {self.price}")
class Fish(Animal): # Inherits from Animal
pass # Don't need to add anything because it's inherited everything from Animal
salmon = Fish("Salmon", "$20") # Make a object from class Fish
salmon.overview() # Run a function with it
dog = Animal("Golden retriever", "$400") # Make a object from class Animal
dog.overview() # Run a function with it
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 Student:
def __init__(self, id, name, age):
self.name = name
self.id = id
self.age = age
def greet(self):
print(f"Hello there.\nMy name is {self.name}")
def get_age(self):
print(f"I am {self.age}")
def __add__(self, other)
return Student(
self.name+" "+other.name,
self.id + " "+ other.id,
str(self.age) +" "+str(other.age))
p1 = Student(1, "Jay", 19)
p2 = Student(2, "Jean", 22)
p3 = Student(3, "Shanna", 32)
p4 = Student(4, "Kayla", 23)
result = p1+p3
xxxxxxxxxx
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
p1.age = 40
print(p1.age)
---------------------------------------------------------------
40
xxxxxxxxxx
class Employee(Object)
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def __str__(self)
return f"Employee {name} \nhes age {age} \nand make {salary}"
xxxxxxxxxx
class Dog(object):
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print("Hi I'm ", self.name, 'and I am', self.age, 'Years Old')
JUB0T = Dog('JUB0T', 55)
Friend = Dog('Doge', 10)
JUB0T.speak()
Friend.speak()