简体   繁体   English

有人可以给我解释一下吗

[英]Can someone please explain to me

.. the difference between the = and the == signs in Python? .. Python中=和==符号之间的区别? ie provide examples when each is used so there's no confusion between the two? 即提供使用示例时的示例,以便使两者之间没有混淆?

= is used to assign variables ie number = 30 - the "number" variable now holds the number 30. =用于分配变量,即number = 30 “数字”变量现在保存数字30。

== is used as a boolean operator to check whether variables are equal to each other ie 1 == 1 would give true and 1 == 2 would return false ==用作布尔运算符,以检查变量是否彼此相等,即1 == 1将为true1 == 2将返回false

= is assignment, == is equality. =是分配, ==是相等。

a = 5  # assigns the variable a to 5
a == 5 # returns true
a == 4 # returns false
a = 4 # a is now 4
a == 4 # returns true

= is assignment, you would use it to give a value to a variable. =是赋值,您将使用它为变量赋值。

str = "hello" assigns "hello" to str , so that if you were to get the value of str , it would be a hello . str = "hello"str = "hello"分配给str ,这样,如果要获取str的值,那将是一个hello

== is equality comparison, you use it to compare two values. ==是相等比较,您可以使用它比较两个值。

if str == "hello": 
   print "equal"
else:
   print "not equal"

In that code you want to see if the value of str is equal to the string "hello", and if we assigned it as above, this would result in "equal" being printed. 在该代码中,您想查看str的值是否等于字符串“ hello”,如果我们按上面的方法进行分配,这将导致打印“ equal”。

"==" is checking for equality. “ ==”正在检查是否相等。 "=" is for assignment of values. “ =”用于分配值。 eg v="100" Then to check whether v is 100, v==100 例如v="100"然后检查v是否为100, v==100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM