Python process control code

Source: Internet
Author: User
This article introduces the Python process control code 1. while statement

Conditional loop control statement. It usually needs to be used with break, otherwise it will enter an endless loop.

Format: [while <条件> :

<内容>

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 <对象集合> :]

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.

Class range (object): "range (stop)-> range object range (start, stop [, step]) -> range object Return an object that produces a sequence of integers from start (random) to stop (exclusive) by step. range (I, j) produces I, I + 1, I + 2 ,..., j-1. start defaults to 0, and stop is omitted! Range (4) produces 0, 1, 2, 3. these are exactly the valid indices for a list of 4 elements. when step is given, it specifies the increment (or decrement ). "def count (self, value): # real signature unknown; restored from doc" rangeobject. count (value)-> integer -- return number of occurrences of value "" return 0 def index (self, value, start = None, stop = None): # real signature unknown; restored from doc "rangeobject. index (value, [start, [stop])-> integer -- return index of value. raise ValueError if the value is not present. "" return 0 def contains (self, * args, ** kwargs): # real signature unknown "" Return key in self. "" pass def eq (self, * args, ** kwargs): # real signature unknown "" Return self = value. "pass def getattribute (self, * args, ** kwargs): # real signature unknown" "Return getattr (self, name ). "" pass def getitem (self, * args, ** kwargs): # real signature unknown "" Return self [key]. "" pass def ge (self, * args, ** kwargs): # real signature unknown "" Return self> = value. "pass def gt (self, * args, ** kwargs): # real signature unknown" "Return self> value. "pass def hash (self, * args, ** kwargs): # real signature unknown" "Return hash (self ). "" pass def init (self, stop): # real signature unknown; restored from doc pass def iter (self, * args, ** kwargs ): # real signature unknown "" Implement iter (self ). "pass def len (self, * args, ** kwargs): # real signature unknown" "Return len (self ). "" pass def le (self, * args, ** kwargs): # real signature unknown "" Return self <= value. "pass def lt (self, * args, ** kwargs): # real signature unknown" "Return self
 
  
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') # running result for statement 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) # run result 23

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

The above is a detailed explanation of the Python process control code. For more information, see other related articles in the first PHP community!

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.