一、格式化输出
#1.利用百分号格式化输出
# name = 'alex'
# age = 18.56
# l = ['alex','eric']
# mesg = 'i am %s,my age is %s ' %(name,age)
# print(mesg) #%s可以输出一切类型
# mesg = 'i am %s,my age is %d ' %(name,age)
# print(mesg)
# mesg = 'i am %s,my age is %d %% ' %(name,age)
# print(mesg)
# mesg = 'i am %s,my age is %.1f %% ' %(name,age)
# print(mesg)
# mesg = 'i am %s,my age is %.1f %% ' %(name,age)
# print(mesg)
# mesg = 'i am %s,my age is %d,my friends are %s ' %(name,age,l)
# print(mesg)
# mesg = 'i am %(a)s,my age is %(b)d,my friends are %(c)s ' %({'a':name,'b':age,'c':l})
# print(mesg)
# print('root','password','user',123456,sep=':')
#2.利用format格式化输出
# name = 'alex'
# age = 18
# l = ['alex','eric']
# mesg = 'i am {},my age is {},my friends are {}.'.format(name,age,l)
# print(mesg)
# mesg = 'i am {2},my age is {1},my friends are {0}.'.format(name,age,l)
# print(mesg)
# mesg = 'i am {2},my age is {1},my friends are {0}.'.format(*[name,age,l])
# print(mesg)
# mesg = 'i am {:s},my age is {:d}, my friends are {}.'.format(name,age,l)
# print(mesg)
# mesg = 'i am {a},my age is {b},my friends are {c}.'.format(a=name,b=age,c=l)
# print(mesg)
# mesg = 'i am {a},my age is {b},my friends are {c}.'.format(**{'a':name,'b':age,'c':l})
# print(mesg)
# mesg = 'numbers:{:b},{:o},{:d},{:x},{:X},{:%}'.format(15,15,15,15,15,15.782,15)
# print(mesg)
# mesg = 'i am {1},my age is {1:d}, my friends are {1}.'.format(name,18,l)
# print(mesg)
二、函数基础
#函数:实现特定功能,代码重复使用,可扩展性,方便维护
def test(x,y,z): #x,y,z是形参,不占内存空间,只有调用函数时占用空间,而且函数运行完,释放空间
m = x+y+z
return m #可以返回多个值,返回多个值时,会用返回元组形式
a = 1
b = 2
c = 3
print(test(a,b,c)) #a,b,c是实参
#位置参数按顺序一一对应;指定参数可以不按顺序一一对应;位置参数必须在指定参数的左边;参数名字不可重复
print(test(a,b,z=4))
#默认参数
def test1(x,y=10): #x,y是默认参数为10
m = x+y
return m
print(test1(1))
print(test1(1,3))
#参数组,*args:读列表,**kwargs:读字典
def test2(x,*args,**kwargs):
print(x)
print(args)
print(kwargs)
test2(1,33,45,'a','b',y=1,z=2)
test2(1,*[33,45,'a','b'],**{'y':1,'z':2})
test2(1,*(33,45,'a','b'),**{'y':1,'z':2})