s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
list_1 = s1 & s2
print(list_1)
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
list_1 = s1 & s2
print(list_1)
2022-06-08
def average(*args):
l=len(args)
if l==0:
return 0
sum=0
for a in args:
sum+=a
return sum/float(l);
print(average(1,2,3,4))
print(average())
l=len(args)
if l==0:
return 0
sum=0
for a in args:
sum+=a
return sum/float(l);
print(average(1,2,3,4))
print(average())
2022-06-07
def sub_sum(L):
j_sum=0
o_sum=0
for index in range(len(L)):
if index % 2 == 0:
j_sum += L[index]
else:
o_sum += L[index]
return j_sum, o_sum
lv = [1,2]
j_sum, o_sum = sub_sum(lv)
print('奇數和 = {}'.format(j_sum))
print('偶數和 = {}'.format(o_sum))
j_sum=0
o_sum=0
for index in range(len(L)):
if index % 2 == 0:
j_sum += L[index]
else:
o_sum += L[index]
return j_sum, o_sum
lv = [1,2]
j_sum, o_sum = sub_sum(lv)
print('奇數和 = {}'.format(j_sum))
print('偶數和 = {}'.format(o_sum))
2022-06-02
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
if s1.isdisjoint(s2):
pass
else:
print(s1&s2)
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
if s1.isdisjoint(s2):
pass
else:
print(s1&s2)
2022-06-02
上面有個地方缺了一個縮進:from decimal import Decimal,ROUND_HALF_UP
def smart_round(x,n):
return str(Decimal(x).quantize(Decimal("0."+"0"*n),rounding=ROUND_HALF_UP))
def smart_round(x,n):
return str(Decimal(x).quantize(Decimal("0."+"0"*n),rounding=ROUND_HALF_UP))
2022-05-29
A = 'Life is {s},you need {p}'
short = 'short'
python = 'Python'
a = A.format(s = short,p = python)
print(a)
B = 'Life {1} short,{2} need {0}'
b = B.format('Python','is','you')
print(b)
short = 'short'
python = 'Python'
a = A.format(s = short,p = python)
print(a)
B = 'Life {1} short,{2} need {0}'
b = B.format('Python','is','you')
print(b)
2022-05-29
實現任務最后一步輸出時可以這樣:print(round(s,2)),但是我第一次做的時候寫成了3,發現結果和2一樣,省略了末位的0,于是搜索了一下,要實現保留精確度到指定長度,自建了一個函數,但對我這個入門級來說,這個函數我看不懂了,就先放著,以后看得懂了再來說說:
from decimal import Decimal,ROUND_HALF_UP
def smart_round(x,n):
return str(Decimal(x).quantize(Decimal("0."+"0"*n),rounding=ROUND_HALF_UP))
輸出時,round改為smart_round
from decimal import Decimal,ROUND_HALF_UP
def smart_round(x,n):
return str(Decimal(x).quantize(Decimal("0."+"0"*n),rounding=ROUND_HALF_UP))
輸出時,round改為smart_round
2022-05-29
def square_of_sum(L):
result=0
for num in L:
result=result+num*num
return result
L=[1,3,5,7,9]
result=square_of_sum(L)
print(result)
result=0
for num in L:
result=result+num*num
return result
L=[1,3,5,7,9]
result=square_of_sum(L)
print(result)
2022-05-25
d = {
'Alice': 45,
'Bob': 60,
'Candy': 75,
'David': 86,
'Ellena': 49
}
old_score=d['Alice']
print("Alice's old score is {}"
.format(old_score))
d['Alice']=60
print(d)
'Alice': 45,
'Bob': 60,
'Candy': 75,
'David': 86,
'Ellena': 49
}
old_score=d['Alice']
print("Alice's old score is {}"
.format(old_score))
d['Alice']=60
print(d)
2022-05-25
d = {
'Alice': [45],
'Bob': [60],
'Candy': [75],
}
A=[50, 61, 66]
B=[80, 61, 66]
C=[88, 75, 90]
i=0
while i<3:
d['Alice'].append(A[i])
d['Bob'].append(B[i])
d['Candy'].append(C[i])
i+=1
print(d)
'Alice': [45],
'Bob': [60],
'Candy': [75],
}
A=[50, 61, 66]
B=[80, 61, 66]
C=[88, 75, 90]
i=0
while i<3:
d['Alice'].append(A[i])
d['Bob'].append(B[i])
d['Candy'].append(C[i])
i+=1
print(d)
2022-05-25
# Enter a code
a=1
b=1
while a<=10:
a=a+1
b=b*a
print(b)
a=1
b=1
while a<=10:
a=a+1
b=b*a
print(b)
2022-05-24