Python provides functionality using library of modules.This is known as standard library.Before you use any library use the import statement.
Importing modules
For example to import string library use:
import string
To import multiple modules use the following syntax:
import module1, module2
Accessing members of a module
to access a member of the module use the following syntax:
module-name.member-name
for example to get the current time you can use the datetime module.In the datetime module there is a datetime object which can be used to get the current system date and time.
to get the current date and time
datetime.datetime.now()
Help on any Module
to get the help on any module use the help() method and pass it the module name.For example to get the information about the
datetime module use the following statement:
help(datetime)
Similarly to get help about the string module use the following statements:
import string help(string)
Shortcut for accessing members of a Module
Above we have used the following statement to display the current date and time
datetime.datetime.now()
there is another way we can write the above statment which is much simpler:
from datetime import datetime
We use the from module-name import member name syntax to directly use the member name without using the module name to access the member name.
There are different modules in Python for different functionality.There are modules related to the different functionalities such as the following:
- string
- datetime
- files
- data persistence
- math
For example you can use the math module to perform different mathematical functions:
math.sqrt(9)
Leave a Reply