Python随笔(三)、python基础
def show(a1,a2): print(a1,a2)show(a2=33334,a1=555556)
动态参数,个数无限制
def show(**arg):
print(arg,type(arg))
show(a1=123,a2=456,a3=789)
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
{'a1': 123, 'a2': 456, 'a3': 789} <class 'dict'>
动态参数(强强联合):
def show(*args,**kwargs):
print(args,type(args))
print(kwargs,type(kwargs))
show(11,222,33,44,n1=88,alex="sb")
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
(11, 222, 33, 44) <class 'tuple'>
{'n1': 88, 'alex': 'sb'} <class 'dict'>
def show(*args,**kwargs):
print(args,type(args))
print(kwargs,type(kwargs))
l =
d = {'n1':88,'alex':'sb'}
show(l,d)
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
(, {'n1': 88, 'alex': 'sb'}) <class 'tuple'>
{} <class 'dict'>
如果我想把l放入列表里面,把d放入字典里面,则:
def show(*args,**kwargs):
print(args,type(args))
print(kwargs,type(kwargs))
l =
d = {'n1':88,'alex':'sb'}
show(*l,**d)
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
(11, 22, 33, 44) <class 'tuple'>
{'n1': 88, 'alex': 'sb'} <class 'dict'>
22 python s12 day3 使用动态参数实现字符串格式化
1、
alex = sb的三种方式:
s1 = "{name} is {acter}"
d = {'name':'alex','acter':'sb'}
#result = s1.format(name='alex',acter='sb')
result = s1.format(**d)
print(result)
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
alex is sb
2、字符串格式化
s1 = "{name} is {acter}"
result = s1.format(name='alex',acter='sb')
print(result)
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
alex is sb
3、
s1 = "{0} is {1}"
l = ['alex','sb']
result = s1.format('alex','sb')
result = s1.format(*l)
print(result)
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
alex is sb
23 python s12 day3 Python lambda表达式
def func(a):
a +=1
return a
result = func(4)
print(result)
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
5
lambda表达式,简单函数的表示方式:
func = lambda a: a+1
#创建形式参数a
#函数内容a+1,并把结果return
ret = func(99)
print(ret)
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
100
24、内置函数:
#绝对值:
a = -100
b = a.__abs__()
print(b)
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
100
更多详见:http://www.runoob.com/python/python-built-in-functions.html
map的巧用:
li =
new_li = map(lambda x:x+100,li)
l = list(new_li)
print(l)
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2017-12-10/2017-12-19/s4.py
页:
[1]