Skip to content

Latest commit

 

History

History
328 lines (229 loc) · 7.73 KB

File metadata and controls

328 lines (229 loc) · 7.73 KB

Mastering Python Modules! A Comprehensive Guide

Slide 1: What are Python Modules?

Python modules are reusable code files containing functions, classes, and variables. They help organize and structure code, making it more maintainable and efficient. Modules can be built-in, like 'os' and 'math', or custom-created by developers.

import math

# Using a function from the math module
radius = 5
area = math.pi * radius ** 2
print(f"The area of a circle with radius {radius} is {area:.2f}")

# Output:
# The area of a circle with radius 5 is 78.54

Slide 2: Importing Modules

Modules can be imported using the 'import' statement. There are different ways to import modules, each with its own use case.

import random

# Importing specific functions from a module
from datetime import datetime, timedelta

# Importing all functions from a module (use with caution)
from math import *

# Using imported functions
print(random.randint(1, 10))
print(datetime.now())
print(sqrt(16))

# Output:
# 7
# 2024-09-16 14:30:45.123456
# 4.0

Slide 3: Creating Custom Modules

Custom modules allow you to organize your code into separate files for better maintainability and reusability.

def greet(name):
    return f"Hello, {name}!"

def add(a, b):
    return a + b

# File: main.py
import my_module

print(my_module.greet("Alice"))
print(my_module.add(3, 4))

# Output:
# Hello, Alice!
# 7

Slide 4: Using Aliases for Modules

Aliases can make your code more concise and readable, especially for modules with long names.

import matplotlib.pyplot as plt

# Generate data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create a plot
plt.plot(x, y)
plt.title("Sine Wave")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

Slide 5: Importing from Different Directories

Sometimes, you need to import modules from different directories. Understanding Python's module search path is crucial.

import os

# Add a directory to Python's module search path
custom_module_path = os.path.abspath('../custom_modules')
sys.path.append(custom_module_path)

# Now you can import modules from the added directory
import my_custom_module

print(my_custom_module.custom_function())

# Output depends on the content of my_custom_module

Slide 6: Handling Import Errors

Import errors are common when working with modules. Understanding how to troubleshoot them is essential.

    import non_existent_module
except ImportError as e:
    print(f"Error importing module: {e}")
    
    # Suggest installing the module if it's a third-party package
    print("Try installing the module using:")
    print("pip install non_existent_module")

# Output:
# Error importing module: No module named 'non_existent_module'
# Try installing the module using:
# pip install non_existent_module

Slide 7: Using name == "main"

The __name__ == "__main__" idiom allows you to write code that runs only when the script is executed directly, not when it's imported as a module.

def main_function():
    print("This is the main function of my_module")

if __name__ == "__main__":
    print("This module is being run directly")
    main_function()
else:
    print("This module is being imported")

# When run directly:
# This module is being run directly
# This is the main function of my_module

# When imported:
# This module is being imported

Slide 8: Exploring Built-in Modules

Python comes with a rich set of built-in modules. Let's explore some useful ones.

import sys
import random
import json

# Get current working directory
print(os.getcwd())

# Get Python version
print(sys.version)

# Generate a random number
print(random.randint(1, 100))

# Work with JSON data
data = {"name": "Alice", "age": 30}
json_string = json.dumps(data)
print(json_string)

# Output varies based on your system and random generation

Slide 9: Working with Package Managers

Package managers like pip make it easy to install and manage third-party modules.

# First, install it using pip:
# pip install requests

import requests

response = requests.get("https://api.github.com")
print(f"GitHub API Status Code: {response.status_code}")

if response.status_code == 200:
    print("Successfully connected to GitHub API")
else:
    print("Failed to connect to GitHub API")

# Output:
# GitHub API Status Code: 200
# Successfully connected to GitHub API

Slide 10: Real-Life Example: Web Scraping

Let's use the 'requests' and 'beautifulsoup4' modules for a simple web scraping task.

# pip install requests beautifulsoup4

import requests
from bs4 import BeautifulSoup

url = "https://news.ycombinator.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

# Extract and print the titles of the top stories
for story in soup.find_all('span', class_='titleline')[:5]:
    print(story.get_text())

# Output will be the titles of the top 5 stories on Hacker News

Slide 11: Real-Life Example: Data Analysis

Using pandas and matplotlib for basic data analysis and visualization.

# pip install pandas matplotlib

import pandas as pd
import matplotlib.pyplot as plt

# Create a sample dataset
data = {
    'Year': [2010, 2011, 2012, 2013, 2014],
    'Sales': [100, 150, 200, 180, 210]
}

df = pd.DataFrame(data)

# Calculate year-over-year growth
df['Growth'] = df['Sales'].pct_change() * 100

# Plot the data
plt.figure(figsize=(10, 6))
plt.plot(df['Year'], df['Sales'], marker='o')
plt.title('Sales Over Time')
plt.xlabel('Year')
plt.ylabel('Sales')
plt.grid(True)
plt.show()

print(df)

# Output:
#    Year  Sales     Growth
# 0  2010    100        NaN
# 1  2011    150  50.000000
# 2  2012    200  33.333333
# 3  2013    180 -10.000000
# 4  2014    210  16.666667

Slide 12: Module Best Practices

Following best practices when working with modules can lead to more maintainable and efficient code.

from math import sqrt, pi

# Bad practice: Using wildcard imports
# from math import *

def calculate_circle_area(radius):
    return pi * radius ** 2

def calculate_hypotenuse(a, b):
    return sqrt(a**2 + b**2)

print(f"Area of circle with radius 5: {calculate_circle_area(5):.2f}")
print(f"Hypotenuse of triangle with sides 3 and 4: {calculate_hypotenuse(3, 4):.2f}")

# Output:
# Area of circle with radius 5: 78.54
# Hypotenuse of triangle with sides 3 and 4: 5.00

Slide 13: Exploring Advanced Module Concepts

Let's dive into some advanced module concepts like lazy imports and context managers.

from importlib import import_module

def lazy_import(module_name):
    return lambda: import_module(module_name)

# The module is only imported when needed
numpy = lazy_import('numpy')

# Context manager example
class FileManager:
    def __init__(self, filename):
        self.filename = filename
        self.file = None

    def __enter__(self):
        self.file = open(self.filename, 'w')
        return self.file

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.file:
            self.file.close()

# Using the context manager
with FileManager('example.txt') as file:
    file.write('Hello, World!')

print("File operations completed.")

# Output:
# File operations completed.

Slide 14: Additional Resources

For further exploration of Python modules and related topics, consider these resources:

  1. Python's official documentation on modules: https://docs.python.org/3/tutorial/modules.html
  2. "The Hitchhiker's Guide to Python" by Kenneth Reitz and Tanya Schlusser
  3. "Fluent Python" by Luciano Ramalho
  4. Python Package Index (PyPI): https://pypi.org/
  5. Real Python tutorials: https://realpython.com/

Remember to always verify the credibility and relevance of additional resources before using them in your learning journey.