Time & amp; datetime and random in common Python modules, and time in python modules

Source: Internet
Author: User
Tags month name timedelta

Time & datetime and random in common Python modules, and time in python modules

This section outlines:

I. Module introduction:

Module, which implements a set of code for a specific function with a single worker code.

Similar to functional programming and process-oriented programming, functional programming completes a function. Other code can be called to provide code reusability and code coupling. For a complex function, multiple functions may be required.

(Functions can also be in different. py files). A set of code consisting of n. py files is called a module.

For example, OS is a system-related module, and file is a file-related module.

There are three modules:

① Custom Module

② Built-in standard module (also known as standard library)

③ Open-source module

Ii. time & datetime Module

1 import time 2 import datetime 3 4 print (time. clock () # returns the processor time. 5 print (time) has been deprecated since 3.3. process_time () # returns the processor time. 6 print (time. time () # returns the current system timestamp 7 print (time. ctime () # output Tue Jan 26 18:23:48 2016, current system time 8 print (time. ctime (time. time ()-86640) # convert the timestamp to the string format 9 print (time. gmtime (time. time ()-86640) # convert the timestamp to the struct_time format 10 print (time. localtime (time. time ()-86640) # converts the timestamp to struct_time format, but returns the local time 11 print (time. mktime (time. localtime () # and time. the localtime () function is opposite. struct_time format is converted back to timestamp format 12 # time. sleep (4) # sleep13 print (time. strftime ("% Y-% m-% d % H: % M: % S", time. gmtime () # convert struct_time format to the specified string format 14 print (time. strptime ("2016-01-28", "% Y-% m-% d") # convert the string format to struct_time format 15 16 # datetime module17 18 print (datetime. date. today () # output format 2016-01-2619 print (datetime. date. fromtimestamp (time. time ()-864400) # convert the timestamp to the date format 20 current_time = datetime. datetime. now () #21 print (current_time) # output 19:04:30. 33593522 print (current_time.timetuple () # returns the struct_time format 23 24 # datetime. replace ([year [, month [, day [, hour [, minute [, second [, microsecond [, tzinfo]) 25 print (current_time.replace (, 12) # output 19:06:24. 074900, returns the current time, but the specified value will be replaced with 26 27 str_to_date = datetime. datetime. strptime ("21/11/06", "% d/% m/% y % H: % M") # convert string to date format 28 new_date = datetime. datetime. now () + datetime. timedelta (days = 10) # add 10 days 29 new_date = datetime. datetime. now () + datetime. timedelta (days =-10) # Minus now 10 days 30 new_date = datetime. datetime. now () + datetime. timedelta (hours =-10) # Minus 10 hours now 31 new_date = datetime. datetime. now () + datetime. timedelta (seconds = 120) # more than now + 120s32 print (new_date)

The format is as follows:

Directive Meaning Notes
%a Locale's abbreviated weekday name.  
%A Locale's full weekday name.  
%b Locale's abbreviated month name.  
%B Locale's full month name.  
%c Locale's appropriate date and time representation.  
%d Day of the month as a decimal number [01,31].  
%H Hour (24-hour clock) as a decimal number [00,23].  
%I Hour (12-hour clock) as a decimal number [01,12].  
%j Day of the year as a decimal number [001,366].  
%m Month as a decimal number [01,12].  
%M Minute as a decimal number [].  
%p Locale's equivalent of either AM or PM. (1)
%S Second as a decimal number []. (2)
%U

Week number of the year (Sunday as the first day of the week) as a decimal number [].

All days in a new year preceding the first Sunday are considered to be in week 0.

(3)
%w Weekday as a decimal number [0 (Sunday), 6].  
%W

Week number of the year (Monday as the first day of the week) as a decimal number [].

All days in a new year preceding the first Monday are considered to be in week 0.

(3)
%x Locale's appropriate date representation.  
%X Locale's appropriate time representation.  
%y Year without century as a decimal number [].  
%Y Year with century as a decimal number.  
%z

Time zone offset indicating a positive or negative time difference from UTC/GMT of the form + HHMM or-HHMM,

Where H represents decimal hour digits and M represents decimal minute digits [-23:59, + 23:59].

 
%Z Time zone name (no characters if no time zone exists ).  
%% A literal'%'Character.

Ii. random Module

1. Random Number

Eg:

1 import random2 print random.random()3 print random.randint(1,2)4 print random.randrange(1,10)

2. Generate a random Verification Code

Eg:

 1 import random 2 checkcode = '' 3 for i in range(4): 4     current = random.randrange(0,4) 5     if current != i: 6         temp = chr(random.randint(65,90)) 7     else: 8         temp = random.randint(0,9) 9     checkcode += str(temp)10 print checkcode

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.