4.14 import
: A Deeper Look
You’ve imported modules (such as math
and random
) with a statement like:
import module_name
then accessed their features via each module’s name and a dot (.
). Also, you’ve imported a specific identifier from a module (such as the decimal
module’s Decimal
type) with a statement like:
from module_name import identifier
then used that identifier without having to precede it with the module name and a dot (.
).
Importing Multiple Identifiers from a Module
Using the from
…import
statement you can import a comma-separated list of identifiers from a module then use them in your code without having to precede them with the module name and a dot (.
):
In [1]: from math import ceil, floor
In [2]: ceil(10.3)
Out[2]: 11
Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.