runfile('C:/Users/86178/.spyder-py3/temp.py', wdir='C:/Users/86178/.spyder-py3')
def calc(*number):
sum = 0
for n in number:
sum+=n*n
return sum
calc(1,2)
Out[2]: 5
calc(1,2,3,4,5)
Out[3]: 55
calc()
Out[4]: 0
nums=[1,2,3]
calc(nums[0],nums[1],nums[2])
Out[6]: 14
nums=[1,2,3]
calc(*nums)
Out[8]: 14
def person(name,age,**kw)
File "<ipython-input-9-8392d7c920c8>", line 1
def person(name,age,**kw)
^
SyntaxError: invalid syntax
def person(name,age,**kw):
print('name:',name,'age:',age,'other:',kw
)
person('Micheal',30)
name: Micheal age: 30 other: {}
person('Bob',35,city='Beijing')
name: Bob age: 35 other: {'city': 'Beijing'}
person('Adam',45,gender='M',job='Engineer')
name: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}
extra = {'city':'Beijing','job':'Engineer'}
person('Jack',24,city=extra['city'],job=extra['job']
)
name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}
extra = {'city':'Beijing','jib':'Engineer'}
person('Jack',24,**extra)
name: Jack age: 24 other: {'city': 'Beijing', 'jib': 'Engineer'}
def person(name,age,**kw):
if'city'in kw:
pass
if'job'in kw:
pass
print('name:',name,'age:',age,'other:',kw)
person('Jack',24,city='Beijing',addr='Chaoyang',zipcode=123456)
name: Jack age: 24 other: {'city': 'Beijing', 'addr': 'Chaoyang', 'zipcode': 123456}
def person(name,age,*,city,job):
print (name,age,city,job)
person('Jack',24,city='Beijing',job='Enigneer')
Jack 24 Beijing Enigneer
def person(name,age,*args,city,job):
print(name,age,args,city,job)
|