Oct 6, 2024
type() function to see that data types like str and int are classes.print(type("Hello")) # Output: <class 'str'>
print(type(1)) # Output: <class 'int'>
x = 1 creates an object of class int with value 1.string.upper()).string = "hello"
print(string.upper()) # HELLO
class keyword, e.g., class Dog:.bark() within the class.class Dog:
def bark(self):
print("bark")
# Create instance of Dog
my_dog = Dog()
my_dog.bark() # Output: bark
__init__ Methodself to refer to the instance itself.class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog("Rex")
print(my_dog.name) # Output: Rex
__init__ using self.class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def bark(self):
print("bark")
class Animal:
def speak(self):
print("I don't know what to say")
class Dog(Animal):
def speak(self):
print("bark")
self or cls.cls.class Math:
@staticmethod
def add(x, y):
return x + y
print(Math.add(5, 3)) # Output: 8
class Person:
number_of_people = 0
@classmethod
def people_count(cls):
return cls.number_of_people