pythonqb
pythonqb
Define Function
A function in Python is defined using the 'def' keyword followed by the function name and
parameters.
Example:
def greet(name):
print("Hello", name)
2. Define Module
A module is a file containing Python code. It allows code reuse and modular programming.
Example (mymodule.py):
return a + b
3. Define Package
Structure:
mypackage/
__init__.py
module1.py
Example:
return a + b
5. Self Parameter
Example:
class Student:
def __init__(self, name):
self.name = name
7. Functions of Directories
Example:
try:
x=1/0
except ZeroDivisionError:
print("Error")
finally:
print("Done")
Use default or variable-length arguments since Python doesn't support traditional overloading.
Example:
def show(*args):
for i in args:
print(i)
10. Programs
# my_module.py
def greet():
print("Hello")
# main.py
import my_module
my_module.greet()
3. Parameterized Constructors:
class Base:
print("Base:", x)
class Derived(Base):
super().__init__(x)
print("Derived:", y)
with open("sample.txt") as f:
line = f.readline()
print(line[:11])