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)
结果
要查看结果,请在命令行窗口中运行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)
结果
打开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)
结果
要查看结果,请在命令行窗口中运行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)
结果
打开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中,使用filter和lambda选择正数。