Chapter: Problem Solving and Python Programming : Data, Expressions, Statements

Python Import

A module is a file containing Python definitions and statements.

Python Import

A module is a file containing Python definitions and statements. Python modules have a filename and end with the extension .py.

Definitions inside a module can be imported to another module or the interactive interpreter in Python. We use the import keyword to do this.

Python provides two ways to import modules.

>>> import math

>>> print math

<module 'math' (built-in)>

>>> print math.pi

3.14159265359

If you import math, you get a module object named math. The module object contains constants like pi and functions like sin and exp.

But if you try to access pi directly, you get an error.

>>> print pi

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'pi' is not defined

As an alternative, you can import an object from a module like this:

>>> from math import pi

Now you can access pi directly, without dot notation.

>>> print pi

3.14159265359

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Data, Expressions, Statements : Python Import |

Related Topics

Problem Solving and Python Programming : Data, Expressions, Statements


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.