4 program control structure

4.1 Conditional expressions

  • As long as the value of the conditional expression is not False, 0 (or 0.0, etc.), empty value None, empty list, empty tuple, empty set, empty dictionary, empty string, empty range object or other empty iteration object, Python interpreter Both are considered equivalent to True.
  • All Python legal expressions can be used as conditional expressions, including expressions containing function calls.

  • For the details of expressions and operators, please refer to section 2.2. Here we will focus on introducing some special operators.

1. Relational operators

  • Relational operators in Py can be used continuously ・

Insert picture description here

  • In Python, "=", is not allowed in conditional expressions
  • Using the assignment operator "=" in a conditional expression will throw an exception, suggesting a syntax error

Insert picture description here

  • Relational operator lazy calculation,
  • Only the values ​​that must be calculated are calculated, not every expression in the relational expression.

Insert picture description here

2. Logical operators

  • Logical operators and, or, not means AND, OR, NOT 3

  • The logical operators and and or have short-circuit evaluation or lazy evaluation
  • As far as "Expression 1 and Expression 2" are concerned, if "Expression 1" is False or other equivalent value, the value of the entire expression is False regardless of "Expression 2", so "Expression
    2" Will be calculated
  • When designing a conditional expression with multiple conditions,
    • If you can roughly predict the probability of failure under different conditions,
    • And multiple conditions are organized according to the short-circuit evaluation characteristics of the and and or operators,
    • Can improve program running efficiency

Insert picture description here

  • The following function connects multiple strings into a string with the specified delimiter,
    • If the user does not specify a separator, a comma is used.

Insert picture description here

4.2 Selection structure

  • Common selection structures include single-branch selection structure, double-branch selection structure, multi-branch selection structure and nested branch structure. Jump tables can also be constructed to achieve similar logic.
  • In addition, the loop structure and the exception handling structure can also have an else clause, which can be regarded as a special form of selection structure. Refer to the introduction in Sections 4.3 and 11.1

4.2.1 Single branch selection structure

  • Generally use 4 spaces as the indent unit.

Insert picture description here

Insert picture description here

  • In Python, the indentation of the code is very important. Indentation is an important way to reflect the logical relationship of the code. The same code block must ensure the same amount of indentation.
  • In actual development, as long as certain conventions are followed, the typesetting of Python code can reduce requirements. For example, the following code, although not recommended to write like this, is indeed executable.

Insert picture description here

4.2.2 Double branch selection structure

here

4.3 Loop structure

4.3.1 for loop and while loop

  • while is used to determine the number of loops in advance, and it can also be used to determine the number of loops;
  • The for loop is generally used when the number of loops can be determined in advance, and is especially suitable for enumerating or traversing elements in a sequence or iterating object.
  • If the loop naturally ends because the conditional expression is not established or the sequence traversal ends, the statement in the else structure is executed.

Insert picture description here

  • The else clause in square brackets can be absent or present.

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

  • The else clause is actually not necessary, just output the result after the loop ends.
  • Only calculate 1 + 2 + 3 +… + 99 + 100,
  • The built-in functions sum () and range () are fine

Insert picture description here

4.3.2 break and continue statements

Published 589 original articles · 300 praises · 80,000 + views

Guess you like

Origin blog.csdn.net/zhoutianzi12/article/details/105572155