Daily learning python

First, conditional statements

 

  Python by a conditional statement or statements of the execution result (True or False) to determine the execution code block.

 

We can understand the simple execution of conditional statements by the following figure:

 

 

 

  Python programming language specify any non-empty and non-0 (null) is true, 0 is null or false.

 

  Python programming if statement for executing the control program, the basic form (the same group must be aligned):

  if the condition is determined:

    Statement 1 is executed

  else:

    Execute the statement 2

  Wherein when the "determination condition" is satisfied (non-zero), the latter statement is executed, the content may be performed multiple lines, indented to distinguish the same range.

else statement is optional, when necessary conditions are not set up to perform content-related statements can be executed.

  Analyzing the condition of the if statement may be represented by the relationship> (greater than), <(less than), == (equal),> = (greater than or equal), <= (less than or equal).

When the determination condition is a plurality of values, the following form:

  if Condition 1 is determined:

    Statement 1 is executed

  elif judgment condition 2:

    Execute the statement 2

  elif judgment condition 3:

    3 execution statement

  else:

    4 Execute the statement

  Because the switch statement does not support python, so multiple conditional, elif can only be achieved, if the judge requires several conditions to be judged at the same time, you can use or (or) to indicate when two conditions are judged to have a set up for success ; use and when (and), represents the only case where two conditions are satisfied, the judgment condition was successful.

  When there are multiple conditions may be used if parentheses to distinguish the sequence determination, the determination parentheses preferentially executed, and in addition, and a priority lower than or> (greater than), <(less than) symbols and the like is determined, i.e., above and below under no circumstances will the brackets or priority than the judge.

Second, slice syntax

  Python slices are particularly commonly used functions, mainly for the elements on the list of values. Use slice it makes your code is especially Pythonic.

  The main sections of the statement is as follows, assuming that there is now a list, named alist:

    alist = [0,1,2,3,4]

  The basic form of slice syntax is:

    alist[start:stop:step]

  It can be seen there are three operating parameters for the list of sections, namely:

    start: Starting position

    stop: end position

    step: step

  Three parameters are optional parameters, meaning the subscript list, that index. a step parameter defaults. There are several forms:

alist[start:stop]
alist[start:]
alist[:stop]
alist[:]

  • The first embodiment specifies the start and stop parameters, specified by the start index from start of elements having alist until stop-1, for example, a [1: 3] results in [1,2].
  • The second embodiment starts from the start of the specified index, taking alist remaining elements. For example a [1:] will be [1,2,3,4].
  • A third embodiment will alist from the start, take list element, until the subscript stop-1, for example a [: 4] would result obtained [0,1,2,3]
  • The fourth way is not specified start and stop parameters, it will return the entire list.

  Note that,: stop represents a value not in the selected slice, alist [start: stop] values ​​similar range of mathematics [start, stop), so that a default step size in the case where the result of the start-stop is the number of elements taken.

Negative usage

  start and stop parameter is negative

  Start and stop values ​​can be negative, that value is the last from the beginning of the list, rather than the beginning. E.g:

grown [-1]
grew [-3:]
grew [: - 1]

[2,3,4]
[0,1,2,3]

  step parameter is negative

  step represents a negative value when the reverse list, the simplest example is as follows:

    alist[::-1]

  The output of [4,3,2,1,0]. The following example is based on the actual return this form,

    [alist[-1], alist[-1-step], ..., alist[0]]

  That starts with the first element of the countdown, every step of the step plus, because there is a negative step, it is a subtraction, until the beginning of alist. Know this principle, we can understand some of the parameters plus the start and stop complex wording, such as:

  Specify the start parameters

    alist[1::-1]

  Actually it is like to return:

    [alist[1], alist[1-step], ..., alist[0]]

  Thus, the return is [1,0]

  Parameters specified stop

    alist[:1:-1]

  Actually it is like to return:

    [alist[-1], alist[-1-step], ..., alist[stop+1]]

  Thus, the return is [4, 3, 2].

  Specify the start and stop parameters

  Note that since the reverse order, so start parameter should be greater than the stop argument.

    alist[3:1:-1]

  Actually it is like to return:

    [alist[start], alist[start-step], ..., alist[stop+1]]

  Thus, the return is [3,2]

  We can see that

  • If parameter specifies the start, from the start parameter index began to take elements until the first stop element of the index or list.
  • If the start parameter is not specified, the first element from the list countdown begins until the next stop of the first element of the mark or list.

  to sum up

  Use slice syntax is particularly flexible, if properly mastered its use, can simplify your code. Note that, Python is also provided for producing slices slice type, for example:

    alist[start:stop:step]

  Essentially equivalent to

    alist[(slice(start,stop,step))]

 

 

 

 

Guess you like

Origin www.cnblogs.com/sndd/p/11567116.html