1、函数定义
1 def fun(): 2 函数逻辑语句 3 4 5 比如: 6 def list_7(): 7 ll = [] 8 for i in range(101): 9 if i % 7 ==0: 10 ll.append(i) 11 return ll 12 13 def定义函数使用的关键字,list_7函数名, 14 ''' 15 ll = [] 16 for i in range(101): 17 if i % 7 ==0: 18 ll.append(i) 19 return ll 20 ''' 函数逻辑语句
2、函数参数
1 def fun1(x,y,z): 2 print(x+y+z) 3 4 def fun2(x,y,z): 5 return x+y+z 6 7 以上两个函数括号中的x、y、z就是对应的参数
3、函数调用
1 def fun1(x,y,z): 2 print(x+y+z) 3 4 #调用函数fun1 5 a = fun1(1,2,3) 6 7 def fun2(x,y,z): 8 return x+y+z 9 10 #调用函数fun2 11 b = fun2(1,2,3) 12 13 c = fun2(y=2,z=5,x=10)
#以上列子中,fun1是函数执行过程中输出1,而fun2是函数执行时会返回一个值,这个值可以用一个变量来接收,上述的fun1和fun2中的x、y、z是必备参数,即在函数调用时必须传值
4、函数中的print和return区别
1 def fun3(x,y=5): 2 return x + y 3 4 def fun4(x,y=5): 5 print(x + y) 6 7 result01 = fun3(3,4) 8 9 result02 = fun4(3,4)
相同:print()和return都不能用赋值语句,而是用表达式;
不同:print是在函数执行过程中输出,return是在函数执行时返回一个值,这个值一般调用会用一个变量来接受,return执行完函数生命周期意味着结束
5、必备参数
def fun3(x,y=5): return x + y
#调用函数 result = fun3(2)
result = fun3(2,5) #函数fun3中,x是必备参数,y是默认参数,在调用函数时,y的值可传可不传,但是x的值必须传
6、关键字参数
1 def fun2(x,y,z): 2 return x+y+z 3 4 S = fun2(y=2,z=5,x=10) 5 print(S) 6 #输出结果:17 7 8 9 #不定长参数(元组类型) 10 def fun4(*arg): 11 print(arg) 12 13 T=fun4(1) 14 #输出结果: >>>(1,) 15 >>> fun4(1,2,3) 16 (1, 2, 3) 17 >>>
#输出结果为元组,所以传入参数的个数现在变成了不定长,另外参数类型的问题在定义时没有关系,即跟*arg没关系,也就是说在定义时便会要求你传入参数的类型是什么,只是跟函数里面的语句有关系。
比如:
>>> fun4(1,2,3,4,'a','b',)
(1, 2, 3, 4, 'a', 'b')
>>>
#字典形式的参数**kwarg def fun5(**kwarg): print(kwarg) >>> fun5(a=1,b=2,c=20) {'a': 1, 'b': 2, 'c': 20} >>>
7、传入参数举例
1 # 传入参数举例 2 >>> def fun1(x,y,z): 3 return x+y+z 4 5 #调用函数,传入元组 6 >>> tp=5,6,7 7 >>> tp 8 (5, 6, 7) 9 >>> 10 >>> fun1(*tp) 11 18 12 >>> 13 14 #调用函数传入字典 15 >>> d = {'z':9,'y':3,'x':4} 16 >>> d 17 {'z': 9, 'y': 3, 'x': 4} 18 >>> fun1(**d) 19 16 20 >>> 21 22 #注意,在传入参数时,不论是传入的是字典还是元组,参数的个数和名称必须与函数中的参数要一一对应,否则就会失败 23 24 >>> def fun(a,b=2,*c,**d): 25 print(a,b,c,d) 26 27 28 >>> fun(1) 29 1 2 () {} 30 >>> 31 >>> fun('a','b','c','d') 32 a b ('c', 'd') {} 33 >>> 34 #要想将d传入到字典里,可以这样操作: 35 >>> fun('a','b','c',z='d') 36 a b ('c',) {'z': 'd'} 37 >>> 38 >>> fun('a',x='b',y='c',z='d') 39 a 2 () {'x': 'b', 'y': 'c', 'z': 'd'} 40 >>> 41 42 #不定长参数实现sum函数功能 43 >>> def fun5(*arg): 44 s = 0 45 for i in arg: 46 s += i 47 return s 48 49 >>> fun5(2,3,4,54,3,21,3) 50 90 51 >>> 52 53 54 #进一步对比,参数只设置一个,但是参数必须是可迭代的对象 55 >>> def fun6(x): 56 s = 0 57 for i in x: 58 s +=i 59 return s 60 61 >>> fun6(9) 62 Traceback (most recent call last): 63 File "<pyshell#87>", line 1, in <module> 64 fun6(9) 65 File "<pyshell#86>", line 3, in fun6 66 for i in x: 67 TypeError: 'int' object is not iterable 68 >>> fun6([1,2,3,4,5,6]) 69 21 70 >>> fun6((1,2,3,4,5,6)) 71 21 72 >>> 73 #从上述表达式可以发现fun6函数里面必须传可以迭代的数据类型,由于是做加法,所以只可以传元组和列表
8、其他案例
# 字典的迭代
a={'a':1,'b':2,'c':3} for i in a: print(i) print('对字典a进行遍历') for key in a.keys(): print(key) print('-----键-----------') for value in a.values(): print(value) print('------值-----------') for items in a.items(): print(items) print('-----键值对--------') #100以内数字相加 s = 0 for i in range(101): s += i print(s) 或者 sum(range(101)) # 九九乘法表 for i in range(1,10): for j in range(1,i+1): print('%s x %s =%2s'%(j,i,j*i),end=' ') print('换行') #100以内被7整除的数 def num(n): num_list=[] for i in range(n): if i % 7 == 0: num_list.append(i) return num_list print(num(100)) #100以内的偶数 方法①
x=[] n=0 while n<101: x.append(n) n+=2
方法② y=[] for i in range(101): if i%2 ==0: y.append(i)
#100以内整数求和 s = 0 def Sum(*kwg): s += kwg return s Sum((1,2,3,4,5,6,7))
#一个整数,它加上100和加上268后都是一个完全平方数,请问该数是多少?
注:一个数如果是另一个整数的完全平方,我们称这个数是完全平方数
例如:1,4,9,16,25,36,49·····
x +100 =y*y x+268=z*z
>>> for y in range(100):
for z in range(100):
if y*y -100 == z*z -268:
print(y*y-100)
-99
21
261
1581
>>>