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 .
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 .
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
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
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
1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial
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 .