Answers for "python classes tutorial"

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
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
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
0

class python example

# If questions ask SK3#3160 he can help you i think
def ok(index):
    print(index) > Hi!
ok("Hi!")
class Lib:
    def ok(self,Token):
        print(Token) > Hello World!
Library = Lib()
Library.ok("Hello World!")
Posted by: Guest on January-08-2021
-1

class in python

class LambdaClass: 
    x = lambda a, b, c, d, e, f: a + b + c + d + e + f
    print(x(31231, 312, 312, 31, 12, 31))

print(LambdaClass)
Posted by: Guest on March-31-2021
-1

class python

class Charge:
    def __init__(self, employee, discount):
        self.employee = employee
        self.discount = discount
        
    def _discounted_pricey(self):
        item_price = 100
        discounted_price = item_price * (1-self.discount)
        return discounted_price
        
    def __discounted_price(self):
        item_price = 100
        discounted_price = item_price * (1-self.discount)
        return discounted_price

    def pays(self):
        price_to_charge = self.__discounted_price() # gotcha: self.
        print(f'Charge {self.employee} ${price_to_charge:.2f} ({self.discount:.2%} off)')
    
Employee = Charge(employee='Billy Beans', discount=.1)

print(Employee.employee) # Billy Beans
print(Employee.discount) # 0.1
print(Employee._discounted_pricey()) # 90.0
print(Employee.__discounted_price()) # AttributeError: 'Charge' object has no attribute '__discounted_price'
print(Employee.item_price) # AttributeError: 'Charge' object has no attribute 'item_price'
Employee.pays() # Charge Billy Beans $90.00 (10.00% off)
Posted by: Guest on August-07-2021

Python Answers by Framework

Browse Popular Code Answers by Language