最赞回答 / qq_慕粉4217309
L = [95.5, 85, 59, 66, 72]L.sort()print(L)print(L[-3:5])这样就好了,因为【:】是从左往右看的
2022-07-28
def sub_sum(L):
result_odd=[]
result_even=[]
for i in L:
if i%2==1:
result_odd.append(i)
else:
result_even.append(i)
return sum(result_odd),sum(result_even)
L = [1, 3, 5, 7, 9]
print(sub_sum(L))
result_odd=[]
result_even=[]
for i in L:
if i%2==1:
result_odd.append(i)
else:
result_even.append(i)
return sum(result_odd),sum(result_even)
L = [1, 3, 5, 7, 9]
print(sub_sum(L))
2022-07-26
def square_of_sum(L):
result=0
for i in L:
result=result+i*i
return result
L = [1, 3, 5, 7, 9, 11]
print(square_of_sum(L))
result=0
for i in L:
result=result+i*i
return result
L = [1, 3, 5, 7, 9, 11]
print(square_of_sum(L))
2022-07-26
# Enter a code
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
same=[]
print(s1.isdisjoint(s2))
for i in s1:
for j in s2:
if i==j:
same.append(i)
print(same)
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
same=[]
print(s1.isdisjoint(s2))
for i in s1:
for j in s2:
if i==j:
same.append(i)
print(same)
2022-07-26
a是字符所以a=True,所以a or 'world'=a 根据与运算法则返回world结果是hello,python
b是空字符串所以b=False,所以b or 'world'根据or运算法返回world,所以结果是hello,world
b是空字符串所以b=False,所以b or 'world'根据or运算法返回world,所以结果是hello,world
2022-07-26
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9, 11])
for i in L:
if i in S:
S.remove(i)
else:
S.add(i)
print(S)
S = set([1, 3, 5, 7, 9, 11])
for i in L:
if i in S:
S.remove(i)
else:
S.add(i)
print(S)
2022-07-25
names = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']
names_set = set(names)
print(names_set)
x="alice"
for i in names_set:
if x.capitalize()==i:
print("True")
names_set = set(names)
print(names_set)
x="alice"
for i in names_set:
if x.capitalize()==i:
print("True")
2022-07-25
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
for i in d:
for j in d[i]:
print(i,j)
for i in d:
for j in d[i]:
print(i,j)
2022-07-22
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
print(d.items())
for i,value in d.items():
print(i,value)
print(d.items())
for i,value in d.items():
print(i,value)
2022-07-22
最赞回答 / weixin_精慕门7014924
sum是你可以自己指定的变量(它甚至可以不叫sum,只要是其他任意符合python命名规范的变量名都可以)。在求和时,因为0加上任何数都不会改变其值,所以将sum设置为0是可行的(当然设置为1也是可行的,得到的结果是一样的);在求乘积时,当然不可以乘0,所以就直接设sum的初始值为1啦。一句话概括,就是你想让sum是什么值它就可以是什么值,只要保证结果正确。
2022-07-20
# Enter a code
count = 10;
this_num = 0;
while this_num < count:
print(this_num)
this_num+=1
count = 10;
this_num = 0;
while this_num < count:
print(this_num)
this_num+=1
2022-07-19
# Enter a code
L = [[75, 92, 59, 68, 99],[8, 2, 6, 4, 8],[715, 952, 589, 698, 919]]
for data in L:
for num in data:
print(num)
L = [[75, 92, 59, 68, 99],[8, 2, 6, 4, 8],[715, 952, 589, 698, 919]]
for data in L:
for num in data:
print(num)
2022-07-19
# coding: utf-8
kscj=58
if kscj < 60:
print('不及格')
elif kscj >= 60 and kscj <80 :
print("及格")
elif kscj >= 80 and kscj <90:
print("优秀!")
elif kscj >= 90 :
print("顶尖!")
kscj=58
if kscj < 60:
print('不及格')
elif kscj >= 60 and kscj <80 :
print("及格")
elif kscj >= 80 and kscj <90:
print("优秀!")
elif kscj >= 90 :
print("顶尖!")
2022-07-19
# coding: utf-8
age = 16
if age>18:
print("恭喜你成年了!")
else:
print("小朋友未成年哦!!")
age = 16
if age>18:
print("恭喜你成年了!")
else:
print("小朋友未成年哦!!")
2022-07-19