-
对于一个拥有必需参数,默认参数,可变参数,可变关键字参数的函数,定义顺序是这样的:
def func(param1, param2, param3 = None, *args, **kwargs):
print(param1)
print(param2)
print(param3)
print(args)
print(kwargs)
func(100, 200, 300, 400, 500, name = 'Alice', score = 100)
# ==> 100
# ==> 200
# ==> 300
# ==> (400, 500)
# ==> {'name': 'Alice', 'score': 100}当然,这么多类型的参数,很容易导致出错,在实际使用上,不建议定义这么多的参数。
查看全部 -
初学者常规写法:
list = 0
for i in range(1,101):
list += i**2
print(list)列表推导式的写法,代码量比较少,更直观
num = [i * i for i in range(1,101)]
a = sum(num)
print(a)生成器的写法,占用内存更低,写法如下
num = sum(i * i for i in range(1,101))
print(num)查看全部 -
有时候需要判断两个集合是否有重合的地方,如果使用传统的方法,需要使用for循环一个一个的去判断,非常麻烦,set提供isdisjoint()方法,可以快速判断两个集合是否有重合,如果有重合,返回False,否则返回True。
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
s3 = s1.isdisjoint(s2)
if not s3:
for item in s1:
if item not in s2:
continue
print(item,s3)判断S1与S2的集合是否有重合
如果有,返回false,并且打印重合项
查看全部 -
针对以下set,给定一个list,对于list里面的每个元素,如果set中包含这个元素,就将其删除,否则添加到set里面去。
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9, 11])
for item in L: 使用for循环,新变量在L中循环
if item in S: 当新变量中的元素也在S中,
S.remove(item) 删除相同的新变量,
else:
S.add(item) 不同的新变量直接加入到S的集合中
print (S)查看全部 -
set中如果有重复的元素,在输出的时候数据是按照顺序,并且重复的只取1个
s = set([1, 4, 3, 2, 5, 4, 2, 3, 1])
print(s) # ==> set([1, 2, 3, 4, 5])查看全部 -
如果模板中{}比较多,则容易错乱,那么在format的时候也可以指定模板数据内容的顺序。
# 指定顺序
template = 'Hello {0}, Hello {1}, Hello {2}, Hello {3}.'
result = template.format('World', 'China', 'Beijing', 'imooc')
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.
# 调整顺序
template = 'Hello {3}, Hello {2}, Hello {1}, Hello {0}.'
result = template.format('World', 'China', 'Beijing', 'imooc')
print(result) # ==> Hello imooc, Hello Beijing, Hello China, Hello World.# 指定{}的名字w,c,b,i
template = 'Hello {w}, Hello {c}, Hello {b}, Hello {i}.'
world = 'World'
china = 'China'
beijing = 'Beijing'
imooc = 'imooc'
# 指定名字对应的模板数据内容
result = template.format(w = world, c = china, b = beijing, i = imooc)
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.课后任务
print('Life is short,you need{}'.format('Python'))
print('life is short,you need {launguage}'.format(launguage='Python'))
查看全部 -
raw字符串受用
print(r'''写任意字符串’‘’)
这其中写什么东西都不会被定义为阅读终止除了三个单引号!!!
查看全部 -
L=['alice',66,'bob',"true",'false',100]
print(L)
L=['Alice','Chinese:92''Msth:75','English:99']
print(L)
查看全部 -
# coding: utf-8
s1='this is an english string'
s2='这是中文字符串'
s3='hello world'
s4='世界你好'
print(s1)
print(s2)
print(s3)
print(s4)
template='{s1}{s2}{s3}{s4}.'
s1='这是一句中英文混合的'
s2='Python'
s3='字符串:'
s4='Hello World'
result=template.format(s1='这是一句中英文混合的',s2='Python',s3='字符串:',s4='Hello World' )
print(result)
template='{s1}{s2}{s3}{s4}.'
result=template.format(s1='这是一句中英文混合的',s2='Python',s3='字符串:',s4='Hello World' )
print(result)
template = "This is {s}, you need{p}."
result = template.format(s='short', p=' python')
print(result)
template = "{t}, {y}."
result = template.format(t='this is short', y='you need python')
print(result)
查看全部 -
s1='ABC'
s2='123'
for x in s1:
for y in s2:
print(x+y)
s1='ABC'
s2='123'
s3='xyz'
for x in s1:
for y in s2:
for z in s3:
print(x+y+z)
查看全部 -
len() 元素有多少个
查看全部 -
dict中元组tuple,可以作为KEY
但是list不可以作为dict的key,不然会报错
查看全部 -
dict查询的速度快,不论是10个元素还是10完个元素查询的速度是一样的,单是缺点是占用内存大
list的正好相反,占用内存小,但是呢,查询速度慢
查看全部 -
d = {
'Alice': 45,
'Bob': 60,
'Candy': 75,
'David': 86,
'Ellena': 49
}
old_score = d.get('Alice')
d['Alice'] = 60
print(old_score)python中程序代码是从上到下运行,所以。先用个变量赋值取的就成绩
然后再降新值正价到字典当中更新并取代旧的值,
然后再打印输出旧的科目成绩
查看全部 -
a=“hello world”
print=(a)
查看全部
举报