优秀的编程知识分享平台

网站首页 > 技术文章 正文

python教程从基础到精通,第10课—流程控制

nanyue 2025-05-05 17:51:54 技术文章 11 ℃

Hello,小伙伴们,五.一快乐!

前面咱们已学习了七大数据类型的Number(数字)、Boolean(布尔类型)、String(字符串),List(列表),Tuple(元组)、Dictionary(字典)、Set(集合)。咱们要进入一个新的阶段,今天来学习python的流程控制结构。

流程控制三种结构,它们分别是顺序结构选择结构循环结构


1、选择结构

python的选择语句有两个:if语句,match..case语句。

1.1、if语句

Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。


语法格式如下:

if 表达式1:

语句

if 表达式2:

语句

elif 表达式3:

语句

else:

语句

elif 表达式4:

语句

else:

语句

示例如下:

num = int(input("输入一个数字:"))
if num % 2 == 0:
if num % 3 == 0:
print("你输入的数字可以整除 2 和 3")
else:
print("你输入的数字可以整除 2,但不能整除 3")
else:
if num % 3 == 0:
print("你输入的数字可以整除 3,但不能整除 2")
else:
print("你输入的数字不能整除 2 和 3")

1.2、match..case语句

Python 3.10 增加了 match...case 的条件判断,不需要再使用一连串的 if-else 来判断了。就跟其它语言switch...case一样。

语法格式如下:

match subject:

case <pattern_1>:

<action_1>

case <pattern_2>:

<action_2>

case <pattern_3>:

<action_3>

case _:

<action_wildcard>

case _: 类似于 C 和 Java 中的 default:,当其他 case 都无法匹配时,匹配这条,保证永远会匹配成功。

示例如下:

def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the internet"

mystatus = 400
print(http_error(mystatus))

2、顺序结构

python程序都是按由左到右,由上到下的顺序结构执行的。跟咱们阅读差不多,不赘述。


3、循环结构

Python 中的循环语句有 while 和 for 语句。

3.1、while语句

(1)while 语句的一般形式:

while 判断条件(condition):

执行语句(statements)……

示例代码:

n = 100
sum = 0
counter = 1
while counter <= n:
sum = sum + counter
counter +=
1
print("1 到 %d 之和为: %d" % (n, sum)) #1 到 100 之和为: 5050

(2)while 循环使用 else 语句

语法格式如下:

while <expr>:

<statement(s)>

else:

<additional_statement(s)>

示例代码:

count = 0
while count < 5:
print (count, " 小于 5")
count = count +
1
else:
print (count, " 大于或等于 5")

输出结果:


3.2、for语句

Python for 循环可以遍历任何可迭代对象,如一个列表或者一个字符串。

for循环的一般格式如下:

for <variable> in <sequence>:

<statements>

else:

<statements>

流程图:

sites = ["Baidu", "Google","Runoob","Taobao"]
for site in sites:
print(site, end=",") #Baidu,Google,Runoob,Taobao,
print()


for x in range(6):
print(x, end=",")
else:
print("Finally finished!") #0,1,2,3,4,5,Finally finished!

下面来看一段打印乘法九九表的程序

for i in range(1,10):#先遍历1~9
for j in range(1,i+1):#然后遍历1~i
print(i,'*',j,'=',i * j,end='丨')#循环输出1~i * i的值
print(end='\n')

结果如下:

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丨

3.3、breakt和continue语句

(1)break 语句

break 语句可以跳出 for 和 while 的循环体。如果你从 for 或 while 循环中终止,任何对应的循环 else 块将不执行。

(2)continue 语句

continue 语句被用来告诉 Python 跳过当前循环,然后继续进行下一轮循环。

先来看看break方法的实例:

n = 5
while n > 0:
n -=
1
if n == 2:
break
print(n)
print('循环结束。')

结果如下:

再来看看continue方法的例子:

n = 5
while n > 0:
n -=
1
if n == 2:
continue
print(n)
print('循环结束。')

结果如下:

3.4、pass语句

pass 是空语句,是为了保持程序结构的完整性。pass 不做任何事情,一般用做占位语句。

def sample(n_samples):
pass

在 Python3.x 中pass可省。

3.5、enumerate函数

enumerate(obj) 返回一个枚举对象,来看个举例:

colors = ['red', 'yellow', 'green', 'black']
result =
enumerate(colors) #产生一个枚举对象
for count, element in result:
print(f"迭代编号:{count},对应元素:{element}")

结果:

愉快学习的时光总是过得很快,一不小心又到结尾啦。

先来给自己一个奖励,双手举起,yeah!

有什么问题可以关注我/私信我/加好友,让我们一起成长吧。

最近发表
标签列表