Note the python conditional statements, loop statements, iteration and generator

This segment of the python in the conditional statement, a loop statement, iterators and generators.

1, conditional statements

python conditional statements if ...... elif ...... else format

. 1 NUM = int (INPUT ( " Please enter your score: ' ))
 2  IF NUM> 85 :
 . 3      Print ( ' excellent ' )   # indentation divided into chunks 
. 4  elif 85> NUM => 70 :
 . 5      Print ( ' good ' )
 . 6  elif 70> NUM => 60 = :
 . 7      Print ( ' fail ' )
 . 8  the else :
 . 9      Print ( ' fail ' )

python does not switch - case statement

2, loop

while loop

. 1 NUM. 5 =
 2  the while (NUM> 2):   # the while loop 
. 3      Print (NUM)
 . 4      NUM - =. 1
 . 5  
. 6 NUM =. 5
 . 7  the while (NUM> 2): # the while the else ...... ...... loops, while conditions when performing false else statement 
. 8      NUM - =. 1
 . 9      Print (NUM)
 10  else :
 . 11      Print ( " else: D% " % NUM)

python does not do ...... while ......, when there is an infinite loop can use CTRL + C to exit

for loop, any sequence may loop through an object, such as a dictionary, strings, lists

. 1 NUM. 5 =
 2  for I in [1,2,3,4,5,7 ]:
 . 3      Print (I)
 . 4  
. 5  # str1 = 'cainiaoxuexi' 
. 6 str1 = ' A ' 
. 7  for I in str1:
 . 8      IF I == ' X ' :
 . 9          Print ( ' break out of the loop ' )
 10          bREAK      # bREAK out the entire cycle 
. 11      Print (I)
 12 is  the else :              #If there is no break cycle, then the end for else perform 
13 is      Print ( ' no loop ' )
 14  Print ( " loop end " )
. 1 str1 = ' AXV ' 
2  for I in str1:
 . 3      IF I == ' X ' :
 . 4          Continue   # end of the cycle 
. 5      Print (I)
 . 6  else :              # If there is no break cycles, then perform the end for else 
7      Print ( ' without bodies ' )
 . 8  Print ( " end of cycle " )

break  statement can jump out of the loop for a while and. If you break out or for while loop, any corresponding loop else block is not executed.

continue  statement is used to tell Python skip the remaining statements in the current loop block, and then proceed with the next cycle.

range () function for generating a sequence of numbers

. 1  for I in Range (100,120,5):   # can specify the start, end, and step length, step may be negative 
2      Print (I)
 . 3  
. 4  for I in Range (-100, -120, -5):   # can specify the start, end, and step length, step length can be negative 
. 5      Print (I)

3, iterators and generators

Iterator can remember the location of objects in the cycle, it is a way to access a collection of elements, starting from the first access element of the collection until all the elements are accessed session is over. Iterator can only move forward not backward.

Iterator has two basic methods: ITER ()  and  Next () .

Strings, lists, tuples or objects can be used to create an iterator:

. 1  Import SYS
 2  
. 3 List1 = [1,2,3,4,5,6,7,8 ]
 . 4 IT = ITER (List1)
 . 5  Print (type (IT)) # is a list of objects fall 
. 6  the while True:
 . 7      the try :
 . 8          Print (Next (IT))
 . 9      the except the StopIteration:
 10          the sys.exit ()

Iterator class creates a need to implement two methods in a class the __iter __ () and the __next __ () 

. 1  class the MyNumber ():
 2      DEF  the __iter__ (Self): #__ ITER __ () method returns a special iterator object
 . 3          self.a =. 1
 . 4          return Self
 . 5  
. 6      DEF  __next__ (Self):
 . 7          IF self.a <= 20 is :
 . 8              X = self.a
 . 9              self.a. 1 = +
 10              return X
 . 11          the else :
 12 is              The raise the StopIteration
 13 is  
14  
15 MyClass = the MyNumber ()
 16 myiter = ITER (MyClass)
17 
18 for x in myiter:
19     print(x)

Builder

In Python, using a function generator is referred to as yield (generator).

The difference is that with an ordinary function, the generator is a return iterator function can only be used to iterate operation, more simply understood generator is a iterator.

During the call generator is running, each encounter yield function pauses and save all of the current operating information, the return value of yield, and the next execution of the next () continue to operate from its current position method.

Calling a generator function, it returns an iterator object.

. 1  Import SYS
 2  
. 3  
. 4  DEF Fibonacci (n-):   # generator function - Fibonacci 
. 5      A, B, counter = 0,. 1 , 0
 . 6      the while True:
 . 7          IF (counter> n-):
 . 8              return 
. 9          the yield A
 10          a, B = B, a + B
 . 11          counter +. 1 =
 12 is  
13 is  
14 F = Fibonacci (10)   # F is an iterator that returns generated by the generator 
15  
16  the while True:
 . 17      the try :
 18 is          Print(next(f), end=" ")
19     except StopIteration:
20         sys.exit()

Guess you like

Origin www.cnblogs.com/heertong/p/12104748.html