d = {
'Alice': [50, 61, 66],
'Bob': [80, 61, 66],
'Candy': [88, 75, 90]
}
for key, value in d.items():
index = 1
for item in value:
score = item
print('{name} 第{index}次成績:{score}'.format(name = key, index = index, score = score))
index += 1
'Alice': [50, 61, 66],
'Bob': [80, 61, 66],
'Candy': [88, 75, 90]
}
for key, value in d.items():
index = 1
for item in value:
score = item
print('{name} 第{index}次成績:{score}'.format(name = key, index = index, score = score))
index += 1
2024-06-19
L = [[1, 2, 3], [5, 3, 2], [7, 3, 2]]
M = []
for list in L:
num = 1
for item in list:
num = num * item
M.append(num)
print(M)
M = []
for list in L:
num = 1
for item in list:
num = num * item
M.append(num)
print(M)
2024-06-19
了解了true和false的判定規則會更容易理解這節課講的內容。
以下對象在布爾上下文中被視為 False:
None
False
數值 0(如 0, 0.0, 0j)
空序列(如 '', (), [])
空集合(如 {}, set())
空 range 對象(如 range(0))
除上述情況外,其他所有對象在布爾上下文中都被視為 True。
以下對象在布爾上下文中被視為 False:
None
False
數值 0(如 0, 0.0, 0j)
空序列(如 '', (), [])
空集合(如 {}, set())
空 range 對象(如 range(0))
除上述情況外,其他所有對象在布爾上下文中都被視為 True。
2024-06-18
list = [1,2,3,4,5,6,7,8,9,10]
sum = 1
for item in list:
sum = sum * (item + 1)
print(sum)
sum = 1
for item in list:
sum = sum * (item + 1)
print(sum)
2024-06-18
L = ['Alice', 66, 'Bob', True, 'False', 100]
for item in L[1::2]:
print (item)
for item in L[1::2]:
print (item)
2024-06-12
最新回答 / 李科霆
在Python中,set是一種無序的數據類型,它存儲唯一的元素。當你將一個列表轉換為集合時,集合中的元素是無序的,這意味著你不能依賴于元素在集合中的特定順序。因此,當你打印出一個集合時,元素的順序可能會變化,這取決于Python的具體實現和你使用的Python版本。在你的代碼中,打印出的集合看起來似乎是有序的,但實際上這只是一種巧合。如果你再次運行相同的代碼,或者在不同的Python環境中運行,輸出的順序可能會有所不同。這是因為集合本身并不保證元素的順序
2024-06-11
def greet(name="world"):
if name == "world":
print("Hello, world.")
else:
print(f"Hello, {name}.")
if name == "world":
print("Hello, world.")
else:
print(f"Hello, {name}.")
2024-05-17
# Enter a code
a=5
x=a
d=10
z=d/5
c=28
t=c/7
y=(x*z)/t
print(y,z)
a=5
x=a
d=10
z=d/5
c=28
t=c/7
y=(x*z)/t
print(y,z)
2024-05-17
def greet(L='world'):
sc='Hello,{}'.format(L)
return sc
print(greet())
print(greet('Lisi')
sc='Hello,{}'.format(L)
return sc
print(greet())
print(greet('Lisi')
2024-05-17
def square_of_sum(list):
result = 0
for num in list:
result += num * num
return result
print(square_of_sum([1, 2, 3, 4, 5]))
result = 0
for num in list:
result += num * num
return result
print(square_of_sum([1, 2, 3, 4, 5]))
2024-05-17
# Enter a code
L = [[1, 2, 3], [5, 3, 2], [7, 3, 2]]
for i in range(len(L)):
a = L[i][0]
b = L[i][1]
c = L[i][2]
print(2 * (a*b + b*c + a*c))
L = [[1, 2, 3], [5, 3, 2], [7, 3, 2]]
for i in range(len(L)):
a = L[i][0]
b = L[i][1]
c = L[i][2]
print(2 * (a*b + b*c + a*c))
2024-05-16
# Enter a code
names = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']
new1 = 'Zero'
new2 = 'Phoebe'
new3 = 'Gen'
names.append(new2)
names.append(new1)
names.insert(5, new3)
print(names)
names = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']
new1 = 'Zero'
new2 = 'Phoebe'
new3 = 'Gen'
names.append(new2)
names.append(new1)
names.insert(5, new3)
print(names)
2024-05-16
# Enter a code
s1='ABC'
s2='123'
s3='xyz'
for i in s1:
for j in s2:
for k in s3:
print(i + j + k)
s1='ABC'
s2='123'
s3='xyz'
for i in s1:
for j in s2:
for k in s3:
print(i + j + k)
2024-05-16
# Enter a code
i = 0
sum = 0
while i <= 1000:
if i%2 != 0:
i = i+1
continue
sum += i
i = i+1
print (sum)
i = 0
sum = 0
while i <= 1000:
if i%2 != 0:
i = i+1
continue
sum += i
i = i+1
print (sum)
2024-05-16
# Enter a code
i = 0
sum = 0
while True:
if i > 1000:
print (sum)
break
if i%2 == 0:
sum += i
i = i+1
i = 0
sum = 0
while True:
if i > 1000:
print (sum)
break
if i%2 == 0:
sum += i
i = i+1
2024-05-16