
The sequential structure has no keywords. The default execution structure of the computer program is the sequential structure, that is, from top to bottom, from left to right, sequential execution. Statement format: Example: ##2.2 Dual Branch 语句格式: 2.3 多分支 语句格式: 依次遍历每个判断条件,如果判断条件为真,则执行相应代码块,否则执行最后一个代码块,所有代码块中有且仅有一个代码块会被执行。 3.1 for循环 语句格式: 依次遍历迭代对象,执行循环体语句 关于 range (前闭后开): range(100): Generate an integer ranging from 0 to 100. Note that 100 cannot be obtained. range(1, 100): Generate an integer ranging from 1 to 100. range(1, 100, 2): Generate odd numbers from 1 to 100, 2 is the step size. range(100, 0, -2): Generate an even number from 100 to 1, -2 is the step size. 语句格式: 当判断条件为真时,执行代码块,直到判断条件为假时退出。 3.3 break、continue、pass break: 在代码块执行过程中终止循环,并且跳出整个循环 continue: Terminate the current loop during the execution of the code block, jump out of the loop, and execute the next loop. pass: is an empty statement to maintain the integrity of the program structure. Example: 语句格式: 示例:
#if 判断条件:
代码块
if 判断条件: 一句代码
"""
登录密码
"""
user = 'Python 当打之年'
password = '123456'
# 用法1
if user == 'Python 当打之年':
print('用户名正确!')
# 用法2
if password == '123456': print('密码正确!')
# 用法3
if user == 'Python 当打之年':
if password == '123456':
print('用户名和密码正确!')
# 用法4
if user == 'Python 当打之年' and password == '123456':
print('用户名和密码正确!')
if 判断条件:
代码块1
else:
代码块2
user = 'Python 当打之年'
password = '123456'
if user == 'Python 当打之年':
print('用户名正确!')
else:
print('用户名错误!')
if password == '123456':
print('密码正确!')
else:
print('密码错误!')
if 判断条件1:
代码块1
elif 判断条件2:
代码块2
...
elif 判断条件n:
代码块n
else:
代码块n+1
"""
成绩等级
"""
score = float(input('请输入成绩: '))
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'E'
print('学生成绩等级为: ', grade)
for 迭代变量 in 可迭代对象:
循环体语句
for i in range(10):
print('i = ', i)
# 100以内偶数和
sum = 0
for i in range(2, 101, 2):
sum += i
print('sum = ', sum)
while 判断条件:
代码块
# 用法1
i = 0
while i < 10:
print('i = ', i)
i += 1
# 用法2
i = 0
while True:
if i < 10:
print('i = ', i)
i += 1
else:
break
"""
输出 0-10 之间大于 2 的奇数
"""
n = 10
while n > 0:
n -= 1
if n == 2:
break
if n % 2 == 0:
continue
else:
pass
print('执行pass语句')
print(n)
# 执行pass语句
# 9
# 执行pass语句
# 7
# 执行pass语句
# 5
# 执行pass语句
# 3
# while 嵌套
while 条件表达式1:
while 条件表达式2:
循环体2
循环体1
# for 嵌套
for 迭代变量1 in 迭代对象1:
for 迭代变量2 in 迭代对象2:
循环体2
循环体1
"""
九九乘法表
"""
for i in range(1, 10):
for j in range(1, i + 1):
print(f'{i}*{j}={i * j}', end='\t')
print()
# 1*1=1
# 2*1=2 2*2=4
# 3*1=3 3*2=6 3*3=9
# 4*1=4 4*2=8 4*3=12 4*4=16
# 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
# 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
# 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
# 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
# 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
The above is the detailed content of Basics | Python flow control statements. For more information, please follow other related articles on the PHP Chinese website!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor