User:Niraj/Teaching-10

From ICTED-WIKI
Jump to navigation Jump to search

Teaching lesson plan 10 Subject: Python programming

Date: 25 Jan 2024

Time: 60 minutes

Period: 3rd

Teaching Item: Python Modules & Packages

Class: Bachelor

Objective:

Students will understand the concept of modules and packages in Python and learn how to create, import, and use them effectively in their code.

Materials Needed:

  • Python interpreter or IDE
  • Projector
  • Internet access for additional resources

1. Introduction to Modules (15 mins)

  • Explain what modules are:
    • Modules are files containing Python code.
    • They help organize code into reusable parts.
  • Discuss the benefits of using modules:
    • Code reuse
    • Simplification of complex programs
  • Show examples of built-in modules such as math, random, and datetime.

2. Creating and Using Modules (20 mins)

  • Demonstrate how to create a module:
    • Create a Python file with some functions or variables.
    • Save it with a .py extension.
  • Explain module import:
    • Syntax: import module_name
    • Example: import my_module
  • Show how to use functions and variables from the imported module:
    • Syntax: module_name.function_name() or module_name.variable_name
    • Example: my_module.my_function() or my_module.my_variable

3. Introduction to Packages (10 mins)

  • Introduce the concept of packages:
    • Packages are a way of organizing related modules into a directory hierarchy.
    • Packages help prevent naming conflicts and provide a hierarchical structure to the module namespace.
  • Discuss the necessity of __init__.py files in packages:
    • __init__.py files indicate that the directory should be considered a package.
    • They can be empty or contain initialization code for the package.

4. Creating and Using Packages (15 mins)

  • Demonstrate how to create a package:
    • Create a directory with a meaningful name.
    • Inside the directory, create multiple Python files (modules) and an __init__.py file.
  • Show how to import modules from packages:
    • Syntax: import package_name.module_name
    • Example: import my_package.my_module
  • Explain relative imports within packages:
    • Syntax: from . import module_name
    • Example: from . import my_module

5. Exercise (10 mins)

  • Provide a simple programming exercise where students:
    • Create a module with some functions.
    • Create a package with multiple modules.
    • Import and use functions from the module and package in a separate Python script.

6. Conclusion (5 mins)

  • Recap the key points covered in the lesson:
    • Modules are files containing Python code.
    • Packages are a way of organizing related modules into a directory hierarchy.
    • Importing modules and packages enables code reuse and simplifies program structure.
  • Encourage students to explore more built-in and third-party modules and packages available in Python.