Answers for "class in python example"

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

classes in python

class Foo:
  def __init__(self):
    self.definition = Foo!
  def hi():
    # Some other code here :)
    
# Classes require an __init__ if you want to assign attributes. (self) defines what describes the attribs.
Posted by: Guest on January-21-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
1

python classes explained

print('hello world')
Posted by: Guest on December-10-2019
1

class methods in python

@classmethod
def func(cls, args...)
Posted by: Guest on November-14-2020
0

Python Class Example

#Source: https://vegibit.com/python-class-examples/
class Vehicle:
    def __init__(self, brand, model, type):
        self.brand = brand
        self.model = model
        self.type = type
        self.gas_tank_size = 14
        self.fuel_level = 0

    def fuel_up(self):
        self.fuel_level = self.gas_tank_size
        print('Gas tank is now full.')

    def drive(self):
        print(f'The {self.model} is now driving.')

obj = Vehicle("Toyota", "Carola", "Car")
obj.drive()
Posted by: Guest on May-17-2021

Python Answers by Framework

Browse Popular Code Answers by Language