can u make classes in python code example
Example 1: python classes
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
Example 2: python class
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
Example 3: How to make a new class in python
#Use the class function and give the class a name
#next use the def __init__() to initilaize and give it some properties.
class Fruits():
def __init__(self, name, colour, taste):
self.name = name
self.colour = colour
self.taste = taste
#Now create an object by first calling the class
fruit1 = Fruits(apple, red, sweet)
print(fruit1.name)
#this will print the name which is apple
print(fruit1.colour)
#this will print the colour which is red
print(fruit1.taste)
#this will print the taste which is sweet