Python Basics (10)--modules

Source: Internet
Author: User
Tags local time string format

1. Module Introduction

Module that implements a code set of a function with a single piece of code

Like functional programming and process-oriented programming, functional programming accomplishes a function, and other code is used to invoke it, providing reusability of code and coupling between the code. For a complex function, multiple functions may be required to complete (the function can be in a different. py file), and the Diane Collection of N. py files is called a module

Module sub-three types

1. Custom Modules

2. Built-in standard module (also known as standard library)

3. Open source module

2. Common Modules

Time Module

Time.time ()

Import  Time Import datetime Print (Time.time ())     # returns the timestamp of the current time 1511775019.4672794
#时间戳计算机时间的一种表示方式 refers to the total number of seconds since GMT January 01, 1970 00:00 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) to the present.

Time.ctime ()

Print # Convert timestamp to string format Mon Nov 27 17:32:48 2017 The default is the timestamp of the current system time Print # CTime can receive a timestamp as a parameter that returns the string form of the timestamp Mon Nov 27 16:32:48
#输出
#Mon Nov 27 17:32:48 2017
#Mon Nov 27 16:32:48 2017

Time.gmtime ()

Print # Convert timestamp to struct_time format, default is current system timestamp Print (Time.gmtime (Time.time () -3600)) # Output # time.struct_time (tm_year=2017, tm_mon=11, tm_mday=27, tm_hour=9, tm_min=38, tm_sec=2, tm_wday=0, tm_yday=331, tm_ isdst=0)#time.struct_time (tm_year=2017, tm_mon=11, tm_mday=27, Tm_hour=8, tm_min=38, Tm_sec=2, TM _wday=0, tm_yday=331, tm_isdst=0)

Struct_time format is also a form of time representation, in fact a bit like a list or tuple form

There are nine elements, the table ID, the struct_time of the same timestamp because the time zone is different in order

1. Year Tm_year

2. Month Tm_mon

3. Day Tm_mday

4. Hours Tm_hour

5. Min tm_min

6. Seconds tm_sec

7. Zhou Tm_wday, note that the week is counted from 0, that is, Monday is 0

8. The day of the year Tm_yday

9. Whether it is Xia Lingzhi TM_ISDST

Time.localtime ()

Print (Time.localtime ()) # The same time stamp is converted to struct_time, except that the local time is displayed, Gmtime shows the standard Time (Galini time) # time.struct_time (tm_year=2017, tm_mon=11, tm_mday=27, tm_hour=17, tm_min=43, tm_sec=16, tm_wday=0, tm_yday=331, TM _isdst=0)

Time.mktime ()

Print # Convert struct_time time format to timestamp # Output 1511775894.0

Time.strftime ()

Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())) # Convert the struct_trime time format to a custom string format # Output 2017-11-27 17:46:23 # " %y-%m-%d%h:%m:%s" is our custom string, "%Y a bit like a placeholder

Time.strptime ()

Print (Time.strptime ("2017-11-27""%y-%m-%d")) # In contrast to strftime, format the string as Struct_time format # output Time.struct_time (tm_year=2017, tm_mon=11, tm_mday=27, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=331, TM _ISDST=-1)
#说明: The first parameter is the string form of the time, the second argument is the format of the first parameter, the format to correspond to the string, and the default is 0 seconds, can be omitted, but the month and day cannot be saved

Time.asctime ()

Print (Time.asctime (Time.localtime ())) # Convert struct_time to string form # output Mon Nov 17:55:05

DateTime module

Datetime.date: A class that represents a date, commonly used properties are Year,month,day

Datetime.time: The class that identifies the time, the commonly used properties are Hour.minute.second.microsecond

Python Basics (10)--modules

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.