Add half of the module on the basis of the random module

A module of four forms

1.1 What is a module

It is a series of functional module assembly, and a function is a function of the aggregate, and therefore the module can be seen as a function of a pile of aggregate. Py inside a file you can put a bunch of functions, so a py file can be seen as a module. If this file is named py module.py, the module name is module.

1.2 modular form:

1, custom module : is the py files you created, which writes a bunch of functions, then the file is called a custom module, namely the use of written python .py file.

2, third-party modules : the need to install their own, there are 130,000 third-party libraries, omnipotent

3, built-in module : python interpreter comes, no need to install

4, Package : Contains __inti__files .py folder, a special module (solve a problem)

1.3 Why use module

  1. With a third party or the built-in module is an ism, it can greatly enhance the development efficiency.
  2. Custom module, the procedures used in our own public function, write a python file, then the component parts of the program can reference the ability to customize the module by way of import.

Two, import and from ... import ...

2.1 import module name

python as a cell phone, pip is the application of the housekeeper, time application is an application tube at home, use it import

import time
time.time()

import module for the first time to import three things happen:

1, generates a namespace called time in memory

2, run time.py file, then the name of the file into space in the time.py name in the time space

3, the time point import namespace and from ... import.py (current file into time module) namespace

Before repeating the import module will add a direct reference to the creation of good too, do not repeat the module file

Use import time import time, use only time. Method name (), method name can not be directly

2.2 from ... import ... module name

This method is not recommended

from time import gmtime
print(gmtime())

from ... import ... for the first time to import 3 module things happen:

1, generates a namespace called time in memory

2, run time.py file, then the name of the file into space in the time.py name in the time space

3, the pointing method gmtime import from ... and the import.py (current file into time module) namespace, the method can not add any prefix used directly

  • Pros: do not add a prefix code more streamlined
  • Disadvantages: easy to perform name conflicts in the file name with the current namespace

Import feature all the content in the file:

__all__Difference in both the module name

from test import *   # __all__限制了 from test import *(导入全部)
from test import f3  # __all__不会限制
import test          # __all__ 不会限制

2.3 import and the similarities and differences from ... import ...

Same point:

1, both of which can execute the corresponding file module, both modules will have a namespace

2, when the two calls, you need to find time to go define the scope of the relationship, regardless of the calling location

difference:

import need to add the prefix; from ... import ... no need to add the prefix

Third, the circulation import questions

#m1.py
from m2 import y
x = 10

#m2.py
from m1 import x
y = 20

Process: m1.py -> m2.py -> m1.py ... to enter an infinite loop circulation import

Results: X / Y has not been generated in this process

Solution one: import statements written on the bottom line

#m1.py
x = 10
from m2 import y


#m2.py
y = 20
from m1 import x

Process: m1.py -> X = 10 -> m2.py -> 20 is Y = -> m1.py -> X = 10

Results: X = 10 -> 20 is Y = -> X = 10

Solution 2: the import statements written on the inside function

#m1.py
def f1():
    from m2 import y
    print(y)
x = 10

#m2.py
def f1():
    from m1 import x
y = 20 

Process: F1 () -> m2.py -> Y = 20 is

Results: Y = 20 is

Fourth, the module search path

1, start looking for the memory

from m2 import y
print(y)
import time
time.sleep(10)  # 10s内删除了m2

from m2 import y
print(y)

2, and then looking from the built-in module

from time import time
print(time)

3, and then find the custom module

That is, to find the file that you create the module

4, finally found an environment variable (the future implementation of the project file must get a link variable)

import sys
print(sys.path)  # 环境变量,模块就是在某个路径中,需要添加路径,在这里找
sys.path.append(r'D:\上海Python11期视频\python11期视频\day 16')
# del sys.path[1]
print(sys.path)

import testt
testt.f1()

Two uses five, python files

1, as a module file (m1.py call m2.py, when you run m1.py, m2.py is the template file), you can have multiple modules

2, as the executable file (currently running file), only one executable file

3, executable file and file module is relative

# m1.py

x = 10
if __name__ == '__main__':
    print(x)
    
# m2.py

import m1

When running m1.py, i.e. m1.py as the execution file, so __name__ __ == main, print x

When running m2.py, namely m1.py as a template file, this time __name__ __ == M1 , IF condition is not satisfied, an error

if __name__ == '__main__':It is actually a ifjudgment, or a filter. You can use the Tab key to quickly hit + main out

Six, random module

Use of the random module

  • grasp

    1, random.random () between a randomly generated tree 0--1

    import random
    
    print(random.random())  #随机生成一个大于0小于1之间的小数
    ------------------------------------------------------------
    0.6331352272344526

    2, the random.randint () : integer randomly generated within a range of values includes two

    import random
    
    print(random.randint(1,5))   #随机生成一个大于等于1且小于等于5之间的整数
    
    ------------------------------------------------------------
    5

    3, random.shuffle (): upset container class elements ---> lists and dictionaries

    import random
    
    list = [1,3,5,7,9]
    random.shuffle(list)   #打乱list的顺序,相当于“洗牌”
    print(list)
    ------------------------------------------------------------
    [9, 5, 3, 7, 1]
  • To understanding

    1, random.randrange (): Generate a random integer in the given range (care regardless of tail)

    import random
    #随机生成一个大于等于1且小于3之间的整数
    # print(random.randrange(1,3))
    ------------------------------------------------------------
    1

    2, random.uniform (): randomly generated within a given range (in spite of both the head and tail) fractional

    import random
    #随机生成一个大于1小于3的小数
    print(random.uniform(1,3))
    ------------------------------------------------------------
    2.7201432742975458

    3, random.choice (): random remove any element in the list in a

    import random
    #随机生成列表内的任意一个元素
    print(random.choice([1,'23',[4,5]]))
    ------------------------------------------------------------
    1

    4, random.sample (): Any combination of elements taken randomly list of n elements

    import random
    #随机生成列表元素任意n个元素的组合
    print(random.sample([1,'23',[4,5]],3)) #这个3就代表n
    ------------------------------------------------------------
    [[4, 5], '23', 1]

Guess you like

Origin www.cnblogs.com/zhuangyl23/p/11360897.html