Reading shortlist: "Fun micro-projects, easy to learn Python" + conditional judgment, loop
[Copy link]
Conditional judgment and loop [if, while, for]
In Python, if statements are used to control the execution flow of a program based on a condition (usually a Boolean expression). If the condition is true (True), the code block under the if statement is executed. If the condition is false (False), the code block is skipped and the execution continues with the rest of the code after the if statement (if any).
The basic structure of an if statement is as follows:
a = -12345
if a > 0:
print("a 是一个正数")
elif a == 0:
print("a 是零")
else:
print("a 是一个负数")
In the following construct, Python evaluates the conditions in the order if , elif , else and executes the code block corresponding to the first condition that is True. If all conditions are not met, else the code block below (if any) is executed. If there is no else clause and all conditions are not met, no code is executed.
if condition1:
# 如果 condition1 为 True,执行这里的代码块
pass
elif condition2:
# 如果 condition1 为 False,但 condition2 为 True,执行这里的代码块
pass
# 可以有更多的 elif 子句...
else:
# 如果所有条件都不满足,执行这里的代码块
pass
The book mentions conditional judgment (if-elif-else) here.
age = 15
if age < 0:
print('You are impossible.')
elif age < 18:
print('You are a minor.')
else:
print('You can vote.')
How this code works:
- First, a variable age is defined and assigned a value of 15.
- Use an if statement to check the value of age.
- If age is less than 0, the code under if is executed.
- If the condition under if is not met, the condition under elif is checked (i.e. age < 18). Since 15 is less than 18, the code block under elif is executed, outputting You are a minor.
- If the conditions under both if and elif are not met (i.e. age is greater than or equal to 18), the code block under else is executed (but it will not be executed here because the condition under elif has been met).
- Finally, you see the output You are a minor.
For Loop
The for loop is used to iterate over a sequence (such as a list, tuple, string) or other iterable objects.
while 条件:
# 循环体,当条件为 True 时执行
# ...
Example:
for i in range(5): # range(5) 生成一个从0到4的序列
print(i)
# 输出:
# 0
# 1
# 2
# 3
# 4
Detailed explanation of the code:
- for i in range(5): This line of code creates a loop that loops over the sequence generated by range(5). range(5) generates a sequence of integers starting from 0 (including 0) and ending at 5 (excluding 5), that is, 0, 1, 2, 3, 4.
- In each loop iteration, i will be assigned the next value in the sequence, I is a variable.
- print(i) This line of code will print out the current value of i.
When you run this code, the output will be: 01234.
while A loop executes a block of code repeatedly while a specified condition is true.
i = 0
while i < 5:
print(i)
i += 1
# 输出:
# 0
# 1
# 2
# 3
# 4
In this example, we initialize the variable i to 1. Then, we use a while loop to check if i is less than or equal to 5. As long as this condition is True, the loop body is executed, which prints the value of i and increases it by 1. When i reaches 6, the loop condition (i<=5) is not met, and the loop stops.
Infinite loops: Be careful to avoid infinite loops. If the condition is always True, the program will execute the loop body forever, which may cause the program to become unresponsive. Be sure to update the loop control variable appropriately within the loop body so that the condition eventually becomes False. (That is, set a value in the loop body that changes the outcome of the condition).
Use continue to start the next loop: You can use the continue statement to directly end the current loop and start the next loop.
Use break to exit the loop: You can use the break statement to exit the loop early. However, break can only jump out of the current loop.
Indentation: The while loop also uses indentation to control the statement block.
The above is my understanding of this. Of course, the book does not emphasize loops, but I just mentioned it here so that you know what it is and how to use it, and record it for easy understanding.
|