- Video, source code, courseware, software, notes: Super comprehensive Python basic introductory tutorial [Nine-day course] Summary table of blog notes [Dark horse programmers]
Python basic day02 homework analysis [6 if true and false questions, 9 loop questions]
![]()
![]()
learning target:
- Able to tell the effect of if branch statement
- Able to tell the grammatical format of the if statement
- Be able to tell the function of the while loop statement
- Able to tell the grammatical format of the while loop
- Be able to tell the grammatical format and function of the for loop
- Be able to tell the role of break in the loop statement
- Be able to tell the role of continue in the loop statement
table of Contents
Create project (store all course codes)
1.1, the basic format of If judgment
Debug detailed steps (press only one key in the whole process: Step Over)
Control code to move up and down/rename files
1.7, if to achieve trinocular operation
Calculate the cumulative sum between 1-100.py
Calculate the cumulative sum of even numbers between 1-100.py
3. Application of for loop and while loop (printing graphics)
Jump breakpoint (Resume Program)
★☆ Looping else structure ☆★【When the loop ends normally, this statement will be executed! 】
pass # placeholder, empty code, so that the code does not report errors
Create project (store all course codes)
0. Review and feedback
![]()
- There is no difference between single quotes and double quotes.
- You need to convert a numeric string to a numeric type (int, float) , you can use eval(); or not, that is, use int() or float() directly.
1. if statement
![]()
![]()
Introduction to judgment sentences
- If certain conditions are met, you can do something; when the conditions are not met, you can't do something. This is the so-called judgment.
- Not only in life, but also in software development, the "judgment" function is often used.
1.1, the basic format of If judgment
if 要判断的条件:
条件成立时,要做的事情...
--- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
if 判断条件:
判断条件为 True, 会执行的代码
判断条件为 True, 会执行的代码
...
--- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
顶格书写的代码,代表和 if 判断没有关系。
在 python 中使用缩进,代替代码的层级关系。
在 if 语句的缩进内,属于 if 语句的代码块(多行代码的意思)。
Case requirements:
Get age through user keyboard input
Judge whether the age meets 18 years old, meet the output
哥18岁了,可以进入网吧为所欲为了
The final output of the program
if 判断结束
(will output regardless of whether it is satisfied or not)
1.2, if else structure
if 条件:
满足条件时要做的事情1
满足条件时要做的事情2
满足条件时要做的事情3
...(省略)...
else:
不满足条件时要做的事情1
不满足条件时要做的事情2
不满足条件时要做的事情3
...(省略)...
--- --- --- --- --- --- --- --- ---
if 判断条件:
判断条件为 True,会执行的代码
判断条件为 True,会执行的代码
...
else:
判断条件为 False, 会执行的代码
判断条件为 False, 会执行的代码
...
--- --- --- --- --- --- --- --- ---
if 和 else 只会执行其中的一个!
1.3, Debug debugging
Debug function:
You can view the execution process of the code
Can troubleshoot errors
Debug detailed steps (press only one key in the whole process: Step Over)
step:
①Breakpoint (usually you can put a breakpoint at the beginning of the code, or breakpoint at the place where you view the code).
②Right click debug to run the code.
![]()
③ Click "Next" to view the code execution process.
![]()
1.4, if elif structure
if xxx1:
事情1
elif xxx2:
事情2
elif xxx3:
事情3
--- --- --- --- --- --- --- --- --- --- --- --- --- ---
if 判断条件1:
判断条件1成立,执行的代码...
elif 判断条件2:
判断条件1不成立,判断条件2成立,会执行的代码...
else:
判断条件1和判断条件2都不成立,执行的代码...
--- --- --- --- --- --- --- --- --- --- --- --- --- ---
if 判断条件1:
判断条件1成立执行的代码...
if 判断条件2:
判断条件2 成立执行的代码...
- When xxx1 is satisfied, execute thing 1, and then the whole if ends
- When xxx1 is not satisfied, then judge xxx2, if xxx2 is satisfied, then execute thing 2, and then the whole if ends
- When xxx1 is not satisfied, xxx2 is also not satisfied, if xxx3 is satisfied, then perform thing 3, and then the whole if ends
demand:
Score greater than or equal to 90, excellent output
Score is greater than or equal to 80, less than 90, good output
The score is greater than or equal to 60, less than 80, and the output is passed
Less than 60, fail the output
Control code to move up and down/rename files
- Press Alt + Shift + up arrow at the same time to move up;
- Press Alt + Shift + down arrow at the same time to move down.
Rename the file:
![]()
1.5, if nested
if 判断条件1:
判断条件1 成立,会执行的代码...
if 判断条件2:
判断条件1成立, 判断条件2成立执行的代码...
else:
判断条件1成立, 判断条件2不成立执行的代码...
if 判断条件:
判断条件成立执行的代码...
else:
判断条件不成立执行的代码...
else:
判断条件1不成立,会执行的代码...
--- --- --- --- --- --- --- --- --- --- --- --- --- ---
if 条件1:
满足条件1 做的事情1
满足条件1 做的事情2
if 条件2:
满足条件2 做的事情1
满足条件2 做的事情2
Description
- The outer if judgment can also be if-else
- The inner if judgment can also be if-else
- Choose according to the actual development situation
You need to convert a numeric string to a numeric type (int, float) , you can use eval(); or not, that is, use int() or float() directly.
1.6, guessing game
Target
- Strengthen the logical operation of multiple conditions
- Experience the use of import module ("tool package")
demand
Enter the punch to be thrown from the console-head (1) / cut (2) / cloth (3)
The computer randomly punches-assume that the computer will only punch the head and complete the overall code function
Better than
Random number processing
import random # 导入随机数模块 在 Python 中,要使⽤随机数,⾸先需要导⼊ 随机数 的 模块 —— “⼯具包”
# 导⼊模块后,可以直接在 模块名称 后⾯敲⼀个 . 然后按 Tab 键,会提示该模块中包含的所有函数
num = random.randint(a, b) # 产生 [a, b] 之间的随机整数,包含 a 和 b
1.7, if to achieve trinocular operation
a if a > b else b # 如果 a > b的条件成立,三目运算的结果是a,否则就是b
if 判断条件1:
表达式1
else:
表达式2
判断条件成立,执行表达式 1;条件不成立,执行表达式 2
变量 = 表达式1 if 判断条件 else 表达式2 # 推荐使用扁平化代码
变量最终存储的结构是:
判断条件成立,表达式1的值;
条件不成立 ,表达式2的值。
Ternary operator: if else structure deformation
2. Loop
The usage of while and if is basically similar, the difference is: if the condition is established, it will be executed once; while the condition is established, it will be executed repeatedly until the condition is not established.
Generally, the code that needs to be executed repeatedly can be completed in a loop.
- Loops are not necessary, but in order to increase code reuse rate, experienced developers will use loops.
Basic syntax of while loop
while 条件:
条件满足时,做的事情1
条件满足时,做的事情2
条件满足时,做的事情3
...(省略)...
--- --- --- --- --- --- --- --- --- --- --- ---
while 判断条件:
判断条件成立, 执行的代码...
判断条件成立, 执行的代码...
判断条件成立, 执行的代码...
不在 while 的缩进内,代表和循环没有关系。
while 和 if 的区别:
if的代码块,条件成立,只会执行一次。
while的代码块,只要条件成立,就会一直执行。
Infinite loop / infinite loop
while True: # infinite loop
codeInfinite loop: Due to the programmer's reason, he forgot to modify the judgment condition of the loop inside the loop, causing the loop to continue to execute, and the program cannot be terminated (equivalent to a bug error in the code)!
Infinite loop: written artificially (written like this on purpose).
while loop application
Calculate the cumulative sum between 1-100.py
Calculate the cumulative sum of even numbers between 1-100.py
while loop nesting
while 判断条件1:
代码1
while 判断条件2:
代码2
--- --- --- --- --- --- --- --- ---
代码1 执行一次, 代码2会执行多次!
--- --- --- --- --- --- --- --- ---
while 条件1:
条件1满足时,做的事情1
条件1满足时,做的事情2
条件1满足时,做的事情3
...(省略)...
while 条件2:
条件2满足时,做的事情1
条件2满足时,做的事情2
条件2满足时,做的事情3
...(省略)...
for loop traversal
Like a while loop, for can perform the function of a loop.
In Python, a for loop can traverse any sequence of items, such as a list or a string.
Disadvantages of for loop: infinite loop cannot be realized.
for 变量 in 字符串:
代码
for循环 也称为 for遍历,会将字符串中的字符全部取到。
for 临时变量 in 列表或者字符串等可迭代对象:
循环满足条件时执行的代码
for loop nesting
3. Application of for loop and while loop (printing graphics)
Print square
Print right triangle
Jump breakpoint (Resume Program)
4. Break 和 continue
1. break 和 continue 是 python 两个关键字。
2. break 和 continue 只能用在循环中。
3. break 是终止循环的执行, 即循环代码遇到 break,就不再循环了。
continue 是结束本次循环,继续下一次循环, 即本次循环剩下的代码不再执行,但会进行下一次循环
★☆ Looping else structure ☆★【When the loop ends normally, this statement will be executed! 】
for x in xx:
if xxx:
xxx # if 判断条件成立 会执行
else:
xxx # if 判断条件不成立,会执行
else:
xxx # for 循环代码运行结束,但是不是被 break 终止的时候会执行。【循环正常结束,会执行此语句!】
Requirements: There is a string'hello python', and I want to determine whether the string contains the character "p". If it does, it outputs "Contains the character p!"; if there is no p, it outputs "The character p is not included!".
![]()
break
- The role of break: immediately end the loop where the break is located.
continue
- The role of continue: used to end this loop, and then execute the next loop.
![]()
summary
- The role of break: immediately end the loop where the break is located.
- The role of continue: used to end this loop, and then execute the next loop.
Break/continue can only be used in loops, and cannot be used alone.
In the nested loop, break/continue only affects the nearest loop.
5. Summary
pass # placeholder, empty code, so that the code does not report errors
1、使用代码的方法,求出这个数字的个位数和十位数
num = 76
个位数: num % 10
十位数: num // 10
2、判断 if elif else
if 判断条件:
pass # 占位,空代码 让代码不报错
elif 判断条件:
pass
else:
pass
3、循环: 重复做一件事
while 判断条件:
pass
for i in xxx:
pass
4、break 和 continue
Seeing Cai Huangong, Bian Que said: "You have disease in your mind, and you will be afraid if you die." Huanhou said: "No one is sick.
Everyone should have learned this article. In the budding stage of the disease, as an individual with the disease, it is very likely that there is no feeling at all. The disease has a latent and development process.