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

写一个 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(最大<NumList[2])=(80<120)–条件为真
最大= NumList[2]
最大= 120
位置= 2

第三次迭代:对于范围(1,5)中的 3–条件为真
If(最大<NumList【3】)=(120<87)–条件为假
最大= 120
位置= 2

第四次迭代:对于范围(1,5)中的 4–条件为真
如果(最大<NumList【4】)=(120<46)–条件为假
最大= 120
位置= 2

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

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/245218.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 13:07
下一篇 2024-12-12 13:07

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python循环符合要求数字求和

    这篇文章将详细介绍如何通过Python循环符合要求数字求和。如果你想用Python求和但又不想手动输入数字,那么本文将是一个不错的选择。 一、使用while循环实现求和 sum =…

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • Python购物车程序

    Python购物车程序是一款基于Python编程语言开发的程序,可以实现购物车的相关功能,包括商品的添加、购买、删除、统计等。 一、添加商品 添加商品是购物车程序的基础功能之一,用…

    编程 2025-04-29
  • 爬虫是一种程序

    爬虫是一种程序,用于自动获取互联网上的信息。本文将从如下多个方面对爬虫的意义、运行方式、应用场景和技术要点等进行详细的阐述。 一、爬虫的意义 1、获取信息:爬虫可以自动获取互联网上…

    编程 2025-04-29
  • Python基本数字类型

    本文将介绍Python中基本数字类型,包括整型、布尔型、浮点型、复数型,并提供相应的代码示例以便读者更好的理解。 一、整型 整型即整数类型,Python中的整型没有大小限制,所以可…

    编程 2025-04-29
  • Vb运行程序的三种方法

    VB是一种非常实用的编程工具,它可以被用于开发各种不同的应用程序,从简单的计算器到更复杂的商业软件。在VB中,有许多不同的方法可以运行程序,包括编译器、发布程序以及命令行。在本文中…

    编程 2025-04-29
  • Python一元二次方程求解程序

    本文将详细阐述Python一元二次方程求解程序的相关知识,为读者提供全面的程序设计思路和操作方法。 一、方程求解 首先,我们需要了解一元二次方程的求解方法。一元二次方程可以写作: …

    编程 2025-04-29

发表回复

登录后才能评论