Chapter VI Common Module (2): python common module (time, datetime, random)

Until last one, we made a brief introduction to the knowledge of python modules. Next we introduce the common python modules.

6.3.1 time, datetime module

  • use:
    • Show time
    • Time Conversion
    • Time of operation
1. time
import time
  1. time.time ([secs]) Returns the current time timestamp

    Timestamp: 1970 to the current second timer unit, the previous time can be specified with a negative

  2. time.gmtime ([secs]) to convert a timestamp truct_time world standard time zone (a tuple, a total of nine elements).

  3. time.localtime ([secs]) to convert a timestamp of the current time zone struct_time. secs parameter is not provided, the current system places on the time
    time.struct_time(tm_year=2019, tm_mon=6, tm_mday=27, tm_hour=15, tm_min=0, tm_sec=2, tm_wday=3, tm_yday=178, tm_isdst=0)

  4. time.mktime (t) to convert a timestamp struct_time

  5. time.sleep (secs) thread is suspended specify the time, in seconds

  6. time.asctime ([t])  
    the tuple indicates a time or struct_time expressed as: 'Sun Oct 1 12:21:11 2018' . If there are no parameters, it will be time.localtime () as a parameter.

  7. time.ctime ([secs]) to a converted time stamp: 'Sun Oct 1 12:21:11 2018'. If there are no parameters, it will be time.localtime () as a parameter.

  8. time.strftime (format [, t]) represents a tuple or the time of conversion to struct_time formatted time string. If you do not specify the parameters t, will be time.localtime () as a parameter.

    The format can be written references
    time.strftime('%Y-%m-%d %H:%M:%S')

  9. time.strptime (string [, format]) represents the time a string the string is converted to struct_time. Reverse corresponds strftime () operation.
    time.strftime('%Y-%m-%d %H:%M:%S')

2. datetime

Over time module interface is more intuitive and easier to call

Advantages: Can be time operation

import datetime
  1. datetime.date()

  2. datetime.time()

  3. datetime.datetime()

  4. datetime.timedelta ()

  5. datetime.replace()

Common methods:

import datetime

d = datetime.datetime.now()  # 取得当前时间的datetime日期类型数据

d + datetime.timedelta(seconds=10)  # 时间的相加,最大单位是天

d.replace(year=2018,month=10)  # 替换时间

6.3.2 random module

  1. random.randint (a, b) taking a random number between ab, a and b range comprising

    random.randrange (a, b) and the randint () is similar to, but not including the range of b

  2. random.random ()  
    Random floating point

  3. random.choice (str) returns a random character string given in

  4. random.sample (str, n) returns a list of n random characters. It can be used for verification code:
    `` `Python
    Import Random
    Import String

    s = string.ascii_lowercase + string.digits + string.punctuation

    a = ''.join(random.sample(s,5))

    print(a)

    ```

  5. random.shuffle () shuffle. Upset the order of the list, and re-assigned to the list. Return None

We can also look more random:
`` `Python
Import Random
Import String

s = string.ascii_lowercase + string.digits + string.punctuation

s = list(s) 
random.shuffle(s)
s = ''.join(s)

a = ''.join(random.sample(s,6))

print(a)
```

Guess you like

Origin www.cnblogs.com/py-xiaoqiang/p/11110849.html