运算符“==”和“=”之间的差异

时间:2015-10-07 20:16:55

标签: python

运算符“==”和“=”之间的差异。何时使用?为什么每个都会被使用?

3 个答案:

答案 0 :(得分:1)

In python and other languages like C, 
     "=" is a assignment operator and is used to assign a value to a variable.
       Example: a=2 # the value of a is 2

whereas "==" is Comparison operator and is used to check whether 2 expressions give the same value .Equality check returns true if it succeeds and else return false.
  Example: a=2 b=3 c=2 
             a==b (#false because 2 is not equal to 3) 
               a==c (#true because 2 is equal to 2)

答案 1 :(得分:0)

==表达式求值为true,是一个相等运算符。 ==具有两个操作数相等的值使条件或语句为真。 =是赋值运算符到变量,数组,对象的符号的表达式。 两个运算符都非常重要,它们在每个等效对象中以不同的方式工作。他们的操作行为基于对象的身份。是他们变量的通货膨胀。 当使用==时,比较两个对象的值,这两个对象示例包含来自同一公司的两辆汽车,并且具有相同的身份和特征以及相同的外观。该规则暗示声明和条件是真实的 要使用=运算符是在何时评估表达式中的变量,如果双方或运算符的含义相同或对象相同(如果不相同),则表达式将为false,如果为true,则表达式或对象相同。

答案 2 :(得分:0)

=用于分配:例如:apple ='apple'。 它说明了什么。 ==将一个值与另一个值进行比较。 5等于5应该这样写:5 == 5

相关问题