Python程序:寻找列表中最大数字

创新互联Python教程:

创新互联公司是专业的桂林网站建设公司,桂林接单;提供做网站、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行桂林网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

写一个 Python 程序,用一个实际例子找出列表中最大的数字。

Python 程序查找列表中最大的数字示例 1

Python max 函数返回列表中的最大值。

# Python Program to find Largest Number in a List 

a = [10, 50, 60, 120, 20, 15]

print("The Largest Element in this List is : ", max(a))

Python 最大列表数输出

The Largest Element in this List is : 120

Python 程序查找列表中最大的数字示例 2

这个 python 程序对于最大列表数量的要求同上。但是这次,我们允许用户输入列表的长度。接下来,我们使用 For Loop 给 Python 列表添加数字。

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

print("The Largest Element in this List is : ", max(NumList))

Python 程序查找列表中最大的数字示例 3

Python 排序函数按照升序对列表元素进行排序。接下来,我们使用索引位置打印列表中的最后一个元素。

a = [10, 50, 60, 80, 20, 15]

a.sort()
print("The Largest Element in this List is : ", a[5])

Python 最大列表数输出

The Largest Element in this List is :  80
>>> 

Python 程序查找列表中最大的数字示例 4

这个 Python 最大列表数程序同上。但这一次,我们允许用户输入自己的列表项。

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

NumList.sort()

print("The Largest Element in this List is : ", NumList[Number - 1])

Python 最大列表数输出

Please enter the Total Number of List Elements: 3
Please enter the Value of 1 Element : 90
Please enter the Value of 2 Element : 56
Please enter the Value of 3 Element : 70
The Largest Element in this List is :  90

在列表中查找最大数字的程序示例 5

该程序按照升序对列表项进行排序。接下来,我们使用反转功能来反转列表项。最后,我们使用索引位置 0 来打印列表中的第一个元素。

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

NumList.sort()
NumList.reverse()

print("The Largest Element in this List is : ", NumList[0])

Python 最大列表数输出

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 60
Please enter the Value of 2 Element : 30
Please enter the Value of 3 Element : 90
Please enter the Value of 4 Element : 89
Please enter the Value of 5 Element : 45
The Largest Element in this List is :  90

返回列表中最大数字的 Python 程序示例 6

在这个程序中,我们没有使用任何内置功能,如排序、反转或最大功能

NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

largest = NumList[0]    
for j in range(1, Number):
    if(largest < NumList[j]):
        largest = NumList[j]
        position = j

print("The Largest Element in this List is : ", largest)
print("The Index position of the Largest Element is : ", position)

Python 最大列表数输出

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 70
Please enter the Value of 2 Element : 80
Please enter the Value of 3 Element : 120
Please enter the Value of 4 Element : 87
Please enter the Value of 5 Element : 46
The Largest Element in this List is :  120
The Index position of the Largest Element is :  2

用户插入的值为 NumList[5] = {70,80,120,87,46} 最大= NumList[0] = 70

第一次迭代–对于范围(1,5)中的 1–条件为真 因此,它开始在循环内执行 If 语句,直到条件失败。

if(最大< NumList[j]) inside the for loop is True because (70 < 80) 最大= NumList[1] 最大= 80 位置= 1

第二次迭代:对于范围(1,5)中的 2–条件为真 If(最大

第三次迭代:对于范围(1,5)中的 3–条件为真 If(最大

第四次迭代:对于范围(1,5)中的 4–条件为真 如果(最大

第五次迭代:对于范围(1,5)中的 5–条件为假 因此,它退出循环。

本文名称:Python程序:寻找列表中最大数字
本文地址:http://www.hantingmc.com/qtweb/news5/509055.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联