knowt logo

Lecture-11 Import_24-25

Introduction to Modules & Functions in Python

  • Overview of COMP101 Introduction to Programming, Lecture 11 covering Modules & Functions in Python

Complexity in Programming

  • Sometimes coding requires complex solutions to tackle problems.

  • Example: The value of pi is utilized across various applications and must be calculated accurately.

  • Instead of directly coding pi every time, a separate program or function can manage its value.

Sample Design for Calculating Pi

  • Method:

    • Inscribe and circumscribe regular hexagons within a circle.

    • Calculate the circumference and diameter of these hexagons.

  • Formula used: pi = circumference/diameter.

  • Considerations include:

    • Decimal accuracy

    • Precision in calculations

    • Selection of the best algorithm.

Importance of Code Reuse

  • Code reusability is crucial for programming efficiency.

  • Avoids repetitive calculations, e.g., using pi.

  • Create and save code as a module, which can then be invoked as needed.

Modules and Functions

  • Saved programs are referred to as modules (files with .py extension).

  • Functions encapsulated in these modules, e.g., calculations for pi, random number generation, etc.

  • Key insight: Reliable functions are often pre-written by experts, tested for accuracy, and included in these modules.

Python Builtin Functions

  • Python auto-loads common functions that are frequently used.

  • Examples include: input(), print(), etc.

  • Function definitions include empty parentheses to input desired data.

  • A thorough list of functions can be found at: https://docs.python.org/3/library/functions.html.

Builtin Functions Overview

  • A brief look at several builtin functions:

    • abs(), all(), any(), bin(), bool(), bytes(), float(), int(), len(), max(), min(), print(), sorted(), str(), zip(), etc.

  • Note: Available functions may vary based on the Python version.

Importing Modules

  • Some required functions may not be available as builtin functions and must be imported from external modules.

  • Modules are Python programs usually with a .py extension and contained within a 'Python Library'.

  • Use of modules requires importing, e.g., math, random, statistics, etc.

Focus on the Math Module

  • The ‘math’ module includes essential mathematical functions necessary for various applications.

  • Available functions in the 'math' module include:

    • sin(), cos(), tan(), sqrt(), pi, factorial(), etc.

Importing Modules and Using Functions

  • Begin with import statements to utilize module functions:

    • Example: import math

    • Access pi value with: print(math.pi)

  • Importance of using 'dot notation' to reference module functions clearly (e.g., math.sqrt()).

Importing Specific Functions

  • It’s possible to import only specific functions from modules for efficiency:

    • Example: from math import pi allows direct access to pi without prefixing with the module name.

  • Limitation: Importing specific functions limits access to the other functions within the module.

Accessing Built-in Functions in Python Shell

  • To view all builtin functions:

    • Use commands:

      • import builtins

      • dir(builtins)

  • Provides immediate availability in the code without needing imports.

Pre-Built Module Access

  • To access pre-built modules:

    • Command: help() followed by:

    • help('modules') to see available modules.

Discovering Functions by Module

  • To find out functions available in a specific module:

    • Example: import math

    • Command: dir(math) to list all functions within the math module.

Additional Resources

  • Easily access Python's documentation online for functions and modules:

    • Direct link: https://docs.python.org/library/math

  • Alternative: Search online for "Python math module" for extensive information.

YP

Lecture-11 Import_24-25

Introduction to Modules & Functions in Python

  • Overview of COMP101 Introduction to Programming, Lecture 11 covering Modules & Functions in Python

Complexity in Programming

  • Sometimes coding requires complex solutions to tackle problems.

  • Example: The value of pi is utilized across various applications and must be calculated accurately.

  • Instead of directly coding pi every time, a separate program or function can manage its value.

Sample Design for Calculating Pi

  • Method:

    • Inscribe and circumscribe regular hexagons within a circle.

    • Calculate the circumference and diameter of these hexagons.

  • Formula used: pi = circumference/diameter.

  • Considerations include:

    • Decimal accuracy

    • Precision in calculations

    • Selection of the best algorithm.

Importance of Code Reuse

  • Code reusability is crucial for programming efficiency.

  • Avoids repetitive calculations, e.g., using pi.

  • Create and save code as a module, which can then be invoked as needed.

Modules and Functions

  • Saved programs are referred to as modules (files with .py extension).

  • Functions encapsulated in these modules, e.g., calculations for pi, random number generation, etc.

  • Key insight: Reliable functions are often pre-written by experts, tested for accuracy, and included in these modules.

Python Builtin Functions

  • Python auto-loads common functions that are frequently used.

  • Examples include: input(), print(), etc.

  • Function definitions include empty parentheses to input desired data.

  • A thorough list of functions can be found at: https://docs.python.org/3/library/functions.html.

Builtin Functions Overview

  • A brief look at several builtin functions:

    • abs(), all(), any(), bin(), bool(), bytes(), float(), int(), len(), max(), min(), print(), sorted(), str(), zip(), etc.

  • Note: Available functions may vary based on the Python version.

Importing Modules

  • Some required functions may not be available as builtin functions and must be imported from external modules.

  • Modules are Python programs usually with a .py extension and contained within a 'Python Library'.

  • Use of modules requires importing, e.g., math, random, statistics, etc.

Focus on the Math Module

  • The ‘math’ module includes essential mathematical functions necessary for various applications.

  • Available functions in the 'math' module include:

    • sin(), cos(), tan(), sqrt(), pi, factorial(), etc.

Importing Modules and Using Functions

  • Begin with import statements to utilize module functions:

    • Example: import math

    • Access pi value with: print(math.pi)

  • Importance of using 'dot notation' to reference module functions clearly (e.g., math.sqrt()).

Importing Specific Functions

  • It’s possible to import only specific functions from modules for efficiency:

    • Example: from math import pi allows direct access to pi without prefixing with the module name.

  • Limitation: Importing specific functions limits access to the other functions within the module.

Accessing Built-in Functions in Python Shell

  • To view all builtin functions:

    • Use commands:

      • import builtins

      • dir(builtins)

  • Provides immediate availability in the code without needing imports.

Pre-Built Module Access

  • To access pre-built modules:

    • Command: help() followed by:

    • help('modules') to see available modules.

Discovering Functions by Module

  • To find out functions available in a specific module:

    • Example: import math

    • Command: dir(math) to list all functions within the math module.

Additional Resources

  • Easily access Python's documentation online for functions and modules:

    • Direct link: https://docs.python.org/library/math

  • Alternative: Search online for "Python math module" for extensive information.

robot