Some Python knowledge points

Source: Internet
Author: User

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

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.