Python 程序:在列表中查找最大和最小数字

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

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

这个 python 程序允许用户输入列表的长度。接下来,我们使用 For 循环向列表中添加数字。这里, Python 中的 min 和 max 函数返回一个列表中的最小和最大数字或最小和最大值。

# Python Program to find Largest and Smallest Number in a List 

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 Smallest Element in this List is : ", min(NumList))
print("The Largest Element in this List is : ", max(NumList))

Python 最大和最小列表号输出

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 50
Please enter the Value of 2 Element : 45
Please enter the Value of 3 Element : 33
Please enter the Value of 4 Element : 78
Please enter the Value of 5 Element : 66
The Smallest Element in this List is :  33
The Largest Element in this List is :  78

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

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

# Python Program to find Largest and Smallest Number in a List 

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 Smallest Element in this List is : ", NumList[0])
print("The Largest Element in this List is : ", NumList[Number - 1])

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

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

# Python Program to find Largest and Smallest Number in a List 

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)

smallest = largest = NumList[0]

for j in range(1, Number):
    if(smallest > NumList[j]):
        smallest = NumList[j]
        min_position = j
    if(largest < NumList[j]):
        largest = NumList[j]
        max_position = j

print("The Smallest Element in this List is : ", smallest)
print("The Index position of Smallest Element in this List is : ", min_position)
print("The Largest Element in this List is : ", largest)
print("The Index position of Largest Element in this List is : ", max_position)

Python 最大和最小列表号输出

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 40
Please enter the Value of 2 Element : 60
Please enter the Value of 3 Element : 20
Please enter the Value of 4 Element : 11
Please enter the Value of 5 Element : 50
The Smallest Element in this List is :  11
The Index position of Smallest Element in this List is :  3
The Largest Element in this List is :  60
The Index position of Largest Element in this List is :  1

从上面的 Python 程序中找到列表输出中最大和最小的数字,用户插入的值是
NumList[5] = {40,60,20,11,50}
最小=最大= NumList[0] = 40

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

如果循环的内的(最小>数值列表【j】为假,因为(40 > 60)
最小= 40
位置= 1

If(最大< NumList[j]) inside the for loop is True because (40 < 60)
最大= 60
位置= 1

第二次迭代:对于范围(1,5)中的 2–条件为真
If(40>20)–条件为真
最小= 20
位置= 2

如果(60 < 20) – Condition False
最大= 60 == >不变
位置= 1 == >不变

第三次迭代:对于范围(1,5)中的 3–条件为真
如果(20>11)–条件为真
最小= 11
位置= 3

若(60 < 11) – Condition False
最大= 60
位置= 1

第四次迭代:对于范围(1,5)中的 4–条件为真
如果(11>50)–条件为假
最小= 11
位置= 3

如果(60 < 11) – Condition False
最大= 60
位置= 1

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

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝的头像小蓝
上一篇 2024-12-09 11:01
下一篇 2024-12-09 11:01

相关推荐

  • 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
  • Vb运行程序的三种方法

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

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

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

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

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

    编程 2025-04-29
  • 如何使用GPU加速运行Python程序——以CSDN为中心

    GPU的强大性能是众所周知的。而随着深度学习和机器学习的发展,越来越多的Python开发者将GPU应用于深度学习模型的训练过程中,提高了模型训练效率。在本文中,我们将介绍如何使用G…

    编程 2025-04-29

发表回复

登录后才能评论