直接使用 get 方法如果找不到默認返回 None 的特性,代碼如下。
d = {
'Alice': 45,
'Bob': 60,
'Candy': 75,
'David': 86,
'Ellena': 49
}
names = ["Alice", "Bob", "Candy", "Mimi", "David"]
for name in names :
print(d.get(name))
d = {
'Alice': 45,
'Bob': 60,
'Candy': 75,
'David': 86,
'Ellena': 49
}
names = ["Alice", "Bob", "Candy", "Mimi", "David"]
for name in names :
print(d.get(name))
2023-12-13
print(r'''"To be, or not to be": that is the question.
Whether it's nobler in the mind to suffer.''')
Whether it's nobler in the mind to suffer.''')
2023-12-07
方式1:
template='life {0}, {1} Python.'
a='is short'
b='you need'
result=template.format(a,b)
print(result)
方式2:
template='life {o}, {t} Python.'
one='is short'
two='you need'
result=template.format(o=one,t=two)
print(result)
template='life {0}, {1} Python.'
a='is short'
b='you need'
result=template.format(a,b)
print(result)
方式2:
template='life {o}, {t} Python.'
one='is short'
two='you need'
result=template.format(o=one,t=two)
print(result)
2023-11-30
d = {
'Alice': 45,
'Bob': 60,
'Candy': 75,
'David': 86,
'Ellena': 49
}
if 'Alice' in d:
d.pop('Alice')
print(d)
else:
print("don't have Alice")
'Alice': 45,
'Bob': 60,
'Candy': 75,
'David': 86,
'Ellena': 49
}
if 'Alice' in d:
d.pop('Alice')
print(d)
else:
print("don't have Alice")
2023-11-28
i = 0
sum = 0
while True:
if i > 1000:
break
else:
if i % 2 == 0:
sum += i
i += 1
print(sum)
sum = 0
while True:
if i > 1000:
break
else:
if i % 2 == 0:
sum += i
i += 1
print(sum)
2023-11-23
num = 0 # 初始化num用于存放偶數和
for i in range(0, 1001, 1): # 利用range從0開始循環到1001,每次遞增1,循環到1001等同于i<=1000
if i % 2 != 0: # 如果i除以2的余數不為0則說明i不能被2整除,此時i的值為奇數
continue # 不能被整除則跳過當前循環
num += i # 將i的值存到num中
print(num) # 輸出num的值,也就是1000以內所有偶數的值
for i in range(0, 1001, 1): # 利用range從0開始循環到1001,每次遞增1,循環到1001等同于i<=1000
if i % 2 != 0: # 如果i除以2的余數不為0則說明i不能被2整除,此時i的值為奇數
continue # 不能被整除則跳過當前循環
num += i # 將i的值存到num中
print(num) # 輸出num的值,也就是1000以內所有偶數的值
2023-11-22
template = 'Life is {0},you need {1}'
result = template.format('short','python')
print(result)
template = 'Life is {s} , you need {p}'
short ='short'
python = 'python'
result = template.format(s =short , p = python)
print(result)
result = template.format('short','python')
print(result)
template = 'Life is {s} , you need {p}'
short ='short'
python = 'python'
result = template.format(s =short , p = python)
print(result)
2023-11-16