Day 3 Advanced Python Concepts
Day 3 Advanced Python Concepts
Modules and Packages: Modules in Python are simply Python files with the .py extension
that contain Python code. Packages are directories that contain multiple modules.
Error and Exception Handling: Errors in Python can be handled using try-except blocks,
allowing graceful recovery from potential exceptions.
1. Array Creation
2. Array Properties
3. Array Manipulation
5. Mathematical Operations
6. Statistical Functions
7. File I/O
8. Special Functions
# mymodule.py
def greet(name):
return f"Hello, {name}!"
# main.py
import mymodule
print(mymodule.greet("Alice"))
Packages:
mypackage/
__init__.py
module1.py
module2.py
try:
x = 1 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
Finally Block:
try:
x = 1 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("This runs no matter what")
Raising Exceptions:
def check_positive(x):
if x < 0:
raise ValueError("x should be positive")
check_positive(-1)
Arrays:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3], [4, 5, 6]])
Array Operations:
Element-wise operations: a + 1, a * 2
Matrix operations: np.dot(a, b), a.T (transpose)
Universal functions: np.sqrt(a), np.exp(a)
Example:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # Output: [5 7 9]
print(a * b) # Output: [ 4 10 18]