Python 函数、循环语句
- 定义函数、函数调用
-
- 函数的参数
- [return 返回值、全局变量及局部变量](#return 返回值、全局变量及局部变量)
- 循环语句
-
- [while 循环](#while 循环)
- [for 循环](#for 循环)
定义函数、函数调用
python
# 定义函数
# def 函数名(参数):
def printme(str):
"打印传入的字符串到标准显示设备上" #函数说明
print(str)
return # 结束函数,不带表达式,相当于返回 None
#函数调用:函数名(参数)
printme("我要调用用户自定义函数!") # 我要调用用户自定义函数!
printme("再次调用同一函数") # 再次调用同一函数
函数的参数
(1)必备参数
(2)关键字参数
(3)默认参数
(4)不定长参数
(5)匿名参数
python
# 必备参数:函数(参数1,参数2,...),多个参数时必须按顺序写,且参数个数必须要够
# 关键字参数:函数(参数1=值,参数2=值,...)如:printme(str=123),多个参数时,参数顺序不再重要
# 默认参数:定义函数时,def 函数名(参数=值);参数值没有传入,则被认为是默认值
def printinfo(name,age=10):
print("name:",name,"age:",age)
return
# 必备参数
printinfo("apple",18) # name: apple age: 18
# 关键字参数
printinfo(age=20, name="orange") # name: orange age: 20
#默认参数
printinfo("banana")# name: banana age: 10
# 不定长参数;比声明更多的参数,声明时不会命名,加星号*的变量名存放所有未命名的变量参数
def printInfo(arg1, *vartrple):
"打印任何输入"
print("输出",arg1)
for var in vartrple:
print(var)
return
printInfo(10) # 输出 10
printInfo(70,60,50)
"""
输出 70
60
50
"""
# 匿名参数 lambda,逻辑不复杂时使用
# lambda 只是一个表达式,不是代码块
sum = lambda arg1,arg2: arg1+arg2
print("相加后的值:",sum(10,20)) # 相加后的值: 30
return 返回值、全局变量及局部变量
python
total = 0 #全局变量
def sum(arg1,arg2):
total = arg1+arg2 # 局部变量
print("函数内的全局变量:", total) # 函数内的全局变量: 30
return total # return 返回值
print(sum(10,20)) # 30
print("函数外的全局变量:", total) # 函数外的全局变量: 0
循环语句
while 循环
python
# while 循环语句,判断条件为 false 则结束循环
# continue 跳过本次循环
# break 退出循环
i = 1
while i < 10:
i += 1
if i%2 > 0: #非双数时跳过输出
continue
print(i) # 输出双数 2、4、6、8、10
i = 1
while 1: #循环条件为1必定成立
print(i) #输出1-10
i +=1
if i > 10: #当i大于10时跳出循环
break
# while...else 在循环条件为false时,执行else语句(只执行1次)
count = 0
while count < 5:
print(count,"is less than 5")
count = count + 1
else:
print(count,"is not less than 5")
"""
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
"""
for 循环
python
# for 循环语句,可遍历任何序列的项目,如:列表、字符串
# continue 跳过本次循环
# break 退出循环,循环没执行完也会停止
for letter in "Python":
print("当前字母:",letter)
fruits = ["apple","banana","orage"]
for fruit in fruits:
print("当前水果:",fruit)
print("Good bye!")
"""
当前字母: P
当前字母: y
当前字母: t
当前字母: h
当前字母: o
当前字母: n
当前水果: apple
当前水果: banana
当前水果: orage
Good bye!
"""
# 通过序列索引迭代:遍历循环 for index in range(len(列表))
fruits = ["apple","banana","orage"]
for index in range(len(fruits)): # range(3) 依次取 0,1,2
print("当前水果:",fruits[index])
"""
当前水果: apple
当前水果: banana
当前水果: orage
"""
# 循环使用 else 语句
# for...else 表示for中语句和普通没有区别,else会在循环正常执行完(即 for不是通过break跳出而中断的)情况下执行
# while...else也是一样
for num in range(10,20): #迭代10到20之间的数字
for i in range(2,num): # 根据因子迭代
if num%i == 0: #确定第一个因子
j=num/i #计算第二个因子
print('%d 等于 %d * %d' % (num,i,j))
break #跳出当前循环
else: #循环的else部分
print(num,'是一个质数')
"""
10 等于 2 * 5
11 是一个质数
12 等于 2 * 6
13 是一个质数
14 等于 2 * 7
15 等于 3 * 5
16 等于 2 * 8
17 是一个质数
18 等于 2 * 9
19 是一个质数
"""
# 循环嵌套
# 在一个循环体里面嵌入另外一个循环,如:while循环嵌入for循环,for循环嵌入while循环
i = 2
while(i < 10):
j = 2
while(j <= (i/j)):
if not(i%j): break
j = j + 1
if (j > i/j): print(i," 是素数")
i = i + 1
print("Good bye!")
"""
2 是素数
3 是素数
5 是素数
7 是素数
Good bye!
"""