Answers for "python classes example"

22

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() # Prints: Hello, my name is Bob and I am 25 years old!
Posted by: Guest on November-17-2019
16

declare class python

# To create a simple class:
class Shape:
  	def __init__():
      	print("A new shape has been created!")
      	pass
    
    def get_area(self):
		pass

# To create a class that uses inheritance and polymorphism
# from another class:
class Rectangle(Shape):
  
	def __init__(self, height, width): # The constructor
    	super.__init__()
        self.height = height
    	self.width = width

	def get_area(self):
      	return self.height * self.width
Posted by: Guest on March-08-2020
19

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
Posted by: Guest on June-12-2020
2

class in python

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()
Posted by: Guest on June-02-2021
1

python classes

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
Posted by: Guest on February-16-2021
4

class and object in python

#NOTES
class PartyAnimal:
  	def Party():
      #blahblah

an=PartyAnimal()

an.Party()# this is same as 'PartyAnimal.Party(an)'
Posted by: Guest on July-13-2020

Python Answers by Framework

Browse Popular Code Answers by Language