程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Function module import of Python Foundation

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .

Catalog

      • One 、 background
      • Two 、 Module import
        • 1. Import the entire module
        • 2. Import specific functions
        • 3. Import all functions in the module
      • 3、 ... and 、 Reference resources
      • Four 、 summary


One 、 background

In the actual development process , You will often encounter many identical or very similar operations , At this time , Code that implements similar operations can be encapsulated as functions , Then call the function where you need it . This can not only realize code reuse , It can also make the code more organized , Increase code reliability . Now let's introduce python Import related contents of the function module of .


Two 、 Module import

1. Import the entire module

Use import The basic format for importing the entire module is as follows :

import Module name [as Alias ]

import math # Import standard library math
math.sqrt(4) # seek 4 Square root 

After importing the module in this way , When calling a function in a module, you need to prefix the function name with the module name :

> Module name . Function name

for example : When the module name is very long , You can use statements “import Module name as Alias ” Set an alias for the imported module , And then use “ Alias . Function name ” Call function in the way of . for example :

>>>import random as r
>>>r.randint(1,100)
47

2. Import specific functions

When we only need to use a function in a module , You can import only specific functions .

from Module name import Function name [as Alias ]

When the function is called , You don't need to use the module name as a prefix . for example :

>>>from math import sqrt # Only the specified functions in the module are imported 
>>>sqrt(9) # Call function , seek 9 Square root 
3.0
>>>from random import randint as r # Assign an alias to the imported function r
>>>r(1,10) # Call function , get [1,10] Random integer of interval 
10

3. Import all functions in the module

Use the asterisk “*” You can import everything in the module ( Including functions and variables ).

from Module name import *

This is a “ Import specific functions ” An extreme case of usage , You can import all the contents of the module at once . for example :

>>>from math import * # Import standard library math Everything in 
>>>pi # constant π
3.141592653589793
>>>log2(8) # Calculate with 2 The value of the base 
3.0
>>>sqrt(16) # Calculation 16 Square root 
4.0

3、 ... and 、 Reference resources

1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial


Four 、 summary

The above is about Python Function module of the import related knowledge , You can refer to it , If you think it's good , Welcome to thumb up 、 Collection 、 Looking at , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved