Answers for "make a class in python"

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
4

python class

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
  def myfunc(self):
    print("Hello my name is " + self.name +".")

p1 = Person("Victor", 24)
p1.myfunc()
Posted by: Guest on April-30-2021
1

classes in python

# Python classes

class Person():
  # Class object attributes (attributes that not needed to be mentioned when creating new class of person)
  alive = True
  
  def __init__(self, name, age):
    # In the __init__ method you can make attributes that will be mentioned when creating new class of person
    self.name = name
    self.age = age
    
  def speak(self):
    # In every method in class there will be self, and then other things (name, age, etc.)
    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

person_one = Person('Sam', 23) # Sam is the name attribute, and 23 is the age attribute
person_one.speak() # Prints Hello, my name is Sam and my age is 23

==================================================================
# Output:

>>> 'Hello, my name is Sam and my age is 23'
Posted by: Guest on January-09-2021
7

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.
Posted by: Guest on June-29-2020
0

creating python classes

class car:
  def __init__(self, model, color):
    self.model = model
    self.color = color
         
tesla = car("model 3", "black")
Posted by: Guest on June-07-2021

Python Answers by Framework

Browse Popular Code Answers by Language