Python Modules: Enhancing Code Capabilities

modules n.w
1 / 8
Embed
Share

Python modules are essential for extending functionality in code through standard and third-party packages. Learn how to import modules, use standard libraries, avoid naming conflicts, and leverage third-party modules effectively. Explore the world of Python scripts as modules for efficient code organization and reusability.

  • Python Modules
  • Standard Libraries
  • Third-Party Packages
  • Importing Modules
  • Code Organization

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Modules Python allows for the inclusion of code packages called modules to support additional capabilities. Packages fall into two broad categories: Standard (built-in) Third party Professor John Carelli, Kutztown University

  2. Useful Standard Libraries The following modules are included in the standard Python distribution os and sys: tools for interfacing with the operating system. math: mathematical functions and operations itertools: tools for constructing and interacting with iterators and generators. random: tools for generating pseudorandom numbers json and csv: tools for reading standard formats pickle: save and load objects to/from disks Professor John Carelli, Kutztown University

  3. Importing Modules In order to use a module (regardless of the source), it must be loaded using the import statement Various ways: import module import module as mod from module import func1, func2, from module import * Professor John Carelli, Kutztown University

  4. Explicit import # explicit import import math # explicit import using an alias import random as rand # module contents accessed using # name/alias with dot operator num= rand.random() root= math.sqrt(num) Professor John Carelli, Kutztown University

  5. Import of contents # explicit import of specific content from math import sqrt # implicit import of all contents from random import * # no need to use module name num= random() root= sqrt(num) Caution when using implicit import! May get naming conflicts of contents between modules or scripted code! (usually better to just import what you need) Professor John Carelli, Kutztown University

  6. Third-party Modules The following are some commonly used third-party* modules available in Python numpy: efficient storage and manipulation of multi- dimensional dense arrays. pandas: provides a labeled interface to multi- dimensional data. matplotlib: data visualizations (plotting) * In general, third-party modules must be downloaded and installed separately (although these are included in an Anaconda installation) Professor John Carelli, Kutztown University

  7. Python Scripts as Modules Any Python script can be treated as a module It can be imported into another script Statements in the script will run Functions/objects defined in the script are accessible to the importing script Professor John Carelli, Kutztown University

  8. Module Convention It is possible to import the contents of a script without executing code that would normally run if the script were executed as a Python program if __name__ == '__main__': # Statements in this block will only be executed # when the script is run as a stand-alone program # They will not be executed when imported! milesModule.py Professor John Carelli, Kutztown University

Related


More Related Content