class python examples
Example 1: how to make a class in python
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()
Example 2: python class
class Animal(object):
def __init__(self, species, price):
self.species = species
self.price = price
def overview(self):
print(f"This species is called a {self.species} and the price for it is {self.price}")
class Fish(Animal):
pass
salmon = Fish("Salmon", "$20")
salmon.overview()
dog = Animal("Golden retriever", "$400")
dog.overview()
Example 3: what is a class in python
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.
Example 4: class python example
def ok(index):
print(index) > Hi!
ok("Hi!")
class Lib:
def ok(self,Token):
print(Token) > Hello World!
Library = Lib()
Library.ok("Hello World!")
Example 5: How to make a new class in python
class Fruits():
def __init__(self, name, colour, taste):
self.name = name
self.colour = colour
self.taste = taste
fruit1 = Fruits(apple, red, sweet)
print(fruit1.name)
print(fruit1.colour)
print(fruit1.taste)