#计算x的n次方 def power(x,n=0): if x==0: return 0 result = 1 while n>0: n-=1 result*=x return result print(power(2,4)) #可变参数,自动把参数封装成数组,计算传入参数的平方和 def calc(*numbers): sum = 0 for i in numbers: sum+=(i*i) return sum print(calc(1,2)) #传入数组 list=[1,2,3] print(calc(*list)) #关键字参数 def student(name,age,**other): print('name:',name,'age:',age,'other:',other) student('tom',18) student('tom',18,city='Hangzhou',provice='Zhejiang') student('tom',18,**{'city':'GuangZhou'}) #命名关键字参数,用*隔开,调用时候必须传入名称 def student(name,age,*,city,job): print('name:',name,'age:',age,'city:',city,'job:',job) student('jack',20,city='Beijing',job='Teacher')
C:\Users\Administrator.000\AppData\Local\Programs\Python\Python36\python.exe E:/python/03函数.py
16
5
14
name: tom age: 18 other: {}
name: tom age: 18 other: {'city': 'Hangzhou', 'provice': 'Zhejiang'}
name: tom age: 18 other: {'city': 'GuangZhou'}
name: jack age: 20 city: Beijing job: Teacher
Process finished with exit code 0