0% found this document useful (0 votes)
4 views

Python Basics 2

Python basics part 2

Uploaded by

thekrish360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Basics 2

Python basics part 2

Uploaded by

thekrish360
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like