Python import Keyword



The Python import keyword is used import modules. The modules can be accessed from another module by importing file/functions using import.

Usage

Following is the usage of the Python import keyword −

import <module_name>

Where, module_name is the name of the module we need to import.

Example

Following is an basic usage of the Python import keyword −

import array
myArray = array.array('i',[1, 2, 3, 4, 5])
print(myArray)

Output

Following is the output of the above code −

array('i', [1, 2, 3, 4, 5])

Using the 'import' Keyword with 'from'

We can use import keyword along with from to import a module.

Example

Following is an example of the usage of import along with from

from math import sqrt, pow
# Using the imported functions
print("Square root of 99 is:", sqrt(99))  
print("7 to the power of 3 is:", pow(7, 3))

Output

Following is the output of the above code −

Square root of 99 is: 9.9498743710662
7 to the power of 3 is: 343.0

Using 'import' with asterisk (*)

The import keyword not only import a particular function but also all the functions of the module can be imported by using asterisk[*]. To import all the functions of a module, the import keyword followed by module-name and asterisk.

Example

Here, we have imported all the functions of math module using import and asterisk

from math import *
print("The exponential value of e power 0:", exp(0))
print("The gcd of 3 and 6 :",gcd(3,6))

Output

Following is the output of the above code −

The exponential value of e power 0: 1.0
The gcd of 3 and 6 : 3

Using 'import' Keyword with 'as'

We can also use as keyword along with import to make a selected name for a particular function.

Example

Following is an example of, usage of import keyword along with as keyword −

from array import array as arr
myArray = arr('i',[10, 20, 30, 40, 50]) 
print(myArray)

Output

Following is the output of the above code −

array('i', [10, 20, 30, 40, 50])
python_keywords.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy