0. New and old
Two kinds of reverse
>>> L = [1,2,3,4]# New Object>>> r[4, 3, 2, 1]# in place >>> l[4, 3, 2, 1]>>>
Two kinds of sort
# New Object [' Black ', ' blue ', ' green ', ' red ', 'white ' >>> r[' white ', ' black ', ' green ', ' blue ', ' red '] # in-place>>> r[' black ', ' blue ', ' green ', ' red ', ' white '
1. Function parameters
def F (A, B, c=0, *ARGC, **kw):
Print A, B, C, argc, KW
>>> f (1, 2, 3, ' A ', ' B ', ' C ', key= ' value ')
1 2 3 (' A ', ' B ', ' C ') {' key ': ' Value '}
A, B is required parameter, c default parameter, argc variable parameter, kw is the keyword parameter. When the function is passed in, ARGC is a tuple,kw of Dict.
Note The default parameter, be sure to point to the immutable variable. The same parameter of multiple functions may point to the same instance
>>>defFoo (bar=[]): ..... bar.append ("Baz") ... returnBar>>>foo () ["Baz"]>>>foo () ["Baz","Baz"]>>>foo () ["Baz","Baz","Baz"]>>>defFoo (bar=None): .....ifBar isNone:#or if not bar:... bar =[] ... bar.append ("Baz")... returnBar ...>>>foo () ["Baz"]>>>foo () ["Baz"]>>>foo () ["Baz"]
2. Iteration
- Determine if you can iterate, use iterable
from Import iterable>>> isinstance ('string', iterable) True>>> Isinstance ([Isinstance], iterable) True>>> (123, iterable) False
- Introduction of subscript, available enumerate
for in Enumerate (['a''b''C' ]):... Print I, v ... 0 A1 b2 c>>>
- Iterate multiple lists at the same time, using function zip
for in Zip ([All-in-a-d], ['a'b''c' ]):... Print I, J ... 1 a2 b3 C
3. If-else in list comprehension
>>> L = ['We',' is', 42,'ALONE','!']>>> [S.lower ()ifIsinstance (S,STR)ElseS forSinchL] ['We',' is', 42,'alone','!']
4. Class
Http://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide
Qualify members of Class
__slots__
class Student (object): ... __slots__ = ('name'' age') ...
5. Exception
Check for multiple exceptions and need to be placed within a tuple
Try :... = ["a""b"] ... Int (l[2except (ValueError, Indexerror) as e: ... Pass ... >>>
Some Python knowledge points