CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day07/findmax.py
Views: 729
1
"""
2
找出列表中最大或最小的元素
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-06
7
"""
8
9
10
def main():
11
fruits = ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya']
12
# 直接使用内置的max和min函数找出列表中最大和最小元素
13
# print(max(fruits))
14
# print(min(fruits))
15
max_value = min_value = fruits[0]
16
for index in range(1, len(fruits)):
17
if fruits[index] > max_value:
18
max_value = fruits[index]
19
elif fruits[index] < min_value:
20
min_value = fruits[index]
21
print('Max:', max_value)
22
print('Min:', min_value)
23
24
25
if __name__ == '__main__':
26
main()
27
# 想一想如果最大的元素有两个要找出第二大的又该怎么做
28
29