Relive python2--notes again

Source: Internet
Author: User

First, the basic knowledge

1. A value can be assigned to several variables at the same time:

>>> x = y = z = 0  #  Zero x, y and z>>> x0>>> y0>> > z0

2. Create complex numbers

# x=1+3j
>>> x.real
1
>>>x
.imag
3

3. String

>>>'Spam eggs''Spam eggs'>>>'doesn\ ' t'"doesn ' t">>>"doesn ' t""doesn ' t">>>'"Yes," he said.''"Yes," he said.'>>>"\ "Yes,\" he said."'"Yes," he said.'>>>'"Isn\ ' t," she said.''"Isn\ ' t," she said.'

The string can be identified in a pair of three quotation marks: """ or ‘‘‘ . In three quotation marks, the line is not escaped, they are already contained in the string.

Hello = R"isa. " Print (hello)

Strings can also be intercepted (retrieved)

Unicode

>>> u'Hello world! ' u ' Hello World! '
>>> str (u"ABC")'abc'

4. List

>>> a = ['spam'eggs', 1234]>> > a['spam'eggs', 100, 1234]

Just like the string index, the list is retrieved starting from 0. Lists can be sliced and connected

Allow nested lists (create a list that contains other lists)

>>> q = [2, 3]>>> p = [1, q, 4]>>> len (p)3>>> p[1][2, 3]>>> p[1][0]2

5. For statement

The Python for statement iterates over the subkeys in any sequence (linked list or string), in the order in which they are in the sequence.

# Measure Some strings:... a = ['cat''window'  defenestrate'] for in A:      ... Print  3612
>>> Range (1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> Range (5) [5, 6, 7, 8, 9]>>> range (0, ten, 33, 6, 9]>>&G T Range ( -10, -100, -30) [-10,-40,-70]

6. Defining functions

Global variables cannot be directly assigned in a function (unless named with a global statement)

def fib (n): ...      ""  Print a Fibonacci series up to N. """ ...      = 0, 1...       while a < N: ...          Print a,...          = B, A+b

The first line of the function body can be an optional string literal, which is the function's document string, or docstring .

6.1.Lambda Form

The Lambda form can be used for any function object that is required.

Similar to nested function definitions, lambda forms can reference variables from outside scopes

def make_incrementor (n): ...      return Lambda x:x + n ... >>> f = make_incrementor ($)>>> F (0)42>>> F (1)43

6.2. Document String

The first line should be an introduction to the object's purpose

If the document string has more than one line, the second line should be empty and clearly separated from the following detailed description.

The Python interpreter does not remove indents from multiple lines of document strings, so you should clear the indentation yourself when necessary.

The first line of the document is usually empty, if written, the __doc__ format is not very good

def Test ():     """ Aaaaaaaaaaaaaaaaaaaa This is     a test document!    BBBBBBBBBBBBBBBBBBBB    "" "    passprint test.  __doc__
The output is as follows:
    Aaaaaaaaaaaaaaaaaaaa This is    a test document!    bbbbbbbbbbbbbbbbbbbb

7. Episode: Coding Style Suggestions

  • Use 4 spaces to indent rather than TAB.

    Between small indents (which can be nested deeper) and large indents (easier to read), 4 spaces are a good compromise. TAB has caused some confusion, preferably deprecated.

  • Wrap the line to make sure it doesn't exceed 79 characters.

    This helps small-screen users to read, or allows large displays to display several code files side-by-side.

  • Use empty lines to separate functions and classes, and large chunks of code in functions.

  • If possible, comment exclusive line

  • Working with document Strings

  • Place the space on either side of the operator and after the comma, but no spaces in the brackets: a = f(1, 2) + g(3, 4) .

  • Uniform functions and class naming.

    Recommended class names 驼峰命名 are used, function and method names 小写_和_下划线 . Always use self the first parameter as the method (for knowledge of classes and methods see First Class).

  • Do not use fancy coding if the purpose of your code is to be in an internationalized environment. Python by default, UTF-8, even ordinary ASCII always works best.

  • Also, do not use identifiers that are non-ASCII characters unless the code is read or maintained in a different language.

Second, data structure

1. More content on the list

Python's list data type contains more methods. Here is all the list object methods:

  • list.append(x) Add an element to the end of the list
  • list.extend(L) Add all elements from a given list to another list
  • list. insert (i, x) Inserts an element at the specified position. The first parameter is the index of the element that is ready to be inserted in front of it
  • list.remove(x) Deletes the first element in a linked list that has a value of x . If there are no such elements, an error is returned.
  • list.pop([i]) Removes the element from the specified position in the linked list and returns it. If no index is specified, the a.pop() last element is returned. The element is removed from the linked list. (the square brackets on both sides of the method indicate that this parameter is optional, rather than asking you to enter a pair of parentheses, you will often encounter such tags in the Python Library Reference Manual.) )
  • list.index(x)   Returns the index of the first element in the list that has a value of x . If there are no matching elements, an error is returned.
  • list.count(x)   Returns the number of occurrences of x in the linked list.
  • list.sort ()   to sort elements in the list in place
  • list.reverse ()  in-place inverted list elements

Relive python2--notes again

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.