The path of Python development--8

Source: Internet
Author: User
Tags local time

One, Module 1. module

(1) Definition: A. py file is a module

(2) Reason: In order to prevent the program code getting longer, the functions are grouped into different folders.

(3) Advantages: Improve the maintainability of the code, the module can be written to be referenced by others, you can also refer to other modules; You can avoid conflicting variable names and function names

(4) Module type: A total of three kinds of ①python standard library; ② third-party module; ③ Application custom Module

2. Module Import method

The essence of module import: by Sys.path find the function to import and execute the script, the (1) method loads the variable name into the variable space, (2) loads the variable name in

(1) Import statement

Import Timetime.sleep (2) Print ("OK")
View Code

(2) From...import statement

 from Import  = cal ($)print(res)
View Code

(3) from...import* statement

 from Import *# does not recommend this method, it is possible to overwrite the function defined by itself (function is variable)res1 =cal (2,3 = sub (4,2) Print (Res1,res2)
View Code3. Package

In order to avoid the module conflict, according to the directory to organize the module, this directory will be delivered, the package directory will have a __init__ file, which is the difference between the package and the normal folder

All modules below the package will not conflict as long as the top-level package name does not conflict

Multilayer Package Reference

 from Import  = cal ($)print(res)
View Code4. Test code
if __name__= ="__main__":

Two features: 1. Display "__main__" in the execution code; 2. Display the name of the file in the calling module

(1) function one: When debugging the code, add, when the module is called from another. py file, the contents of debugging are not displayed

def cal (x, y  ): return (x+y) def Sub (x, y    ): return (xy) if __name__= ="__main__"    :print ("  OK")
View Code

(2) function Two: In the execution of code, to prevent their own logic code is called by others

Second, time module 1. Timestamp
Import  Time Print (Time.time ()) # Timestamp number of seconds, starting from the early 1970, the birth time of Unix
View Code2. Structured time
Import  Time Print (Time.localtime ()) # local time Print (Time.gmtime ()) # UTC Time
View Code

Convert structured time to timestamp

Import  Time Print (Time.mktime (Time.localtime ()))
View Code

Convert structured time to string time

Import  Time Print (Time.strftime ("%y-%m-%d  %x", Time.localtime ()))
View Code

3. String time
Import= time.localtime ()print(time.asctime (t))Print (Time.ctime (1124324))
View Code

Convert string time to structured time

Import  Time Print (Time.strptime ("2018-5-11 21:01:26","%y-%m-%d%x"))
View Code4.time.sleep () and Time.clock ()

Time.sleep () Delays the specified time run, in seconds

The Time.clock () function returns the current CPU time in seconds calculated as a floating-point number. It is more useful than time.time () to measure the time spent on different programs. It is important to note that the meanings are different on different systems. On a UNIX system, it returns "process time", which is a floating-point number (timestamp) in seconds. In Windows, the first call returns the actual time that the process is running. The second subsequent call is the elapsed time since the first call to the present. (actually based on QueryPerformanceCounter () on WIN32, which is more accurate than milliseconds)

Import  Time Print (Time.clock ()) Time.sleep (2) Print (Time.clock ())
View Code5.datetime Module

Show Current Time

Import datetime Print (Datetime.datetime.now ())
View CodeThird, random module

The random module mainly has the following functions

ImportRandomPrint(Random.random ())#randomly generate a floating-point number less than 1Print(Random.randint (1,3))#randomly generates an integer that can be taken to 3Print(Random.randrange (1,3))#randomly generates an integer that takes less than 3Print(Random.choice ([11,22,33,44]))#randomly select a number from a listPrint(Random.sample ([11,22,33,44],2))#randomly select two numbers from a listPrint(Random.uniform (1,4))#generate floating-point numbers within a specified rangeret = [1,2,3,4,5]random.shuffle (ret)#Scramble OrderPrint(ret)
View Code

Generate Verification Code

Import Random def V_code ():     ""     for  in range (4):        = Random.randint (0,9)        = Chr (Random.randint (65,90))        = Str (random.choice ([Num,letter])        + = s    return  ret Print (V_code ())
View Code

The path of Python development--8

Related Article

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.