-
對于一個擁有必需參數,默認參數,可變參數,可變關鍵字參數的函數,定義順序是這樣的:
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 , 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)
查看全部
舉報