MODULES
Modules in Python are files containing Python code
(definitions and statements). They provide a way to
organize code into manageable sections. A module can
define functions, classes, and variables that can be
imported and used in other Python programs.
# mymodule.py
def add(a, b):
return a + b
# main.py
import mymodule
print(mymodule.add(3, 4)) # Output: 7
Object-Oriented Programming (OOP)
Python supports object-oriented programming, a paradigm that
uses objects and classes. OOP concepts include:
CLASS : A blueprint for creating objects. Classes encapsulate
data and behaviors.
python
Copy code
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return "Woof!"
my_dog = Dog("Buddy", 3)
print(my_dog.bark()) # Output: Woof
INHERITANCE : A mechanism for creating a new class from an
existing class. The new class (subclass) inherits attributes
and behaviors from the existing class (superclass).
class Animal:
def __init__(self, species):
self.species = species
def sound(self):
return "Some sound"
class Cat(Animal):
def sound(self):
return "Meow"
my_cat = Cat("Feline")
print(my_cat.sound()) # Output: Meow