题目
1-20
之间能够被3整除输出apple
,能够被5
整除输出orange
,同时被3
和5
整除输出appleorange
,否则输出元素本身。
普通写法
# 普通写法
for i in range(1, 21):
if i % 3 == 0 and i % 5 != 0:
print("orange")
elif i % 5 == 0 and i % 3 != 0:
print("apple")
elif i % 3 == 0 and i % 5 == 0:
print("appleorange")
else:
print(i)
两行代码解决
for i in range(1, 21):
print("apple"[i % 3 * 5::] + "arange"[i % 5 * 6::] or i)
结果
1
2
apple
4
arange
apple
7
8
apple
arange
11
apple
13
14
applearange
16
17
apple
19
arange
Process finished with exit code 0