I. Introduction to the environment
Python version Python 3.8.8 (
Pycharm version 2021.1.2
II. Introduction to Conditional Judgment
Python conditional statements are blocks of code whose execution is determined by the result (True or False) of one or more statements.
A block of statements is a group of statements that are executed when a condition is true (conditional statement) or executed multiple times (looping statement). Blocks are created by indenting statements by placing spaces in front of the code.
III. Use of if statements
The first use of the
For if statements, if the condition is determined to be true, then the statement block that follows is executed. If the condition is false, the statement block is skipped and not executed.
# Conditional judgment # The first use of the if statement money = int(input("Please enter the balance of your wallet:")) if money > 300: print("Eating in restaurants.") print("Go home and code.")
The second method of use of the
If the condition is valid. Execute code 1, otherwise execute code 2
# Second if statement usage money = int(input("Please enter the balance of your wallet:")) if money > 3000: print("Buy a switch game handheld.") else: print("Go home and read and study.")
The third method of use of the
Nesting if statements within if statements
# Third if usage money = int(input("Please enter the balance of your wallet:")) if money > 3000: if money > 5000 print("Buy a PS5 game.") else: print("Buy a switch game handheld.") else: print("Go home and read and study.")
The fourth method of use of the
If you need to check more than one condition, you can use elif, which is a shorthand for else if and a joint use of if and else statements, i.e., use elif to do more detailed
money = int(input("Please enter the balance of your wallet:")) if money > 5000: print("Buy a PS5.") elif money > 3000: print("Buy a switch game handheld.") else: print("Go home and read and study.")
IV. Operators of if judgment
V. Learning summary
1. Equal sign operator: ==, a single equal sign = for the assignment operator;
The conditional judgment if...elif...else is flexible;
3. Conditional judgment matches from top to bottom;
4. Short-circuit logic applies to Boolean operators (and, or).
to this article on the basis of Python conditional statements to this article, more related to Python conditional statements, please search for my previous posts or continue to browse the following articles hope that you will support me in the future more!