问题:确定整数是否在其他两个整数之间?
如何确定给定的整数是否在其他两个整数之间(例如,大于/等于10000
和小于/等于30000
)?
我正在使用2.3 IDLE,到目前为止,我一直没有尝试:
if number >= 10000 and number >= 30000:
print ("you have to pay 5% taxes")
How do I determine whether a given integer is between two other integers (e.g. greater than/equal to 10000
and less than/equal to 30000
)?
I’m using 2.3 IDLE and what I’ve attempted so far is not working:
if number >= 10000 and number >= 30000:
print ("you have to pay 5% taxes")
回答 0
if 10000 <= number <= 30000:
pass
if 10000 <= number <= 30000:
pass
回答 1
>>> r = range(1, 4)
>>> 1 in r
True
>>> 2 in r
True
>>> 3 in r
True
>>> 4 in r
False
>>> 5 in r
False
>>> 0 in r
False
>>> r = range(1, 4)
>>> 1 in r
True
>>> 2 in r
True
>>> 3 in r
True
>>> 4 in r
False
>>> 5 in r
False
>>> 0 in r
False
回答 2
您的操作员不正确。应该是if number >= 10000 and number <= 30000:
。此外,Python的缩写是if 10000 <= number <= 30000:
。
Your operator is incorrect. Should be if number >= 10000 and number <= 30000:
. Additionally, Python has a shorthand for this sort of thing, if 10000 <= number <= 30000:
.
回答 3
您的代码段,
if number >= 10000 and number >= 30000:
print ("you have to pay 5% taxes")
实际检查数字是否同时大于10000和30000。
假设您要检查数字范围在10000-30000之间,可以使用Python间隔比较:
if 10000 <= number <= 30000:
print ("you have to pay 5% taxes")
Python文档中进一步描述了此Python功能。
Your code snippet,
if number >= 10000 and number >= 30000:
print ("you have to pay 5% taxes")
actually checks if number is larger than both 10000 and 30000.
Assuming you want to check that the number is in the range 10000 – 30000, you could use the Python interval comparison:
if 10000 <= number <= 30000:
print ("you have to pay 5% taxes")
This Python feature is further described in the Python documentation.
回答 4
if number >= 10000 and number <= 30000:
print ("you have to pay 5% taxes")
if number >= 10000 and number <= 30000:
print ("you have to pay 5% taxes")
回答 5
比较的麻烦在于,当您将一个>=
应该放置在<=
# v---------- should be <
if number >= 10000 and number >= 30000:
print ("you have to pay 5% taxes")
Python使您可以用语言写下您的意思
if number in xrange(10000, 30001): # ok you have to remember 30000 + 1 here :)
在Python3中,您需要使用range
代替xrange
。
编辑:人们似乎更关注微基准标记以及如何进行酷链接操作。我的答案是关于防御性(错误的攻击面更少)的编程。
由于评论中有声明,因此我在此处为Python3.5.2添加了微型基准测试
$ python3.5 -m timeit "5 in range(10000, 30000)"
1000000 loops, best of 3: 0.266 usec per loop
$ python3.5 -m timeit "10000 <= 5 < 30000"
10000000 loops, best of 3: 0.0327 usec per loop
如果您担心性能,可以一次计算范围
$ python3.5 -m timeit -s "R=range(10000, 30000)" "5 in R"
10000000 loops, best of 3: 0.0551 usec per loop
The trouble with comparisons is that they can be difficult to debug when you put a >=
where there should be a <=
# v---------- should be <
if number >= 10000 and number >= 30000:
print ("you have to pay 5% taxes")
Python lets you just write what you mean in words
if number in xrange(10000, 30001): # ok you have to remember 30000 + 1 here :)
In Python3, you need to use range
instead of xrange
.
edit: People seem to be more concerned with microbench marks and how cool chaining operations. My answer is about defensive (less attack surface for bugs) programming.
As a result of a claim in the comments, I’ve added the micro benchmark here for Python3.5.2
$ python3.5 -m timeit "5 in range(10000, 30000)"
1000000 loops, best of 3: 0.266 usec per loop
$ python3.5 -m timeit "10000 <= 5 < 30000"
10000000 loops, best of 3: 0.0327 usec per loop
If you are worried about performance, you could compute the range once
$ python3.5 -m timeit -s "R=range(10000, 30000)" "5 in R"
10000000 loops, best of 3: 0.0551 usec per loop
回答 6
定义数字之间的范围:
r = range(1,10)
然后使用它:
if num in r:
print("All right!")
Define the range between the numbers:
r = range(1,10)
Then use it:
if num in r:
print("All right!")
回答 7
有两种比较三个整数并检查b是否在a和c之间的方法:
if a < b < c:
pass
和
if a < b and b < c:
pass
第一个看起来更易读,但是第二个运行得更快。
让我们使用dis.dis进行比较:
>>> dis.dis('a < b and b < c')
1 0 LOAD_NAME 0 (a)
2 LOAD_NAME 1 (b)
4 COMPARE_OP 0 (<)
6 JUMP_IF_FALSE_OR_POP 14
8 LOAD_NAME 1 (b)
10 LOAD_NAME 2 (c)
12 COMPARE_OP 0 (<)
>> 14 RETURN_VALUE
>>> dis.dis('a < b < c')
1 0 LOAD_NAME 0 (a)
2 LOAD_NAME 1 (b)
4 DUP_TOP
6 ROT_THREE
8 COMPARE_OP 0 (<)
10 JUMP_IF_FALSE_OR_POP 18
12 LOAD_NAME 2 (c)
14 COMPARE_OP 0 (<)
16 RETURN_VALUE
>> 18 ROT_TWO
20 POP_TOP
22 RETURN_VALUE
>>>
并使用timeit:
~$ python3 -m timeit "1 < 2 and 2 < 3"
10000000 loops, best of 3: 0.0366 usec per loop
~$ python3 -m timeit "1 < 2 < 3"
10000000 loops, best of 3: 0.0396 usec per loop
此外,您可以按照之前的建议使用range,但是它要慢得多。
There are two ways to compare three integers and check whether b is between a and c:
if a < b < c:
pass
and
if a < b and b < c:
pass
The first one looks like more readable, but the second one runs faster.
Let’s compare using dis.dis:
>>> dis.dis('a < b and b < c')
1 0 LOAD_NAME 0 (a)
2 LOAD_NAME 1 (b)
4 COMPARE_OP 0 (<)
6 JUMP_IF_FALSE_OR_POP 14
8 LOAD_NAME 1 (b)
10 LOAD_NAME 2 (c)
12 COMPARE_OP 0 (<)
>> 14 RETURN_VALUE
>>> dis.dis('a < b < c')
1 0 LOAD_NAME 0 (a)
2 LOAD_NAME 1 (b)
4 DUP_TOP
6 ROT_THREE
8 COMPARE_OP 0 (<)
10 JUMP_IF_FALSE_OR_POP 18
12 LOAD_NAME 2 (c)
14 COMPARE_OP 0 (<)
16 RETURN_VALUE
>> 18 ROT_TWO
20 POP_TOP
22 RETURN_VALUE
>>>
and using timeit:
~$ python3 -m timeit "1 < 2 and 2 < 3"
10000000 loops, best of 3: 0.0366 usec per loop
~$ python3 -m timeit "1 < 2 < 3"
10000000 loops, best of 3: 0.0396 usec per loop
also, you may use range, as suggested before, however it is much more slower.
回答 8
假设有3个非负整数:a
,b
,和c
。从数学上讲,如果我们想确定是否c
介于a
和之间b
,可以使用以下公式:
(c-a)*(b-c)> = 0
或在Python中:
> print((c - a) * (b - c) >= 0)
True
Suppose there are 3 non-negative integers: a
, b
, and c
. Mathematically speaking, if we want to determine if c
is between a
and b
, inclusively, one can use this formula:
(c – a) * (b – c) >= 0
or in Python:
> print((c - a) * (b - c) >= 0)
True
回答 9
仅当数字介于10,000和30,000之间时,您才希望输出打印给定语句。
代码应该是;
if number >= 10000 and number <= 30000:
print("you have to pay 5% taxes")
You want the output to print the given statement if and only if the number falls between 10,000 and 30,000.
Code should be;
if number >= 10000 and number <= 30000:
print("you have to pay 5% taxes")
回答 10
条件应该是
if number == 10000 and number <= 30000:
print("5% tax payable")
使用的原因number == 10000
是,如果number的值是50000,并且我们使用number >= 10000
该条件,那么条件将过去,这不是您想要的。
The condition should be,
if number == 10000 and number <= 30000:
print("5% tax payable")
reason for using number == 10000
is that if number’s value is 50000 and if we use number >= 10000
the condition will pass, which is not what you want.