如何在 Python 中检查数字是否在范围内

PythonPythonBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在这个实验中,你将学习如何在 Python 中检查一个数字是否在特定范围内。本实验重点在于定义数字范围,并使用比较运算符来判断给定数字是否在定义的区间内。

你将创建一个名为 ranges.py 的 Python 脚本,以探索这些概念。该脚本将展示如何定义一个数字,然后使用比较运算符(例如 <=)来检查该数字是否在指定范围内,比如 10 到 100。通过修改数字并运行脚本,你将观察到输出如何根据数字是在定义范围内还是范围外而变化。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/numeric_types("Numeric Types") python/BasicConceptsGroup -.-> python/booleans("Booleans") python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") subgraph Lab Skills python/numeric_types -.-> lab-559555{{"如何在 Python 中检查数字是否在范围内"}} python/booleans -.-> lab-559555{{"如何在 Python 中检查数字是否在范围内"}} python/conditional_statements -.-> lab-559555{{"如何在 Python 中检查数字是否在范围内"}} python/build_in_functions -.-> lab-559555{{"如何在 Python 中检查数字是否在范围内"}} end

定义数字范围

在这一步中,你将学习如何在 Python 中定义数字范围。当你需要检查一个数字是否在特定区间内时,数字范围非常有用。Python 提供了多种定义和使用数字范围的方法,包括使用比较运算符和 range() 函数。

首先,让我们使用 VS Code 编辑器在 ~/project 目录下创建一个名为 ranges.py 的 Python 脚本。

#!/usr/bin/env python3

## 定义一个变量
number = 50

## 使用比较运算符检查数字是否在范围内
if 10 <= number <= 100:
    print(f"{number} is within the range of 10 to 100")
else:
    print(f"{number} is outside the range of 10 to 100")

在这个脚本中:

  • 我们定义了一个变量 number,并将其赋值为 50。
  • 我们使用比较运算符 (<=) 来检查 number 是否在 10 到 100 的范围内。
  • 如果条件为真,我们打印一条消息,表明该数字在范围内。否则,我们打印一条消息,表明该数字在范围外。

现在,让我们运行这个脚本:

python ~/project/ranges.py

你应该会看到以下输出:

50 is within the range of 10 to 100

让我们修改脚本以检查另一个数字。将 number 变量的值改为 5,然后再次运行脚本。

#!/usr/bin/env python3

## 定义一个变量
number = 5

## 使用比较运算符检查数字是否在范围内
if 10 <= number <= 100:
    print(f"{number} is within the range of 10 to 100")
else:
    print(f"{number} is outside the range of 10 to 100")

运行脚本:

python ~/project/ranges.py

你应该会看到以下输出:

5 is outside the range of 10 to 100

这展示了如何在 Python 中使用比较运算符来定义和检查数字范围。

使用比较运算符

在这一步中,你将加深对 Python 中比较运算符的理解,以及如何使用它们来定义更复杂的数字范围。比较运算符允许你创建用于检查值之间关系的条件。

让我们继续使用 ~/project 目录下的 ranges.py 文件。我们将修改脚本,使其包含更多的比较运算符。

#!/usr/bin/env python3

## 定义一个变量
number = 50

## 检查数字是否大于或等于 20 且小于 80
if number >= 20 and number < 80:
    print(f"{number} is greater than or equal to 20 and less than 80")
else:
    print(f"{number} is not greater than or equal to 20 and less than 80")

## 检查数字是否等于 50 或等于 100
if number == 50 or number == 100:
    print(f"{number} is either 50 or 100")
else:
    print(f"{number} is not 50 or 100")

在这个脚本中:

  • 我们使用 >=(大于或等于)和 <(小于)运算符来检查 number 是否在特定范围内。
  • 我们使用 and 运算符来组合两个条件,只有当两个条件都为真时,整个条件才为真。
  • 我们使用 ==(等于)运算符来检查 number 是否等于特定值。
  • 我们使用 or 运算符来组合两个条件,只要其中一个条件为真,整个条件就为真。

现在,让我们运行脚本:

python ~/project/ranges.py

你应该会看到以下输出:

50 is greater than or equal to 20 and less than 80
50 is either 50 or 100

让我们再次修改脚本,将 number 的值改为 100,并观察输出。

#!/usr/bin/env python3

## 定义一个变量
number = 100

## 检查数字是否大于或等于 20 且小于 80
if number >= 20 and number < 80:
    print(f"{number} is greater than or equal to 20 and less than 80")
else:
    print(f"{number} is not greater than or equal to 20 and less than 80")

## 检查数字是否等于 50 或等于 100
if number == 50 or number == 100:
    print(f"{number} is either 50 or 100")
else:
    print(f"{number} is not 50 or 100")

运行脚本:

python ~/project/ranges.py

你应该会看到以下输出:

100 is not greater than or equal to 20 and less than 80
100 is either 50 or 100

这展示了如何使用比较运算符和逻辑运算符(andor)在 Python 中为数字范围定义更复杂的条件。

使用 range() 检查整数范围

在这一步中,你将学习如何使用 Python 中的 range() 函数生成一个数字序列,并检查一个整数是否在该范围内。当你需要遍历一个数字序列或创建一个特定区间内的整数列表时,range() 函数特别有用。

让我们使用 VS Code 编辑器在 ~/project 目录下创建一个名为 range_check.py 的新 Python 脚本。

#!/usr/bin/env python3

## 定义一个变量
number = 25

## 检查数字是否在 1 到 50(不包含 50)的范围内
if number in range(1, 50):
    print(f"{number} is within the range of 1 to 49")
else:
    print(f"{number} is outside the range of 1 to 49")

## 检查数字是否在 0 到 100 之间,步长为 5
if number in range(0, 101, 5):
    print(f"{number} is within the range of 0 to 100 with a step of 5")
else:
    print(f"{number} is outside the range of 0 to 100 with a step of 5")

在这个脚本中:

  • 我们定义了一个变量 number,并将其赋值为 25。
  • 我们使用 range(1, 50) 函数生成一个从 1 到(但不包括)50 的数字序列。
  • 我们使用 in 运算符来检查 number 是否存在于生成的序列中。
  • 我们使用 range(0, 101, 5) 函数生成一个从 0 到(但不包括)101 的数字序列,步长为 5(即 0、5、10、15、...、100)。

现在,让我们运行脚本:

python ~/project/range_check.py

你应该会看到以下输出:

25 is within the range of 1 to 49
25 is within the range of 0 to 100 with a step of 5

让我们修改脚本,将 number 的值改为 7,并观察输出。

#!/usr/bin/env python3

## 定义一个变量
number = 7

## 检查数字是否在 1 到 50(不包含 50)的范围内
if number in range(1, 50):
    print(f"{number} is within the range of 1 to 49")
else:
    print(f"{number} is outside the range of 1 to 49")

## 检查数字是否在 0 到 100 之间,步长为 5
if number in range(0, 101, 5):
    print(f"{number} is within the range of 0 to 100 with a step of 5")
else:
    print(f"{number} is outside the range of 0 to 100 with a step of 5")

运行脚本:

python ~/project/range_check.py

你应该会看到以下输出:

7 is within the range of 1 to 49
7 is outside the range of 0 to 100 with a step of 5

这展示了如何使用 range() 函数和 in 运算符来检查一个整数是否在 Python 中的特定范围内。

总结

在本次实验中,你学习了如何在 Python 中定义数字范围,并检查一个数字是否在特定区间内。第一步是创建一个名为 ranges.py 的 Python 脚本,并使用比较运算符(<=)来判断给定的数字是否在 10 到 100 的范围内。

本次实验展示了如何修改脚本以测试不同的数字,并观察相应的输出,从而确认每个数字是在定义的范围内还是范围外。这让你对在 Python 中使用比较运算符进行范围检查有了实际的理解。