Lecture-11 Import_24-25
Overview of COMP101 Introduction to Programming, Lecture 11 covering Modules & Functions in Python
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.
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.
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.
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 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.
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.
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.
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.
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()
).
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.
To view all builtin functions:
Use commands:
import builtins
dir(builtins)
Provides immediate availability in the code without needing imports.
To access pre-built modules:
Command: help()
followed by:
help('modules')
to see available modules.
To find out functions available in a specific module:
Example: import math
Command: dir(math)
to list all functions within the math module.
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.
Overview of COMP101 Introduction to Programming, Lecture 11 covering Modules & Functions in Python
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.
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.
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.
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 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.
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.
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.
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.
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()
).
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.
To view all builtin functions:
Use commands:
import builtins
dir(builtins)
Provides immediate availability in the code without needing imports.
To access pre-built modules:
Command: help()
followed by:
help('modules')
to see available modules.
To find out functions available in a specific module:
Example: import math
Command: dir(math)
to list all functions within the math module.
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.