Python程序打印指定范围内的所有正数


有时任务是从给定范围内选择所有正数。在本篇 Python 文章中,首先将范围作为输入,然后选择此范围内的负整数和正整数。在本篇 Python 文章中,然后使用四种不同示例中的不同方法从这些数字中仅选择正数。在示例 1 中,选择正数并将它们分离到另一个列表中。在示例 2 中,删除所有非正元素。在示例 3 中,将排序列表拆分为零,并仅保留正数。在示例 4 中,使用过滤器选择正数。

示例 1 - 选择指定范围内的所有正数并将这些正数分离到另一个列表中进行打印

算法

步骤 1 − 指定范围内的最小和最大数字作为输入。最小数字应为负数,最大数字应为正数。创建给定范围内的整数列表

步骤 2 − 首先将所有正数分离到一个单独的列表中。打印这些数字。

步骤 3 − 运行程序,然后检查结果。

Python 文件包含以下内容

lowNum=-50
highNum=60
mainlist=[]
listPos=[]

#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
   mainlist.append(item)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)

#dividing the main list into negatives and positives 
for item in mainlist:
   if (item > 0): 
      listPos.append(item)
            
print("\nThe Negative Elements in the Range :")
print(listPos)

查看结果 - 示例 1

要查看结果,请在 cmd 窗口中运行 Python 文件。

In the given range from  -50  to 60  :

The Main List :
[-50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]

The Positive Elements in the Range :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]

示例 2:删除所有非正元素并打印剩余的正数

算法

步骤 1 − 首先输入范围内的最小和最大数字。最小数字应为负数,最大数字应为正数。创建给定范围内的所有整数列表

步骤 2 − 首先将其复制为排序列表,然后从此列表中删除所有小于或等于零的数字。

步骤 3 − 打印主列表中剩余的所有正数。

步骤 4 − 运行程序,然后检查结果。

Python 文件包含以下内容

lowNum=-40
highNum=70
mainlist=[]
mainlistcopy=[]

#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
   mainlist.append(item)


print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)

mainlistcopy=sorted(mainlist)

for item in mainlistcopy:
   if (item <= 0): 
      mainlist.remove(item)
            
print("\nThe Positive Elements in the List :")
print(mainlist)

查看结果 - 示例 2

打开 cmd 窗口并运行 python 文件以查看结果。

Enter a negative number for the start of a Range (example -100):  -40
Enter a positive number for the end of a Range (example 100): 70
In the given range from  -40  to 70  :

The Main List :
[-40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70]

The Positive Elements in the List :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70]

示例 3 - 将排序列表拆分为零并保留正数以进行打印

算法

步骤 1− 指定范围内的最小和最大数字作为输入。最小数字应为负数,最大数字应为正数。创建输入范围内的所有整数列表

步骤 2− 首先复制此列表,然后将此列表拆分为零。

步骤 3− 打印列表副本中剩余的所有正数。

步骤 4− 运行程序,然后检查结果。

Python 文件包含以下内容

lowNum=-20
highNum=40
mainlist=[]
listPos=[]

#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
   mainlist.append(item)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)

listPos=mainlist

listPos[:] = [item for item in listPos if item > 0]
          
print("\nThe Positive Elements in the List :")
print(listPos)

查看结果 - 示例 3

要查看结果,请在 cmd 窗口中运行 Python 文件。

Enter a negative number for the start of a Range (example -100):  -20
Enter a positive number for the end of a Range (example 100): 40
In the given range from  -20  to 40  :

The Main List :
[-20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

The Positive Elements in the List :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

图 3:显示命令窗口中的结果。

示例 4:使用过滤器选择正数并过滤掉非正数

算法

步骤 1 − 提供范围内的最小和最大数字的输入。最小数字应为负数,最大数字应为正数。创建此输入范围内的所有整数列表

步骤 2 − 使用带有 lambda 函数的过滤器。

步骤 3 − 在新列表中过滤出正数。打印新列表中所有已过滤的正数。

步骤 4 − 运行程序,然后检查结果。

Python 文件包含以下内容

lowNum=-50
highNum=50
mainlist=[]
listPos=[]

#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
   mainlist.append(item)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)

listPos = list(filter(lambda item: item > 0, mainlist))
          
print("\nThe Positive Elements in the Range :")
print(listPos)

查看结果 - 示例 4

打开 cmd 窗口并运行 python 文件以查看结果。

Enter a negative number for the start of a Range (example -100):  -50
Enter a positive number for the end of a Range (example 100): 50
In the given range from  -50  to 50  :

The Main List :
[-50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

The Positive Elements in the Range :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]]

在本篇 Python 文章中,通过四个不同的示例,介绍了如何从给定范围内查找正数并打印这些正数的方法。首先,在示例 1 中,将正数分离到另一个列表中。在示例 2 中,删除所有负元素。在示例 3 中,将列表拆分并仅保留正数。在示例 4 中,使用过滤器和 lambda 选择正数。

更新于: 2023-07-28

475 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告