Python Process Control and python Process Control
1. while statement
Conditional loop control statement. It usually needs to be used with break, otherwise it will enter an endless loop.
Format: [while <condition>:
<Content>
Break]
X = int (input ('enter a number: ') while x> 0: print ('positive number') break
2. if statement
Conditional Control of process branches, generally used with elif and else.
X = int (input ('enter a number: ') if x <0: print ('negative number') elif x = 0: print ('0') else: print ('positive number ')
3. for statement
A loop control statement can be used to traverse an object and use it with in.
Format: [for <> in <object set> :]
X = ['A', 'B', 'C', 'D'] for I in x: # I position characters, as long as they are not keywords, print (I)
4. range () function
Number Sequence iterator, When you iterate on it, it is an object that can return continuous items as expected sequence, but to save space, it does not really construct the list.
Format: range (stop) indicates the end value. The default start value is 0 and the interval is 1.
Range (start, stop) is the start value and the end value. The interval is 1.
Range (start, stop, step) indicates the start value and the end value. The interval is the step value.
1 class range(object): 2 """ 3 range(stop) -> range object 4 range(start, stop[, step]) -> range object 5 6 Return an object that produces a sequence of integers from start (inclusive) 7 to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1. 8 start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3. 9 These are exactly the valid indices for a list of 4 elements.10 When step is given, it specifies the increment (or decrement).11 """12 def count(self, value): # real signature unknown; restored from __doc__13 """ rangeobject.count(value) -> integer -- return number of occurrences of value """14 return 015 16 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__17 """18 rangeobject.index(value, [start, [stop]]) -> integer -- return index of value.19 Raise ValueError if the value is not present.20 """21 return 022 23 def __contains__(self, *args, **kwargs): # real signature unknown24 """ Return key in self. """25 pass26 27 def __eq__(self, *args, **kwargs): # real signature unknown28 """ Return self==value. """29 pass30 31 def __getattribute__(self, *args, **kwargs): # real signature unknown32 """ Return getattr(self, name). """33 pass34 35 def __getitem__(self, *args, **kwargs): # real signature unknown36 """ Return self[key]. """37 pass38 39 def __ge__(self, *args, **kwargs): # real signature unknown40 """ Return self>=value. """41 pass42 43 def __gt__(self, *args, **kwargs): # real signature unknown44 """ Return self>value. """45 pass46 47 def __hash__(self, *args, **kwargs): # real signature unknown48 """ Return hash(self). """49 pass50 51 def __init__(self, stop): # real signature unknown; restored from __doc__52 pass53 54 def __iter__(self, *args, **kwargs): # real signature unknown55 """ Implement iter(self). """56 pass57 58 def __len__(self, *args, **kwargs): # real signature unknown59 """ Return len(self). """60 pass61 62 def __le__(self, *args, **kwargs): # real signature unknown63 """ Return self<=value. """64 pass65 66 def __lt__(self, *args, **kwargs): # real signature unknown67 """ Return self<value. """68 pass69 70 @staticmethod # known case of __new__71 def __new__(*args, **kwargs): # real signature unknown72 """ Create and return a new object. See help(type) for accurate signature. """73 pass74 75 def __ne__(self, *args, **kwargs): # real signature unknown76 """ Return self!=value. """77 pass78 79 def __reduce__(self, *args, **kwargs): # real signature unknown80 pass81 82 def __repr__(self, *args, **kwargs): # real signature unknown83 """ Return repr(self). """84 pass85 86 def __reversed__(self, *args, **kwargs): # real signature unknown87 """ Return a reverse iterator. """88 pass89 90 start = property(lambda self: object(), lambda self, v: None, lambda self: None) # default91 92 step = property(lambda self: object(), lambda self, v: None, lambda self: None) # default93 94 stop = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
Range
For I in range (3): # The running result is, 2 print (I) for I in range (): # The running result is, 4 print (I) for I in range (-, 2): # The running result is-, print (I)
5. break and continue statements, as well as the else statements in the loop
1) The break statement is similar to the one in C and is used to jump out of the last level for or while loop.
while True: print('hello') break
2) continueThe statement indicates that the loop continues to execute the next iteration:
For x in range (1, 4): print (x, 'for clause ') continue print (x, 'after the continue clause') else: print (x, 'else statement') # Run result 1 for Statement 2 for Statement 3 for Statement 3 else statement
3) The else in the loop
For example, in the continue example, if a for-else statement exists, the else statement is executed after the loop jumps out, but the break jumps out of the loop and does not execute else, therefore, else can be used to handle exceptions in the loop.
For x in range (1, 4): print (x) else: print (x) # running result 1233
6. pass statement
The pass statement does not do anything. It is used for statements that require syntax, but the program does nothing. It is usually used to create a class with the minimum structure.
On the other hand, pass can be used as a placeholder for a function or control body when a new code is created. It allows you to think at a more abstract level.
class EmptyClass: pass