程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning (III) for loop in branch structure and loop structure

編輯:Python

Branches and loops

Process control

Control the sequence of code execution

  1. Sequential structure : The code is executed from top to bottom , Each statement is executed only once ( By default )
  2. Branching structure : Choose to execute or not execute part of the code according to the conditions ( Use if sentence )
  3. Loop structure : Let the code execute repeatedly (for perhaps while)

1、 Sequential structure

  1. Code execution from top to bottom successively perform
  2. Each code is executed only once

2、 Branching structure

  1. if Single branch structure :
    If … Just ….

    # Grammatical structure 
    if Conditional statements :
    Code segment
    

    explain :
    if: keyword , Fixed writing
    Conditional expression : You can any expression that has a result
    :: Fixed writing
    Code segment : Structurally, it is and if One or more statements that hold an indent ( At least one ); Logically, it is the code that will be executed only after the conditional statement is satisfied

  2. if Two branch structure :
    If … Just … otherwise …

    # Grammatical structure 
    if Conditional statements :
    Code segment 1 ( Code that meets the conditions to execute )
    else:
    Code segment 2 ( Code that does not meet the conditions to execute )
    
  3. if Multi branch structure :
    If … Just … If … Just … … otherwise …

    # Grammatical structure 
    if Conditional statements 1:
    Code segment 1 ( Meet the conditions 1 Executed code )
    elif Conditional statements 2:
    Code block 2 ( Meet the conditions 2 Executed code )
    ...
    else:
    Code segment n ( Code executed without any conditions )
    elif It could be any number ;else There can be or not .
    

3、 Loop structure

  1. for loop

    # Grammatical structure 
    for Variable in Sequence :
    The loop body
    

    explain :
    for: keyword ;, Fixed writing
    Variable : Valid variable name ( Can be defined , It can also be undefined )
    in : keyword ; Fixed writing
    Sequence ; Data of container data type , Container data structures include : character string 、 list 、 Dictionaries 、 aggregate 、 Tuples 、 iterator 、 generator 、range etc.
    : : Fixed writing
    The loop body : and for One or more statements that hold an indent ; A loop body is a block of code that needs to be executed repeatedly
    Execution process : Let the variable go out of the sequence , One by one , Until it's done ; Taking a value will execute the loop body once
    ·for loop · The number of cycles is related to the number of elements in the sequence

  2. range() function : To generate a sequence of equal differences ( Integers ).

    for i in range(3)
    print(i) # 1, 2, 3
    
    • range(n) : produce [0——n] Equal difference sequence of , The difference is 1
    • range(m,n) : produce [m——n) Equal difference sequence of , The difference is 1
    • range(m,n, setp) : produce [m——n) Equal difference sequence of , The difference is step
  3. for Basic application of circulation

    1. Cumulative sum

      • First step : Define variables to hold data , The initial value of the variable It's usually 0 or 1.
      • The second step : Use a loop to get cumulative data
      • The third step : Merge each data into the result variable in the loop
    2. Number of Statistics

      • First step : Define the last number of variables saved . Variable The default is 0
      • The second step : Use the loop to get the statistical object
      • The third step : Encounter a data that meets the conditions , Variables defined +1

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved