Python Basics 4 - Judgment (if) Statement

6. Judgment (if) statement

6.1 Basic syntax of if judgment statement

In Python, the if statement is used to judge, the format is as follows:

if condition to be judged:
    What to do when the conditions are met
    ……

Note: Code is indented by one tab, or 4 spaces - spaces are recommended

    • In Python development, don't mix tabs and spaces!
age = 18
# if statement and indented code is a complete code block
if age >= 18:
    print("You can enter the Internet cafe, hipi...")
# 3. Think! - will be executed regardless of whether the condition is met or not
print("When will this code be executed?")

Note :

    • The if statement and the indented part are a complete block of code

6.2 else handles the case where the condition is not met

else, the format is as follows:

if condition to be judged:
    What to do when the conditions are met
    ……
else:
    What to do when the conditions are not met
    ……

  Note :

    • if and else statements and their indented parts together form a complete code block
age = int(input("How old is this year?"))
# if statement and indented code is a complete syntax block
if age >= 18:
    print("You can enter the Internet cafe, hipi...")
else:
    print("You haven't grown up yet, you should go home and do your homework!")
print("When will this code be executed?")

  

6.3 Logical Operations

  • and
Condition 1 and Condition 2

    And / and , both conditions are satisfied at the same time, return True, as long as one is not satisfied, return False

  • or
Condition 1 or Condition 2

    Or / or , as long as one of the two conditions is satisfied, return True, if both conditions are not satisfied, return False

  • not
not condition

  not / not

6.4 The elif statement

  • In development, use if to  judge conditions
  • Use else to handle   situations where the condition is not true
  • However, if you want  to add some more conditions , the conditions are different, and the code to be executed  is , you can use elif

 

if condition 1:
    Condition 1 is satisfied to execute the code
    ……
elif condition 2:
    When condition 2 is met, the code to execute
    ……
elif condition 3:
    When condition 3 is met, the code to execute
    ……
else:
    When none of the above conditions are met, the code to execute
    ……

# Compare the code of logical operators
if condition 1 and condition 2: Code that executes when condition 1 is met and condition 2 is met ……

  

Notice

  1. Both elif and else must be used in conjunction with if, not alone
  2. You can think of if, elif, else and their indented code as a complete code block

6.5 Nesting of if

  • The application scenario of if 's nesting is: on the premise that the previous conditions are satisfied, add additional judgments
  • The syntax format of if 's nesting , except for indentation, is no different from the previous one
if condition 1:
    Condition 1 is satisfied to execute the code
    ……
    if condition 1 is based on condition 2:
        When condition 2 is met, the code to execute
        ……  
    else:
        When condition 2 is not met, the code to execute
else:
    When condition 1 is not met, the code to execute
    ……

  

has_ticket = True
knife_length = 20

if has_ticket:
    print("There is a ticket, you can start the security check...")
    if knife_length >= 20:
        print("It is not allowed to carry knives with a length of %d cm" % knife_length)
    else:
        print("Passed the security check, I wish you a good trip...")
else:
    print("Brother, you have to buy a ticket first")

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215020&siteId=291194637