00x1 Python Branch Loop

Source: Internet
Author: User
Tags case statement ming


Three major structures
    • Order
    • Branch
    • Cycle
Branch
    • Basic syntax for branching

      • An IF condition expression:
        • Statement 1
        • Statement 2
        • Statement 3
        • ......
    • A conditional expression is an expression that evaluates to a Boolean value
    • The colon after the expression must not be less
    • Note If the statement that appears after the IF statement block, you must have the same lock level
    • Conditional expression evaluates to True to execute the indented statement block after the IF
In [4]:
# If Statement Practice # If Age is less than 18 years old, then print the message "Minors cannot get on the bus." Age    = 17if ages <: Print    (' minors not on the car ')    print (' You go, my mom won't let me play with the kids ')

  

Minors can't get on the bus you go, my mom won't let me play with the kids.
In [2]:
# If Statement Practice # If Age is less than 18 years old, then print the message "Minors cannot get on the bus" ages = 19if <:    print (' Minors cannot get on the bus ')    

  

Get in the car, boys.
In [3]:
# If Statement Practice # If Age is less than 18 years old, then print the message "Minors cannot get on the bus" ages = 19if < 18:    

  

We're not taking you on.
In [5]:
Print (' Learn for loop today ') gender = "male" if gender = = "female":    print (' Come on, uncle gives you sugar to eat ') print (' Start speaking for Loop ')?

  

Learn about the For loop today and start with the For loop.
Two-Way Branch
    • If...else ... Statement

      • An IF condition expression:
        • Statement 1
        • Statement 2
        • ...
      • Else
        • Statement 1
        • Statement 2
        • .....
    • A two-way branch has two branches when the program executes to the IF ... else ... Statement, one of the if or else must be executed, and only one

    • Indentation issues, if and else one level, the rest of the statements one level

In [8]:
# The role of input is # 1. Output the string # 2 in parentheses on the screen. Accepts user-entered content and returns to program # 3. The content returned by input must be a string type gender = input (' Please enter Gender: ') print (' The gender you entered is: {0} '. Format (gender))? If gender = = ' man ':    print (' Come on, Shall we commemorate today, the code knocks 10 times ')? Else:    print (' Hair sugar ')    

  

Please enter gender: Male you entered the sex is: male, we commemorate today, code 10 times the end
In [9]:
# test Results Judgment # 90 above: Output Excellent # 80-90: Good # 70-80: # 60-70: Flat # 60 below: output: I don't have a monk.

  

In [10]:
# score  Store Student Results # Note The return value type of input score = input ("Please enter student score:") # need to convert str to Intscore = Int (score)? If score >=:    print ( ' A ') if score >= and score <:    print (' B ') if score >= and score <:    print (' C ') if score >= 6 0 and score <:    print (' D ')? If score <:    print ("You go, my mom won't let me play with Fools")  

  

Please input student score: 80B
Multi-Channel Branch
    • Many branches of the case, referred to as the multi-channel branch

      • An IF condition expression:
        • Statement 1
        • ....
      • elif conditional expression:
        • Statement 1
        • ...
      • elif conditional expression:

        • Statement 1
        • ...
      • .....

      • ELSE:-Statement 1 ....
    • Elif can be made up of many songs

    • Else optional

    • Multiple branches will only select one execution
In [11]:
# score  Store Student Results # Note The return value type of input score = input ("Please enter student score:") # need to convert str to Intscore = Int (score)? If score>=90:    print ( "A") elif score>=:    print ("B") elif score >=:    print ("C") elif score >=60:    print ("D") Else:    print ("You go, my mom won't let me play with Fools")

  

Please input student score: 8 You go, my mom won't let me play with fools.
If statement other:
    • If statements can be nested, but not recommended
    • Python does not have a switch-case statement
Looping statements
    • To perform certain fixed actions repeatedly or to handle basic fixed things.
    • Classification
      • For loop
      • While loop # for loop
    • For loop

      • For variable in sequence:
        • Statement 1
        • Statement 2
        • 。..
In [12]:
# A list is a column of numbers or other values, usually denoted by brackets # for example  [' Google ', ' Baidu ', ' GitHub ', ' Cnbolg ']?# print student list name for name in  [' Google ', ' Baidu ', ' GitHub ', ' Cnbolg ']:    

  

Googlebaidugithubcnbolg
In [14]:
# Print Student List name # if it is sister, that must be my favorite, # if it is a male classmate, that should be cold to refuse him for the name in  [' quiet ', ' Xiao Ming ', ' Lao Wang ', ' Old bi ']:    print (name)    if name = = "Quietly ":        print (' My favorite {0} sister '. Format (name))    else:        

  

Quietly my favorite quiet sister Xiao Ming you give me away, my mother does not let me play with the male classmate Lao Wang you give me away, my mother won't let me play with the male classmate you give me away, my mother won't let me play with male classmates
Range Introduction
    • Generate a sequence of numbers
    • Specific range can be set
In [15]:
# range Exercise # Print numbers from 1-10 # Note that generally in Python, if represented by a number range of two numbers, usually contains the left number does not contain the right number # Randint is a special case, he contains the # range function in Python2 and There are serious differences in the Python3 for I in Range (1,11):    print (i)

 

12345678910
For-else statements
    • When the For loop finishes, the Else statement is executed
    • Else statement is an optional statement
In [16]:
?
# for-else Statement # for-else # Print List of classmates, # if not in the list, or the end of the list, we need to print the prompt statement, indicating that is not in love? # Print student List name # If it is quiet, it must be my favorite sister, # if it's a male student, That's a cold rejection of him for name in [' Quiet ', ' xiaoming ', ' Lao Wang ', ' Old bi ']:    print (name)    if name = = ' silently ':        print (' My favorite {0} sister appeared '. Format ( Name))?    else:        print (' You go, my mom won't let me play with my boys ')? Else: Print (' My    favorite sister is not in, I'm Angry ') print (' My    favorite sister's not in, I'm angry ')    print (' My favorite sister is not in, I'm angry ')    print (' My favorite sister is not in, I'm angry ')   

  

Quietly my favorite quiet sister appeared, Xiao Ming you go, my mother won't let me play with the boys. You go, my mother won't let me play with the male classmate you go, my mother won't let me play with male classmates I like the sister not, I am angry I like the sister not, I am angry I like the sister not, I am angry I like the sister not, I'm angry.
Break,contineu,pass of the Cycle
    • Break: Unconditionally end the entire cycle, referred to as circular death
    • Continue: Unconditional end of this cycle, new into the next round cycle
    • Pass: Indicates a skip, usually used for positioning
In [17]:
# in the number 1-10, look for the number 7, once found, print out, the rest is nothing to do # for the variable representation in the loop, generally with i,k,m,n, or Indx,idx,item and the like # in Python, if the loop variable name is not important, you can use an underscore (_) instead? For I in Range (1,11):    if i = = 7:        print ("I found")        break    Else:        

  

123456 I found it.
In [18]:
# continue Statement # # # # # # # # # # # # # # # # # # # # # 1-10, look for all even numbers, find even, print even #            

  

2 is even 4 is even 6 is even 8 is even 10 is even
In [19]:
?
# Continue statement version # Contin # in the number 1-10, look for all even numbers, find even after you print even? For I in Range (1,11):    If I% 2 = = 0:        print ("{0} is even". Format ( i))   

  

2 is even 4 is even 6 is even 8 is even 10 is even
In [20]:
# Continue statement version * in the number 1-10, look for all even numbers, find even and print even numbers # This case full display continue usage and action for I in Range (1,11):    If I% 2 = = 1:        continue    

  

2 is even 4 is even 6 is even 8 is even 10 is even
In [22]:
# Pass example, typically used for placeholder # pass without skipping function? For I in Range (1,10):    Pass    

  

test!test!test!test!test!test!test!test!test!
in []:
?

00x1 Python Branch Loop

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.