python commonly used modules: Module Exercises

Original link: http://www.cnblogs.com/wuzhengzheng/p/9775173.html
Today's job:
 
1. Description of
 
what is a module
  Some will function-module enclosed within a file, a 'file name .py' name to "import file name" invoke
 
module which sources
   custom, built-in, the DLL compiler, the packet
request module format which
  Naming format "filename + .py" call format has "import function" or "from xxx import xxx"
 
2. Define a cuboid module, the module has three variable length (long) Width (Wide) high (High), numeric values, there is a return value of the method circumference perimeter, an area is the surface area of ​​the return value of the method
wide=3

high=4

def perimeter():
    return (long+wide+high)*4

def area():
    return long*wide*high

 

3. Define a user file stu1.py, print the document length and breadth of the cuboid, and obtaining the perimeter and the surface area, printed
from cuboid import long,wide,high
print(long,wide,high)
import cuboid
res=cuboid.perimeter()
res1=cuboid.area()
print(res,res1)

 

 
Alias ​​module simplicity of introducing cuboid modules 4. stu2.py file, using the alias in question 3 to complete the operations performed
import cuboid as cub
print(long,high,wide)
res=cub.perimeter()
res1=cub.area()
print(res,res1)

 

5. There are three modules sys, time, place, three modules can be imported in run.py file? There are several ways? Are written
A: sys and time are built-in functions, call the conflict, can only place calls normally, if sys, time, place module is not built-in function, can be in two ways: import sys, time, place and the module into three the bag calls from models import *
 
6. The combination of 2,3,4 complete title from ... import ... case, perform the same function
from cuboid import long,wide,high
print(long,wide,high)

from cuboid import perimeter
print(perimeter())

from cuboid import area
print(area())

7. Comparison of summary import from ... import ... and their advantages and disadvantages

import
advantages: a relatively independent module and executable file, calls do not conflict
Cons: need to use the module name + function for each call

form ... import ...
Advantages: Each call does not need to add the module name, save memory
Disadvantages: easily with the duplicate file names in the implementation of conflict

Reproduced in: https: //www.cnblogs.com/wuzhengzheng/p/9775173.html

Guess you like

Origin blog.csdn.net/weixin_30438813/article/details/94880640