04:python Common Modules

Source: Internet
Author: User

1.1 Time Module times () and DateTime ()

1. Important functions in the time () module

Function

Describe

Asctime ([tuple])

Convert a time tuple to a string

LocalTime ([secs])

Converts the number of seconds to a date tuple (convert the cost country time zone instead of UTC time zone)

Mktime (tuple)

Convert a time tuple to local time

Sleep (secs)

hibernate (don't do Anything) secs seconds

Strptime (string[, format])

Resolving a string to a time tuple

Time ()

Gets the current time stamp

Time.gmtime ()

convert Time to UTC format the tuple format

2. Time () module format conversion

3. Time () module timing Conversion

1. Timestamp after January 1, 1970 seconds, i.e.: Time.time ()

2. Formatted string 2014-11-11 11:11, i.e.: Time.strftime ('%y-%m-%d ')

3. Structured time tuple contains: year, day, week, etc. time.struct_time is: time.localtime ()

Import TimePrint(Time.time ())#time stamp: 1511166937.2178104Print(Time.strftime ('%y-%m-%d'))#formatted string: 2017-11-20Print(Time.localtime ())#structured Time (tuple): (tm_year=2017, tm_mon=11 ...)Print(Time.gmtime ())#convert time to a tuple format in UTC format: (tm_year=2017, tm_mon=11 ...)#1. Convert structured time to timestamp: 1511167004.0Print(Time.mktime (Time.localtime ()))#2. Convert the lattice string time to a structured time tuple: (tm_year=2017, tm_mon=11 ...)Print(Time.strptime ('2014-11-11','%y-%m-%d'))#3. Structured time (tuple) converted to string time: 2017-11-20Print(Time.strftime ('%y-%m-%d', Time.localtime ()))#Default Current Time#4. Convert structured time (tuple) to English string time: Mon-16:51:28Print(Time.asctime (Time.localtime ()))#5. Turn timestamp into English string time: Mon-16:51:28Print(Time.ctime (Time.time ()))
time () module timing conversion

4, CTime and asctime differences

1) CTime The time stamp passed in seconds is converted to format time

2) Asctime incoming time tuple converted to formatted time

Import= time.time ()print(t1)               #1483495728.4734166Print (Time.ctime (t1))   #Wed Jan  4 10:08:48t2 = time.localtime ()  Print(T2)               #time.struct_time (tm_year=2017, Tm_mon=1, tm_mday=4, tm_hour=10, print ( Time.asctime (T2)) #Wed Jan  4 10:08:48
CTime and Asctime differences

5. DateTime

Importdatetime#1, Datetime.datetime get the current timePrint(Datetime.datetime.now ())#2, get three days after the timePrint(Datetime.datetime.now () +datetime.timedelta (+3))#3. Get the time three days agoPrint(Datetime.datetime.now () +datetime.timedelta (-3))#4, get three hours after the timePrint(Datetime.datetime.now () +datetime.timedelta (hours=3))#5. Get the time three minutes agoPrint(Datetime.datetime.now () +datetime.timedelta (minutes = 3))ImportdatetimePrint(Datetime.datetime.now ())#2017-08-18 11:25:52.618873Print(Datetime.datetime.now (). Date ())#2017-08-18Print(Datetime.datetime.now (). Strftime ("%y-%m-%d%h-%m-%s"))#2017-08-18 11-25-52
datetime usage1.2 Random () module

1. Random () module common functions

Some important functions in the random module

Function

Describe

Random ()

Back to 0<n<=1

Getrandbits (N)

return n random bits in a long shape

Uniform (A, B)

Returns the random real number n, where a<=n<=b

Randrange ([start], stop, [step])

Returns the random number in range (Start,stop,step)

Choice (seq)

Returning random elements from a sequence seq

Shuffle (seq[, Random])

Specify sequence seq in place (the sequence list becomes unordered: Shuffle)

Sample (Sea, N)

Select n Random and independent elements from a sequence seq

ImportRandom#⒈ random integers:Print(Random.randint (0,99))#randomly select an integer between 0-99Print(Random.randrange (0, 101, 2))#randomly select an even number between 0-101#⒉ Random floating point number:Print(Random.random ())#0.972654134347Print(Random.uniform (1, 10))#4.14709813772#⒊ random characters:Print(Random.choice ('ABCDEFG'))#CPrint(Random.sample ('Abcdefghij', 3))#[' J ', ' F ', ' C ']
example of using the random () function

2. Use random to implement four-bit verification code

Import" for" in range (4):    = Random.randrange (0,4)     if current = = I        := Chr (Random.randint (65,90))    #65,90 denotes all uppercase letters     Else:        = Random.randint (0,9)    + = STR (TMP)  Print(checkcode)                            # run Result: 851K
using a For loop implementation
Import Random Import  = string.ascii_letters += random.sample (str_source,7)#[' i ', ' Q ', ' u ', ' u ', ' A ' ', ' 0 ', ' 9 ']print'. Join (str_list)#iQUuA09 Print (str_final)            # running Result: jkfu2ed
using the Random.sample implementation
>>> string.digits'0123456789'>>> string.ascii_ lowercase'abcdefghijklmnopqrstuvwxyz'
1.3 OS Module

04:python Common Modules

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.