Python's Process Control statement

Source: Internet
Author: User

Typically, Python scripts are always executed in sequence, but for some complex programs, you may want to skip the other part of the statement by choosing to execute a portion of the statement depending on the circumstances that occur during execution, or repeat a portion of the statement, which requires a Process control statement in Python.

    • Branching structure: if statement

The IF statement is a basic conditional test statement that is used to determine the different situations that may be encountered and to execute a certain portion of the statement for different situations. The basic form of the IF statement is as follows:

If < conditions;: statement elif< condition;: statement else: statement

You can also nest if in if statements such as:

If < conditions;: if < conditions;: statement else: statement elif< condition;: if < condition;: statement else: Language Clause ELSE: statement

Example

 sex=input ("Input your gender") Please enter your gender              if sex== "Girl"             if you're a woman,                      print ("I word like")     I Like you              elif sex== "Man"       otherwise you're a guy                      print ("Going  to homesexual ")     we engage in the base              else:                                print ("Pervert ")
    • Loop structure for statement

      A For statement can be used to loop through an object He also has an additional else block. The accompanying else fast is optional primarily for handling the break statement when the For loop is break, the Else statement is executed, and the For statement can also use continue to skip the next round of statements that begin with the following loop for statement format:

For <> in < object collection;: #对象集合可以是元祖, List, dictionary, or range () function produces an integer set if< condition;: Break #终止循环 if< Bar Pieces > Continue #使用continue Skip other statements, continue looping < other statements >else: <> #如果for循环未被break语句终止, execute the statement in the Else block

Example:

For i in [1,2,3,4,5]: If I==6:break if i==2:continue print (i) else:print ("All")
    • Circular structure Whie statement

      The while statement is also a loop control statement, unlike for loops, where the while statement stops only when the test condition is false, while the statement block of the while is bound to include a statement that alters the test condition, which guarantees that the loop can end, avoid a dead loop, Break and continue can also be useful for while loops, while statements are formatted as follows:

While < conditions;: Break #终止循环 if< Condition > Continue #使用continue Skip other statements and continue looping if< < other statements >else: <> #如果for循环未被break语句终止, execute the statement in the Else block

Example:

Lucky_num=19input_num=-1guess_count=0while guess_count<3:input_num = Int (input ("Input The Guess num:")) if input _num > Lucky_num:print ("smaller") Elif Input_num < Lucky_num:print ("bigger") Else:prin T ("Bingo") break Guess_count + = 1else:print ("Too Many Retrys")

Welcome everyone to advise

This article is from the "Ancient Capital Road" blog, please be sure to keep this source http://7157581.blog.51cto.com/7147581/1790633

Python's Process Control statement

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.