Python Import and from import use

Source: Internet
Author: User
Tags for in range

A Python program can invoke a basic set of functions (that is, built-in functions), such as print (), input (), and Len (). Python itself also contains a set of modules (the standard library). Each module is a Python program and contains a set of related functions that can be embedded into your program, for example, the math module contains functions related to mathematical operations, the random module contains functions that are related to stochastic numbers, and so on.

First, import statements

Before you start using a function in a module, you must import the module with an import statement.

Grammar:
import module1[, module2[,... Modulen]]
Instance:

1. Use the Random module ranint () function:

# printrandom.py Import Random  for  in range (5):    Print(Random.randint (1, ten))#  Result: 53649

Note: Because the Randint () function belongs to the random module, you must precede the function name with random, telling Python to look for this function in the random module.

2. Import multiple modules:

import Math, sys, random, OS
Second, from import statement

This is another form of the import module, which uses this form of import statement to invoke functions in the module without modulename prefix. However, using the full name makes the code more readable, so it is best to use the normal form of the import statement.

Grammar:
import name1[, name2[, ... namen]]|*
Instance:

Import all functions under the random module:

 from Import * for in range (5):    print#  There is no need for random. Prefix 

Import the Randint under the random module, the random function:

 from Import Randint, random

Python Import and from import use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.