Python_ Common built-in modules

Source: Internet
Author: User
Tags month name timedelta

One, Time module (Timing module):

Three ways to represent time:

In Python, there are usually three ways to represent time: timestamp, tuple (struct_time), formatted time string:

(1) timestamp (timestamp): Typically, a timestamp represents an offset that is calculated in seconds, starting January 1, 1970 00:00:00. We run "type (Time.time ())" and return the float type.

(2) formatted time string (format string): ' 1995-10-04 '

%y Two-digit year representation (00-99)%Y Four-digit year representation (000-9999)%m Month (01-12)One day in%d months (0-31)%H 24-hour hours (0-23)%I 12-hour hours (01-12)%M minutes (00=59)%s seconds (00-59)%a local simplified week name%A Local Full week name%b Local simplified month name%B Local Full month name%c Local corresponding date representation and time representation%j Day of the Year (001-366)%p equivalent of local a.m. or P.M.%u days of the year (00-53) Sunday for the beginning of the week%w Week (0-6), Sunday for the beginning of the week%W number of weeks in the year (00-53) Monday for the beginning of the week%x Local corresponding date representation%X Local corresponding time representation%Z name of the current time zonePercent% of the number itself
Time Formatting symbols

(3) tuple (struct_time): Struct_time A total of 9 elements total nine elements: (years, months, days, hours, minutes, seconds, the first week of the year, the day of the year, etc.

1 Import Time2 3 4 Print(Time.asctime ())#format of return time: Sun may 21:31:15 20185 Print(Time.time ())#return timestamp: 1526823135.95312056 Print(Time.gmtime ())#the same as the time.localtime () effect; Returns the struct time object format for local times: Time.struct_time (tm_year=2018, tm_mon=5, tm_mday=20, tm_hour=13, Tm_min=33, tm_sec=33, tm_wday=6, tm_yday=140, tm_isdst=0)7 Print(Time.localtime ())8 Print(Time.strftime ("%y-%m-%d"))#returns the current time in a custom format: 2018-05-209 Print(Time.strptime ("2018-05-20","%y-%m-%d"))#Convert The specified time to a struct the Date object format: Time.struct_time (tm_year=2018, tm_mon=5, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_ Wday=6, tm_yday=140, Tm_isdst=-1)

1 Importdatetime2 3 Print(Datetime.datetime.now ())#return Current time: 2018-05-20 21:42:57.4867704 Print(Datetime.date.fromtimestamp (Time.time ()))#convert timestamp to TIME format: 2018-05-205 Print(Datetime.datetime.now () + Datetime.timedelta (3))#return time on current date + 3 days6 Print(Datetime.datetime.now () + Datetime.timedelta (-3))#return time on current date-3 days7 Print(Datetime.datetime.now () + Datetime.timedelta (hours= 3))#return time at current time + 3 hours8 Print(Datetime.datetime.now () + Datetime.timedelta (minutes=30))#return time at current time + 30 minutes
Second, random module (stochastic number module):
1 ImportRandom2 3 Print(Random.random ())#returns a decimal between 0 and 1:0.92280974804305174 Print(Random.uniform (1,3))#returns any floating-point number for a specified range5 Print(Random.randint (1,5))#returns an integer from 1 to 56 Print(Random.randrange (10))#returns an integer7 Print(Random.choice ("Hello"))#returns any element in a string8 Print(Random.sample ("Hello", 3))#returns any element of a specified number as a list9 Ten #Generate random numbers One  Arandomnum="" -  forIinchRange (5): -  theAddnum=random.choice ([Random.randint (1.,10), Chr (Random.randint (65,90))]) -randomnum+=Str (addnum) -  - Print(Randomnum)

Python_ Common built-in 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.