[英]Getting same result if I use > or != in python
I am learning python.我正在学习 python。 I am creating a program to printing Stars '*' in D Shape using for loop.我正在创建一个程序来使用 for 循环在 D 形中打印 Stars '*'。 I am using the following code.我正在使用以下代码。
for row in range(7):
for col in range(5):
if (col==0 or (col==4 and row!=0 and row!=6)) or ((row==0 or row==6) and (col!=0 and col!=4)):
print("*", end="")
else:
print(end=" ")
print()
In if statements first section, if I use row>0 and row<6, I get the same result.在if语句的第一部分,如果我使用 row>0 和 row<6,我会得到相同的结果。 Now I am confused which operator I should use.现在我很困惑我应该使用哪个运算符。
col==4 and row!=0 and row!=6 col==4 和 row!=0 和 row!=6
It doesn't matter.没关系。
Since row
iterates on range(7)
, it can only assume the values of 0, 1, 2, 3, 4, 5 and 6.由于row
range(7)
上迭代,它只能假设值 0、1、2、3、4、5 和 6。
The condition row != 0
applies to 1, 2, 3, 4, 5 and 6.条件row != 0
适用于 1、2、3、4、5 和 6。
The condition row > 0
applies to 1, 2, 3, 4, 5 and 6.条件row > 0
适用于 1、2、3、4、5 和 6。
The condition row != 6
applies to 0, 1, 2, 3, 4 and 5.条件row != 6
适用于 0、1、2、3、4 和 5。
The condition row < 6
applies to 0, 1, 2, 3, 4 and 5.条件row < 6
适用于 0、1、2、3、4 和 5。
So in this specific case, you can use them interchangeably (however, I consider >
and <
to be more readable).因此,在这种特定情况下,您可以互换使用它们(但是,我认为>
和<
更具可读性)。
Your rows go from 0 to 6 so:你的行 go 从 0 到 6 所以:
The reason why row > 0 and row < 6
and row != 0 and row != 6
in this use case results to the same condition is because your row
will only get the values of 0 1 2 3 4 5 6.在这个用例中row > 0 and row < 6
和row != 0 and row != 6
的原因是相同的,因为你的row
只会得到 0 1 2 3 4 5 6 的值。
row > 0 and row < 6
means 1 2 3 4 5 row > 0 and row < 6
表示 1 2 3 4 5
row != 0 and row != 6
means 1 2 3 4 5 row != 0 and row != 6
表示 1 2 3 4 5
Both are ok, but in either case, I'd chain:两者都可以,但无论哪种情况,我都会链接:
0 != row != 6
0 < row < 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.