1class Person:
2 def __init__(self, _name, _age):
3 self.name = _name
4 self.age = _age
5
6 def sayHi(self):
7 print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
8
9p1 = Person('Bob', 25)
10p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
1# To create a simple class:
2class Shape:
3 def __init__():
4 print("A new shape has been created!")
5 pass
6
7 def get_area(self):
8 pass
9
10# To create a class that uses inheritance and polymorphism
11# from another class:
12class Rectangle(Shape):
13
14 def __init__(self, height, width): # The constructor
15 super.__init__()
16 self.height = height
17 self.width = width
18
19 def get_area(self):
20 return self.height * self.width
21
1class Person:#set name of class to call it
2 def __init__(self, name, age):#func set ver
3 self.name = name#set name
4 self.age = age#set age
5
6
7 def myfunc(self):#func inside of class
8 print("Hello my name is " + self.name)# code that the func dose
9
10p1 = Person("barry", 50)# setting a ver fo rthe class
11p1.myfunc() #call the func and whitch ver you want it to be with
1class Foo:
2 def __init__(self):
3 self.definition = Foo!
4 def hi():
5 # Some other code here :)
6
7# Classes require an __init__ if you want to assign attributes. (self) defines what describes the attribs.
8
1# Python classes
2
3class Person():
4 # Class object attributes (attributes that not needed to be mentioned when creating new class of person)
5 alive = True
6
7 def __init__(self, name, age):
8 # In the __init__ method you can make attributes that will be mentioned when creating new class of person
9 self.name = name
10 self.age = age
11
12 def speak(self):
13 # In every method in class there will be self, and then other things (name, age, etc.)
14 print(f'Hello, my name is {self.name} and my age is {self.age}') # f'' is type of strings that let you use variable within the string
15
16person_one = Person('Sam', 23) # Sam is the name attribute, and 23 is the age attribute
17person_one.speak() # Prints Hello, my name is Sam and my age is 23
18
19==================================================================
20# Output:
21
22>>> 'Hello, my name is Sam and my age is 23'
1class fruit:
2 def __init__(self,color,taste,name):
3 self.color = color
4 self.name = name
5 self.taste = taste
6
7 def myfunc(self):
8 print("{} = Taste:{}, Color:{}".format(self.name, self.taste, self.color))
9
10f1 = fruit("Red", "Sweet", "Red Apple")
11f1.myfunc()