有人可以向我解释一下吗

发布于 2024-09-27 16:37:25 字数 54 浏览 4 评论 0原文

..Python 中 = 和 == 符号的区别?即提供每个使用时的示例,以便两者之间不会混淆?

.. the difference between the = and the == signs in Python? i.e provide examples when each is used so there's no confusion between the two?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荆棘i 2024-10-04 16:37:25

= 用于分配变量,即 number = 30 - “number”变量现在保存数字 30。

== 用作布尔运算符来检查变量是否彼此相等,即 1 == 1 将返回 true1 == 2 将返回 false

= is used to assign variables ie number = 30 - the "number" variable now holds the number 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

乖乖兔^ω^ 2024-10-04 16:37:25

= 是赋值,== 是相等。

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, == 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
有木有妳兜一样 2024-10-04 16:37:25

= 是赋值,你可以用它来给变量赋值。

str = "hello" 将“hello”分配给 str,因此如果您要获取 str 的值,它将是一个 <代码>你好。

== 是相等比较,您用它来比较两个值。

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

在该代码中,您想查看 str 的值是否等于字符串“hello”,如果我们按上面的方式分配它,这将导致打印“equal”。

= 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.

== 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.

滥情稳全场 2024-10-04 16:37:25

“==”检查是否相等。 “=”用于赋值。例如 v="100" 然后检查 v 是否为 100,v==100

"==" is checking for equality. "=" is for assignment of values. eg v="100" Then to check whether v is 100, v==100

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文