• Classes

    A class in programming is like a group of people. For example, if you have a group of friends, each person in the group is called a "friend". In programming, we call each person in the group a "class". Just like your friends might all have different names and personalities, classes can also have different names and qualities. So when you write code, you can create different classes with different names and abilities to do things.

      • Object Classes

      • class Person:
        	def __init__(self, name, age):
        		self.name = name
        		self.age = age
        
        	def greet(self):
        		print('Hello my name is ' + self.name)
        
        
        human = Person('John', 36)
        
        print(human.name)  # John
        print(human.age)  # 36
        human.greet()  # Hello my name is John
        
      • Trait Classes

      • import math
        
        
        class HasFlippers(object):
        	def swim(self):
        		return 'can swim'
        
        
        class HasFeathers(object):
        	def fly(self):
        		return 'can fly'
        
        
        class Duck(HasFlippers, HasFeathers):
        	pass
        
        
        class Seal(HasFlippers):
        	pass
        
        
        a = Duck()
        print(a.swim())
        print(a.fly())
        
        b = Seal()
        print(b.swim())
        
      • Interface Classes

      • import math
        
        
        class Shape:
        	def area():
        		pass
        
        
        class Rectangle(Shape):
        	def __init__(self, width, height):
        		self.width = width
        		self.height = height
        
        	def area(self):
        		return self.width * self.height
        
        
        class Circle(Shape):
        	def __init__(self, radius):
        		self.radius = radius
        
        	def area(self):
        		return math.pi * self.radius * self.radius
        
        
        a = Rectangle(width=3, height=5)
        b = Circle(radius=4)
        
        print('area of a is {:f}'.format(a.area()))  # area of a is 15.000000
        print('area of b is {:f}'.format(b.area()))  # area of b is 50.265482
        
      • Struct Classes

      • from typing import NamedTuple
        
        class Person(NamedTuple):
        	name: str
        	age: int
        
        
        human = Person(name='John', age=36)
        
        print(human.name)  # John
        print(human.age)  # 36
        
      • Enum Classes

      • from enum import Enum
        
        class Days:
        	Sunday = 0
        	Monday = 1
        	Tuesday = 2
        	Wednesday = 3
        	Thursday = 4
        	Friday = 5
        	Saturday = 6
        
        day = 4;
        
        if day == Days.Saturday or day == Days.Sunday :
        	print('a weekend');
        else:
        	print('a weekday'); # a weekday