如何在 Python 中查找数字是否属于特定范围? [英] How to find whether a number belongs to a particular range in Python?

查看:15
本文介绍了如何在 Python 中查找数字是否属于特定范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想检查 x 是否属于 0 到 0.5 的范围.我该怎么做?

Suppose I want to check if x belongs to range 0 to 0.5. How can I do it?

推荐答案

不,你不能那样做.range() 需要整数参数.如果您想知道 x 是否在此范围内,请尝试以下某种形式:

No, you can't do that. range() expects integer arguments. If you want to know if x is inside this range try some form of this:

print 0.0 <= x <= 0.5

小心你的上限.如果您使用 range() 它被排除在外(range(0, 5) 不包括 5!)

Be careful with your upper limit. If you use range() it is excluded (range(0, 5) does not include 5!)

这篇关于如何在 Python 中查找数字是否属于特定范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆