Standard library datetime requires importing a datetime module: Import datetime module: Processing of date, time, timestampclass method for D atetime Today ()Now (Tz=none) returns the DateTime object of the current time, time to microseconds, if TZ is the None return value and today ()UtcNow () Fromtimestamp (Timestamp,tz=none) returns a Datemime object from a timestampDateTime object:timestamp () returns a timestamp to microsecondsTimestamp : GMT 170 January 1 0 o'clock to present the number of secondsDateTime object, Construction Method: Datetime.datetime (2017,10,08,13,55,35,123456) year months month day hour time minute minutes second seconds microsecond microsecond replace () returns and modifies the new Time Isocalendar () returns a ternary group (year, week, Day of week) date formatting:
Class method Strptime (Date_string,foamat), returning a DateTime object Object method Strftime (Fotmat), returns a string string formatted with the Format function
Timedelta object:
datetime2= datetime1+datedelta datetime2=datetime1-datedelta timedelta=datetime1- DateTime2 Construction Method: datetime.timedelta (day = 0, sec =0, microsecond =0, ms = 0, Min =0, time =0,weeks=0) year = Datetime.timedelta (day=365) total_seconds () returns the total number of seconds of the time difference time: time.sleep (secs) suspends the calling thread for the specified number of seconds
List Resolution:Example: Generate a list, element 0~9, increment 1 after each element to return a new list
L1=list (range) L2=[] for in L1: l2.append ((i+ 1) **2 )
A program that uses list parsing is:
L1=list (range) L2 for in L1]print
List parsing lists comprehension syntax:
for inch if conditions] Use brackets [], Inside is for loop, if condition statement optional return a new list
List parsing is a syntactic sugarthe compiler optimizes and does not affect efficiency by shorthand, but optimizes efficiencyReduced code volume, readability, reduced workload, reduced errorsList parsing Advanced: List parsing is:[expr for item in iterable if Cond1 if Cond2]The natural syntax is:
ret = [] for in iterable: if cond1: if cond2: ret.append (expr)
Example:
for inch if and i%3==for ifif i%3==0]
the use of a list-resolved multilayer loop:A list of analytic expressions:
[Expr for I in Iterable1 for J in Iterable2]Natural notation:
ret = [] for in iterable1: for in iterable2: Ret.append (expr)
Example
[(x, Y) forXinch 'ABCDE' forYinchRange (3)][[x, y] forXinch 'ABCDE' forYinchRange (3)][{x:y} forXinch 'ABCDE' forYinchRange (3)]
Generator expression:Grammar
( return value for element in can iterate object if condition ) list parsing the brackets in parentheses are the generator expressions that return a generator
and list parsing differences:
The generator expression is evaluated on demand (' lazy evaluation ', deferred calculation), and the value list parsing is the return value immediately when needed
Generator:can iterate over objectsiteratorsExample
and list analytic comparison:
Calculation: Generator expression Deferred calculation, list parsing immediately calculates memory consumption: single from the return value, the generator expression saves memory, the list is resolved to return the new list Generator no data, memory consumption, when used, return data, Total occupancy data Almost list parsing a new list takes up memory space calculation speed: The generator expression is time-consuming and the column parsing is time-consuming, but the generator itself does not return any values, only one generator object is returned The list-parsed construct returns a new list
Summarize:
In general, multi-use analytic, short and efficient if an analytic is very complex and difficult to understand, consider disassembling the different objects for the For loop generator and iterator, but are all iterative objects
built-in functions:
ID (value)
Represents the unique identity of an ID return object
Hashed hash () returns the hash value of an object
Types Type ()
Returns the type of the object type convert float () int () bin () Hex () Oct () bool () list () tuple () Dict () set () complex () bytes () ByteArray (
Enter input ([prompt]) to receive user input, return a string
Print (*objects, sep=', end='\ n', File=sys.stdout, flush=False) printout, default with space split, end of line wrap, output to console
Object length Len (s) returns the number of elements of a collection type
Isinstance (obj,class_or_tuple) determines whether the object obj is of a type or a type listed in the tuple isinstance (true,int) Issubclass (cls,class_or _tuple) Determines whether the type CLS is a subclass of a type or a subcategory of a type listed in a tuple issubclass (bool,int)
Absolute ABS (x) x is numeric
Max Max () min min () returns the maximum or minimum value in an iterative object that returns the maximum or minimum value in multiple parameters
Round (x) round six rounding, Rount ( -0.5) pow (x, y) equals x**y
Common functions ()
Range (stop) is an iterative object from 0 to stop-1, and Range (Start,stop[,step]) from start to stop-1 step to step
Divmod (x, y) is equivalent to a tuple (x//y,x%y) sum (which iterates over the object [, start]) sums all the numeric elements of the iterated object by Sum (1,100,2)) 1 to 100 steps for 2 elements and values
Chr () given a range of integers returns the corresponding character chr (=a chr (20013) = Medium ord () returns the integer corresponding to the character Ord ('A ') =97 ord (' Middle '
Sorted (Iterator Object [element][reverse]) sort Returns a new list by default ascending reverse is inversion reversal: Reversed (seq) Returns an iterator that reverses the element
enumeration: Enumerate (seq, start=0) iterates over a sequence that returns an index number and an element consisting of a two-tuple star that represents the starting number of the index, which defaults to 0
Iterators and fetching elements ITER (an iterative object), next (an iterative object) ITER encapsulates an iterative object into an iterator next removes an element from an iterator if all elements have been taken, Again next will throw Stopiteration exception
Objects that can be iterated:
can iterate over and over again return different elements of the object, the so-called same does not mean that the value is the same, but whether the element is the same in the container, for example, the value of the list can be repeated can iterate, but not necessarily orderly, may not be indexed can iterate objects are: List tuple String bytes ByteArray range set Dict generator and so on, in essence mountain is the object being traversed
Iterators:
A special object, which must be an iterative object, has the characteristics of an iterative object. encapsulating an iterative object into an iterator via the ITER French method through the next method, iterate over the Iterator Object Builder object , which is the iterator object
Zip function Zip (multiple iterative objects)
Item Zipper merges multiple, iterative objects together, returning an iterator
Python list parsing, generator, and part of the built-in function usage method