符号“="是什么意思和“=="在python中是什么意思? [英] What do the symbol "=" and "==" mean in python?

查看:200
本文介绍了符号“="是什么意思和“=="在python中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候应该使用符号==",什么时候只使用符号="就足够了?python中的符号'=='是什么意思?

When should i use the symbol '==' and when only the symbol '=' is enough? What does the symbol '==' mean in python?

我两周前开始使用 python 编码,这两个符号有时让我感到困惑.多次使用="时,我会收到一条错误消息.在我将其更改为=="后,不再有错误消息.当 '=' 工作时,'==' 似乎可以在任何情况下使用.这是真的吗?谁能解释一下这两个符号的使用原理?

I started python coding 2 weeks ago and these two symbols confused me sometimes. Several times when i used '=', i would get an error message. After i changed it to '==', no error message any more. It seems that '==' can be used in any situation when '=' works. Is it true? Can any one of you explain the principle of using these two symbols?

非常感谢您的帮助!

推荐答案

== 是一个比较运算符,而 = 会为该变量赋值.

== is a comparison operator while = will assign a value to said variable.

你可以用==来判断任意两个元素是否相同,只要它们是相同类型的:

You can use == to see whether any two items as long they are the same type are equivalent:

if a == 2: # Compares whether a is equal to 2
    print a

现在事情来了.如果您正在比较任何两个这样的项目,则会弹出一个错误:

Now here's the thing. If you are comparing any two items like these, an error will pop up:

  • 带整数的字符串

  • String with integer

带字符串的整数

字符串和浮点

浮点数和字符串

浮点数和整数是可比较的,因为它们是数字,但通常彼此不相等,除非浮点数基本上是整数,但在末尾添加了 .0.使用==时,如果两项相同,则返回True.否则,它将返回False.

Floats and integers are comparable as they are numbers but are usually not equal to each other except when the float is basically the integer but with .0 added to the end. When using ==, if the two items are the same, it will return True. Otherwise, it will return False.

您可以使用 = 为变量赋值.使用 == 要么什么都不做,要么抛出错误(如果变量未定义).例如,您希望变量 hi 的值为 2.然后使用 =:

You can use = to assign values to variables. Using == will either do nothing or throw an error (if the variable is undefined). For example, you wanted the variable hi to have the value of 2. Then use the =:

hi = 2

现在 hi 等于 2.您可以将 =+- 等操作结合起来,假设变量是整数或浮点数:

Now hi is equal to 2. You can combine = with operations like + and - assuming the variable is an integer or float:

hi += 1
hi -= 1

现在通过使用 +=-= 像上面一样,必须已经定义了变量,因为这些运算符将直接改变变量的值.基本上,他们是这样的:

Now by using += or -= like above, the variable must already be defined as these operators will directly change the value of the variable. Basically, they are like this:

hi += 1 # is the same as hi = hi + 1
hi -= 1 # is the same as hi = hi - 1

总之,它们的不同之处在于:

So in conclusion, they are different as:

  • == 是一个比较运算符:返回 True 是两项相等,如果不相等则返回 False,否则抛出错误如果用于在定义前分配变量并且如果两个项不兼容

  • == is a comparison operator: returns True is the two items are equal, returns False if not, throws error if used to assign variable before definition and if the two items are not compatible

= 是一个赋值运算符:将字符串或数字之类的值分配给变量.当变量的值是一个数字并且已经定义时,可以以类似 += 的形式使用.

= is an assignment operator: will assign values like strings or numbers to variables. Can be used in forms like += when variable's value is a number and is already defined.

它们可以同时使用的唯一方法是它们可以在字符串中使用:

The only way they can be used the same time is that they can be used in strings:

"hi = hello"
"2 == 3 probably returns False don't you think?" 

这篇关于符号“="是什么意思和“=="在python中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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