Python Development "section 4th", "Python branching structure and looping structure"

Source: Internet
Author: User

1. Process Control

  流程: The order in which the computer executes the code is the process

  流程控制: The management of the Order of computer code execution is the process control

  流程分类: Process Control is divided into 3 categories:

      • Sequential structure
      • Branching structure/selection structure
      • Loop structure
2. Branch structure (IF...ELSE)

Multiple branching structures:

#!/usr/bin/python3age = Int (input ("Please enter your dog's Age:")) Print ("") if < 0:    print ("You are kidding me!") elif age = = 1:    print ("the equivalent of 14-year-old person.") ") elif age = = 2:    print (" equivalent to 22-year-old person. ") Elif Age > 2:    human = + (age-2)    print (" corresponding to human Ages: ", Human) # # # Exit Prompt input (" Click the Enter key to exit ")

Nested Branching structures:

#!/usr/bin/python3num=int (Input ("Enter a number:")) if num%2==0:    if num%3==0:        print ("The number you enter can be divisible by 2 and 3")    else:        print ("The number you enter can be divisible by 2, but not divisible by 3") Else:    if num%3==0:        print ("The number you enter can be divisible by 3, but not divisible by 2")    else:        print  ("The number you entered cannot be divisible by 2 and 3")
3, loop structure 3.1, while loop
Format 1: While    conditional expression: The        contents of a loop        [variable change] format 2: While    conditional expression:        The contents of the loop        [variable change]    else:        Python statement:
3.2. For...in cycle

For...in loop is used to iterate over the data of the container class (String, list, tuple, dictionary, collection)

Format 1:    for  variable  in  container:        python code, where you can use variable Format 2:    for variable 1, variable 2 in container:        python code, Variable 1 and variable 2 can be used here
3.3. Else clause
Format:    for  variable  in container:        python code, where you can use the variable    else:        Loop End is the code of execution!
3.4. Break statement

Break effect: Terminates the subsequent operation of the current loop structure

#!/usr/bin/python3for letters in ' Runoob ':     # First Instance   if letter = = ' B ':      break   print (' The current letter is: ', ') var = 1 0                    # Second instance while var > 0:                 print (' When variable value is: ', var)   var = var-1   If var = = 5:      breakprint ("Good bye!")
3.5. Continue statement

Continue effect: Skips the remaining statements in the current loop block and proceeds to the next round of loops.

#!/usr/bin/python3for letters in ' Runoob ':     # First Instance   if letter = = ' O ':        # Skip Output      continue   Print (' Current letters: ', letter) var = ten                    # second instance while var > 0:                 var = var-1   If var = = 5:             # variable is 5 o'clock skip output      Conti Nue   print (' Current variable value: ', var) print ("Good bye!")
3.6. Pass Statement

Pass is an empty statement, in order to maintain the integrity of the program structure, pass does not do anything, generally used as a placeholder statement

def spide ():     Pass if __name__ " __main__ " :    spider ()

Python Development "section 4th", "Python branching structure and looping structure"

Related Article

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.