1、for遍歷查找
numbers = [2, 4, 6, 8, 1]
for number in numbers:
if number % 2 == 1:
print(number)
break
else:
print(f"{number} 是奇數!")
2、定義多個變數值
list = [1, 2, 3, 4, 5]
one, two, three, four, five = list
3、獲取最大或最小元素
import heapq
scores = [50, 24, 58, 87, 90, 75, 14, 49, 35, 82]
print(heapq.nlargest(3, scores)) #獲取三個最大值
print(heapq.nsmallest(5, scores)) # 獲取五個最小值
4、傳遞函式
#可利用*號提取所以元素
list = [1, 2, 3, 4]
print(list) # [1, 2, 3, 4]
print(*list) # 1 2 3 4
def sum_of_elements(*arg):
total = 0
for i in arg:
total += i
return total
result = sum_of_elements(*[1, 2, 3, 4])
print(result) # 10
5、取串列中間值
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/386591.html
標籤:python
上一篇:圣誕節快到啦,我可以有python圣誕樹詞云嗎?(中英文版及代碼)
下一篇:matplotlib可視化基本散點圖、在影像指定區域繪制方框并進行自定義色彩填充(Draw Rectangle filled with color)
