class Product: quantity = 200 product1 = Product() print(product1) print(product1.quantity) product2 = Product() print(product2.quantity)
class Product: quantity = 200 def __init__(self,name,price): self.name = name self.price = price product2 = Product("Phone",500) print(product2.name) print(product2.price)
class Product: quantity = 200 def __init__(self,name,price): self.name = name self.price = price def summer_discount(self,discount_percent): self.price = self.price - (self.price *discount_percent/100) product1 = Product("Laptop",500) print(product1.price) #let's call the method on object here product1.summer_discount(10) print(product1.price)
.
def product_data(): product_name = input('Enter name of product') product_price = input('Enter price of product') print(product_name) print(product_price) product_data()
class Product: def __init__(self,name,price): self.name = name self.price = price def get_data(self): self.name = input('Enter name') self.price = input('Enter price') def put_data(self): print(self.name) print(self.price) product1 = Product("","") product1.get_data() product1.put_data()
class Product: def __init__(self,name,price): self.name = name self.price = price def get_data(self): self.name = input('Enter name') self.price = input('Enter price') def put_data(self): print(self.name) print(self.price) class DigitalProduct(Product): def __init__(self,link): self.link = link def get_link(self): self.link = input('Enter product link') def put_link(self): print(self.link) ebook = DigitalProduct("") ebook.get_data() ebook.get_link() ebook.put_data() ebook.put_link()
class A: def method_a(self): print('Method of class A') class B: def method_b(self): print('Method of class B') class C(A,B): def method_c(self): print('Method of class C') cobject = C() cobject.method_a() cobject.method_b() cobject.method_c()
class A: def method_a(self): print('Method of class A') class B(A): def method_b(self): print('Method of class B') class C(B): def method_c(self): print('Method of class C') cobject = C() cobject.method_a() cobject.method_b() cobject.method_c()
print(10+20) print('Hello'+'world')
# In this case python counts the number of characters in a string print(len('HelloWorld')) # In this case python counts the number of items in a list print(len(['Apple','Mango','Banana']))
class Food: def type(self): print("Food") class Fruit(Food): def type(self): print('Fruit') burger = Food() print(burger.type()) # this invokes the method in the Food class apple = Fruit() print(apple.type()) # The method in the Fruit class overrides the method in the Food class.
.
class Point: def __init__(self,x,y): self.x = x self.y = y #To overload the + operator, we use the __add__ method # To perform overloading, python provides a special method # to overload the + operator we use the magic method __add__ # we want the plus operator to perform addition of two Points/ two objects def __add__(self, other): x = self.x + other.x y = self.y + other.y #once we have the values of x and y we want to return then into a point return Point(x,y) point1 = Point(1,2) point2 = Point(3,4) print(point1+point2)
def __str__(self): return '({0},{1})'.format(self.x,self.y)
class Point: def __init__(self,x,y): self.x = x self.y = y def __add__(self, other): x = self.x + other.x y = self.y + other.y #once we have the values of x and y we want to return then into a point return Point(x,y) def __str__(self): return '({0},{1})'.format(self.x,self.y) point1 = Point(1,2) point2 = Point(3,4) print(point1+point2)
class Student: def __init__(self, name): self.name = name def hello(self): print(f"Hello, my name is {self.name}") def name_length(self): return len(self.name) # create an instance of the class student1 = Student("John") student1.hello() length = student1.name_length() print(length)
class Student: # class variable category = 'student' def __init__(self, name): self.name = name def hello(self): print(f"Hello, my name is {self.name}") def name_length(self): return len(self.name) @classmethod def info(cls): print(f"This is a method of the class {cls.category}") Student.info()
class Student: # class variable category = 'student' def __init__(self, name): self.name = name def hello(self): print(f"Hello, my name is {self.name}") def name_length(self): return len(self.name) @classmethod def info(cls): print(f"This is a method of the class {cls.category}") @staticmethod def add(a, b): print(a+b) Student.add(2, 4)
Another example:
class Circle: @staticmethod def area(r): return 3.14*r*r @staticmethod def circumfurence(r): return 2*3.14*r # calling static methods a = Circle.area(10) print(a) c = Circle.circumfurence(10) print(c)
class Car: def __init__(self, brand): self.brand = brand # create an object of the inner class self.steering_obj = self.Steering() def drive(self): print('Drive') class Steering: # this inner class will now have acess to the outer class def rotate(self): print("Rotate") # now let's create an object of the outer class # create instance of the outer class car = Car("ABC") # access the attribute of the outer class print(car.brand) # access method of outer class car.drive() # access the inner class using the outer object steering = car.steering_obj # now access the inner method steering.rotate()
Another example:
class Zoo: def __init__(self): # iniitially no animals in the zoo # create a list called animals self.animals = [] # create a method that adds animal to above list def add_animal(self, name, species): # now to add an animal, create an object from below class animal = self.Animal(name, species) # now append this animal to the animals list self.animals.append(animal) class Animal: def __init__(self, name, species): self.name = name self.species = species def display_info(self): print(f"Name:{self.name}, Species:{self.species}") # first create a zoo my_zoo = Zoo() # Add animals to the zoo my_zoo.add_animal("Lion", "Mammal") my_zoo.add_animal("Eagle", "Bird") my_zoo.add_animal("Crocodile", "Reptile") # Display information about the animals for animal in my_zoo.animals: animal.display_info()
class Parent: def __init__(self): self.balance = 50000 def display_balance(self): print(f"Parent's property is {self.balance}") class Child(Parent): pass mike = Child() mike.display_balance()
class Parent: def __init__(self): self.balance = 50000 def display_balance(self): print(f"Parent's property is {self.balance}") class Child(Parent): def __init__(self): self.balance = 20000 def display_balance(self): print(f"Child's balance is {self.balance}") mike = Child() mike.display_balance()
class Parent: def __init__(self): self.parent_balance = 50000 def display_balance(self): print(f"Parent's property is {self.parent_balance}") class Child(Parent): def __init__(self): super().__init__() self.child_balance = 20000 def display_balance(self): print(f"Child's balance is {self.child_balance + self.parent_balance}") mike = Child() mike.display_balance()
# super class class Vehicle: # defining a class attribute class_attribute = "This is a vehicle" # creating a constructor with parameters def __init__(self, name, color): self.name = name self.color = color # instance method def display_info(self): print(f"Name:{self.name}, Color:{self.color}") # class method @classmethod # passing class to the method def class_method(cls): print('This is a class method') # accessing a class attribute print(f"I can access class attribute {cls.class_attribute}") # static method @staticmethod def static_method(): # does not access class or instance attribute print("I am a static method, I cannot access anything") # subclass inheriting from Vehicle super class class Car(Vehicle): # subclass consctuctor def __init__(self, name, color, fuel_type): # using super function to invoke init method of the superclass # so that we can access name and color attributes super().__init__(name, color) # creating own instance attribute of a subclass self.fuel_type = fuel_type # sub class overriding the display info method from the superclass def display_info(self): print(f"{self.name},{self.color},{self.fuel_type}") # creating object of the superclass vehicle = Vehicle("CoolCar", "Red") # invoking instance method vehicle.display_info() # creating object of subclass car = Car("LuxuryCar", "Black", "Petrol") # inviking overriden method car.display_info() # acccessing class attribute directly from the class print(Vehicle.class_attribute) # inoking static method. Vehicle.static_method()
class Student: def __init__(self,name,roll_number,age): self.name=name self.roll_number=roll_number self.age=age class School: def __init__(self): self.students = [] def add_student(self,name,roll_number,age): #create a student object student=Student(name,roll_number,age) self.students.append(student) print(f"Student {name} added successfully") def display_students(self): if self.students: for student in self.students: print(f"Name:{student.name}") print(f"Roll number:{student.roll_number}") print(f"Age:{student.age}") print("...................") def edit_student(self,roll_number,new_name,new_age): for student in self.students: if student.roll_number == roll_number: student.name=new_name student.age= new_age print(f"Student {student.name} successfully updated") return def delete_student(self,roll_number): for student in self.students: if student.roll_number == roll_number: self.students.remove(student) print(f"Student {student.name} deleted successfully.") return #Create a school object first: school = School() while True: choice = input('Enter your choice: \\n1)Add student \\n2)Display list of students \\n3)Edit student data \\n5)Exit: ') if choice=="1": name = input('Enter name') roll_number= input('Enter roll number') age = input('Enter age') school.add_student(name, roll_number, age) elif choice =="2": school.display_students() elif choice =="3": roll_number = input("Enter roll number of student you want to edit") new_name = input("Enter new name for the student") new_age = input("Enter new age for the student") school.edit_student(roll_number, new_name, new_age) elif choice=="4": roll_number= input("Enter roll number you want to delete") school.delete_student(roll_number) elif choice=="5": break