'''
列表(List)推导式(返回一个新的List)
'''
names =['Amy','Bob','Alice']
new_names = [name.upper() for name in names]
print(new_names)
new_names = [name.upper() for name in names if len(name)>3]
print(new_names)
'''
字典(Map)推导式(返回一个新的Map)
'''
names =['Amy','Bob','Alice']
new_names = {name:len(name) for name in names}
print(new_names)
new_names = {name:len(name) for name in names if len(name)>3}
print(new_names)
'''
集合(Set)推导式(返回一个新的Set)
'''
names =['Amy','Bob','Alice']
new_names = {name for name in names}
print(new_names)
'''
元组(只读列表,Tuple)推导式(返回一个新的Tuple)
'''
names =['Amy','Bob','Alice']
new_names = (name for name in names)
print(new_names)
print('我叫 %s,今年 %s 岁' % ('小明',10))
'''
%s 格式化字符串
%d 格式化整数
%f 格式化浮点数(可指定小数点后精度,如%2f、%3f)
...
'''
print('我叫{}'.format('小明'))
print('我叫{name},今年{age}岁'.format(name='小明',age=10))
print('我叫{0},今年{1}岁'.format('小明',10))
print('圆周率={0:.3f}'.format(math.pi))
print('{name:10} ===> {age:10}'.format(name='小明',age=10))
str = """这是一个多行字符串实例
哈哈哈
怎么样
"""
my_name = 'john'
my_map = {'age':10,'parent':'Smith'}
print(f'my name is {my_name}')
print(f'I am {my_map["age"]} years old.My parent is {my_map["parent"]}')
> https://docs.python.org/zh-cn/3.12/tutorial/inputoutput.html
#字符串常用函数
len(x) #返回字符串x的长度
x.find(y) #检查子字符串y是否在x中,若存在则返回索引值,不存在返回-1
isalpha(x) #检查是否都是字母或中文,是则返回true
isdigit(x)、isnumeric(x) #检查是否都是数字,是则返回true
'abc'.format() # 格式化输出(重要)
'abc'.rjust() # 在左侧填充空格,对给定宽度字段中的字符串进行右对齐
def test1(arg1):
print(arg1)
def test2(arg1):
print(arg1)
return arg1
test2(arg1=1)
def test3(arg1=0):
print(arg1)
test3()
def test4(arg1,*arg2):
print(arg1)
print(arg2)
test4(10,20,30,40)
def test5(arg1,**arg2):
print(arg1)
print(arg2)
test5(10,{'a':20,'b':30})
x = lambda a : a+10
print(x(5))
x = lambda a,b : a+b
print(x(1,2))