Python 条件控制、循环与函数
if 语句
python
a = 1
if a<10:
print('a<10')
elif a==10:
print('a == 10')
else:
print('a>10')a = 1
if a<10:
print('a<10')
elif a==10:
print('a == 10')
else:
print('a>10')match ...case
python
match subject:
case <pattern_1>:
<action_1>
case <pattern_2>:
<action_2>
case <pattern_3>:
<action_3>
case _:
<action_wildcard>match subject:
case <pattern_1>:
<action_1>
case <pattern_2>:
<action_2>
case <pattern_3>:
<action_3>
case _:
<action_wildcard>循环语句
while
python
a = 10
while a<10:
if a % 2 == 0:
print('a是整数')
else:
print('a是奇数')
a+=1
a = 1
while a<5:
print('a<5')
a+=1
else:
print('a>5')a = 10
while a<10:
if a % 2 == 0:
print('a是整数')
else:
print('a是奇数')
a+=1
a = 1
while a<5:
print('a<5')
a+=1
else:
print('a>5')九九乘法表
i = 1
while i<=9:
j = 1
while j<=i:
print(f"{j} * {i} = {i * j} \t", end="")
j+=1
i+=1
print()i = 1
while i<=9:
j = 1
while j<=i:
print(f"{j} * {i} = {i * j} \t", end="")
j+=1
i+=1
print()for
python
list1 = ['cao','lin','ch']
for i in list1:
print(i)
str = 'asdasfdfsd'
for i in str:
print(i)list1 = ['cao','lin','ch']
for i in list1:
print(i)
str = 'asdasfdfsd'
for i in str:
print(i)九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print(f"{j} * {i} = {i * j}\t",end = "")
print()for i in range(1,10):
for j in range(1,i+1):
print(f"{j} * {i} = {i * j}\t",end = "")
print()for else
for...else 语句用于在循环结束后执行一段代码。
str = 'asdasfdfsd'
for i in str:
print(i)
else:
print('循环结束了')for...else 语句用于在循环结束后执行一段代码。
str = 'asdasfdfsd'
for i in str:
print(i)
else:
print('循环结束了')break 与 continue
python
break是终止当前循环 continue是跳过当前这一轮循环继续下一轮循环
for 实例中使用了 break 语句,break 语句用于跳出当前循环体,不会执行 else 子句
sites = ["Baidu", "Google","Runoob","Taobao"]
for site in sites:
if site == "Runoob":
print("菜鸟教程!")
break
print("循环数据 " + site)
else:
print("没有循环数据!")
print("完成循环!")
#循环数据 Baidu
#循环数据 Google
#菜鸟教程!
#完成循环!break是终止当前循环 continue是跳过当前这一轮循环继续下一轮循环
for 实例中使用了 break 语句,break 语句用于跳出当前循环体,不会执行 else 子句
sites = ["Baidu", "Google","Runoob","Taobao"]
for site in sites:
if site == "Runoob":
print("菜鸟教程!")
break
print("循环数据 " + site)
else:
print("没有循环数据!")
print("完成循环!")
#循环数据 Baidu
#循环数据 Google
#菜鸟教程!
#完成循环!range() 函数
python
如果你需要遍历数字序列,可以使用内置 range() 函数。它会生成数列,例如
for i in range(10): #0 - 10 的数字 不包含10
print(i)
for i in range(1,10): #1 - 10 的数字 不包含10
print(i)如果你需要遍历数字序列,可以使用内置 range() 函数。它会生成数列,例如
for i in range(10): #0 - 10 的数字 不包含10
print(i)
for i in range(1,10): #1 - 10 的数字 不包含10
print(i)斐波那契额数列
python
a,b = 0,1
while b<20:
print(a,'a',b,'b')
a,b = b,a+b
n = 10
a,b = 0,1
for i in range(n):
print(b,end=',')
a,b = b,a+ba,b = 0,1
while b<20:
print(a,'a',b,'b')
a,b = b,a+b
n = 10
a,b = 0,1
for i in range(n):
print(b,end=',')
a,b = b,a+b推导式 逻辑缩写
python
>>> multiples = [i for i in range(30) if i % 3 == 0]
>>> print(multiples)
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
listdemo = ['Google','Runoob', 'Taobao']
# 将列表中各字符串值为键,各字符串的长度为值,组成键值对
>>> newdict = {key:len(key) for key in listdemo}
>>> newdict
{'Google': 6, 'Runoob': 6, 'Taobao': 6}>>> multiples = [i for i in range(30) if i % 3 == 0]
>>> print(multiples)
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
listdemo = ['Google','Runoob', 'Taobao']
# 将列表中各字符串值为键,各字符串的长度为值,组成键值对
>>> newdict = {key:len(key) for key in listdemo}
>>> newdict
{'Google': 6, 'Runoob': 6, 'Taobao': 6}函数
注意事项 类似 javascript
- 参数不需要,可以省略
- 返回值不需要,可以省略
def 函数名(接受参数):
函数体
return 返回值
函数名(传入参数)def 函数名(接受参数):
函数体
return 返回值
函数名(传入参数)海象运算符(Walrus Operator)
Python 3.8+ 的 := 运算符,可在表达式内部赋值:
python
# 传统写法
data = get_data()
if data:
print(data)
# 海象运算符
if (data := get_data()):
print(data)
# 在 while 循环中使用
while (line := file.readline()):
print(line)
# 在列表推导式中使用
results = [y for x in range(10) if (y := x * 2) > 5]
# 避免重复计算
# 传统写法需要调用两次 len()
if len(a) > 10:
print(f"长度: {len(a)}")
# 海象运算符只调用一次
if (n := len(a)) > 10:
print(f"长度: {n}")# 传统写法
data = get_data()
if data:
print(data)
# 海象运算符
if (data := get_data()):
print(data)
# 在 while 循环中使用
while (line := file.readline()):
print(line)
# 在列表推导式中使用
results = [y for x in range(10) if (y := x * 2) > 5]
# 避免重复计算
# 传统写法需要调用两次 len()
if len(a) > 10:
print(f"长度: {len(a)}")
# 海象运算符只调用一次
if (n := len(a)) > 10:
print(f"长度: {n}")global 与 nonlocal 关键词
使用 globa1 关键字 可以在函数内部声明变量为全局变量
python
num = 10
def text1():
print(num) 10
def text2():
global num
num = 20
text1()
text2()
print(num) 20num = 10
def text1():
print(num) 10
def text2():
global num
num = 20
text1()
text2()
print(num) 20
Cao Note