(D13) Random Module-Package

Source: Internet
Author: User

Stochastic module: Random

Role:
For simulating or generating a module that outputs immediately.

Usage:

import random as R
Name of function Description
R.random () Returns a stacked real number between (0,1)
R.unifrom (A, B) Returns the random real number between [A, b]
R.randin (A, B) Returns an integer within the range of [a, b]
R.randrange Returns the random number in range (Start,stop,step)
R.chise (seq) Returns any element from the sequence
R.shuffle (Seq[random]) Randomly specify sequence order (scrambled order)
R.sample (Seq,n) Select the non-repeating elements of n random amounts from a sequence
How to Import

importStatement
from importStatement
from import *Statement
Example:

def myfac(n):    ‘‘‘这是函数的文档字符串‘‘‘    print("正在计算n的阶乘")def mysum(n):    print("!!!正在计算1 + 2 + 3 + .... + %d的和" % n)name1 = "Audi"name2 = "tesla"print("mymod.py 文件被加载并导入")print("当前我这个模块在:", __file__)print("mymod.py模块的模块名为:", __name__)if __name__ == ‘__main__‘:    print("您正在用 python3 把 mymod.py当主模块运行")else:    print("此模块是被import 导入运行的")

The import statement finds the path order of the module files:
Import Module name # Where to find this "module name. Py"

1. 搜索内建模块2. 搜索程序运行时的路径(当前路径)3. sys.path 提供的路径sys.path 是一个列表,内部存入的都是模块的搜索路径    >>> import sys    >>> print(sys.path)

The process by which the module is imported and executed:

    • Find the module name by searching the relevant path first. py
    • Determine if there is a PYC file for this module. If the. pyc file is more than. py
      The new file is loaded directly with the. pyc file
    • If the. pyc file is older than the. py file, it is generated with the module name. py. pyc
      file, and load execution
      Compilation of modules
      Compilation Interpretation execution
      mymod.py-----> Mymod.pyc--------> Python3
Properties of the module:

__doc__ Property: The document string used to bind the module
__file__ Property: File path name for binding module
__name__ attribute: Used to record the name of the module itself

Example:

print("mymod.py 文件被加载并导入")print("当前我这个模块在:", __file__)print("mymod.py模块的模块名为:", __name__)if __name__ == ‘__main__‘:    print("您正在用 python3 把 mymod.py当主模块运行")else:    print("此模块是被import 导入运行的")
__all__ List of modules

Role:
Import only attributes from the __all__ list when the FROM MOD import * statement is imported

Description
The __all__ list is a list of strings that hold exportable properties
Example:

__all__ = [‘f1‘, ‘name1‘]def f1():    f2()    f3()def f2():    passdef f3():    passname1 = ‘aaaaa‘name2 = ‘bbbbb‘

Hidden properties of the module:
Properties that begin with ' _ ' in the module are not imported when using from mod import *, also known as hidden properties

(Module Package) Package

A package is a way to group the organization of a module folder
Role:
Classify and manage a range of modules to help prevent naming conflicts
Can load one or part of the module when needed
Example:

mypack/     __init__.py     menu.py     games/         __init__.py         contra.py         supermario.py         tanks.py     office/         __init__.py         excel.py         word.py

Import of packages:

import rules for the same module  
    • Import package name [as package alias]
    • Import package name. module name [as module new name]
    • Import package name. Child Package name. module name [as module new name]
    • From Package name import module name [as module new name]
    • From package name. Sub-package Name import module name [as module new name]
    • From package name. Child Package name. Module Name Import property name [as property new name]
    • From Package name Import *
    • From package name. Module Name Import *
      ...
      __init__ file
    • Files that must exist in a regular package
    • is automatically called when the package is loaded
      Role:
      Write the contents of this package
      Fill in a document string within a package
      Some other modules that this package depends on can be loaded in __init__.py

List of __all__ in all.py
Role:
Used to record which modules or packages need to be imported
When the From package import * statement is imported into the module, only all modules or sub-packages in the __all__ are found
Description
The __all__ list also works only in the From MoD import *

Relative imports of packages:
The relative import of a package refers to the mutual import of modules within a package
Grammar:
From relative path package or module import property or module name
Or
From relative path package or module import *
Relative path:
In the from import statement and from IMOPRT * statements, you can make
With relative paths
-

    • Represents the current directory
      • Represents the previous level of the directory
        • Represents the top level two directory
          • And so on
            Note: You cannot exceed the external package when importing

Example:

# file : mypack/games/contra.pydef play():    print("正在玩魂斗罗....")def gameover():    # 调用mypack/menu.py里的show_menu    # 1. 用绝对导入方式导入    # from mypack.menu import show_menu    # show_menu()     # 2. 用相对导入方式导入    from ..menu import show_menu    show_menu()    # 调用mypack/games/tanks.py 里的play()    # 1. 绝对导入    # from mypack.games.tanks import play    # 2. 相对导入    # from .tanks import play    from ..games.tanks import play    # from ...mypack.games.tanks import play # 出错    play()print("魂斗罗模块被加载")

Practice:

      1. Simulated bucket landlord licensing, a total of 54 card
        Spades (' \u2660 '), plum (' \u2663 '), Red Peach (' \u2666 ')
        Block (' \u2665 ')
        A2-10jqk
        King, Xiao Wang
        All three, each issued 17 cards, cards left three
        Enter, print 17 cards for the 1th person
        Enter, print 17 cards for the 2nd person
        Enter, print 17 cards for the 3rd person
        Input enter, display three cards
        Ideas:
        #1. Loop first to put all poker in a list
        #2. Import random.shffle () scrambled order
        #3. Slice 17 pieces separately and the last three cards

(D13) Random Module-Package

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.