I hope this helps you understand this concept. Likewise if you try to compare two strings (or other data types) with the = operator like this: if "something" = "nothing": If it is already assigned it will work but it will not do what you expect. However if I try to assign a variable with the = operator for the initial assignment of the variable it will not work. If I want to compare two strings (or other data types) like this: if "something" = "nothing": I am assigning the variable to an object using that operator. Two variables that are equal do not imply that they are identical. In Python, is and is not are the identity operators both are used to check if two values are located on the same part of the memory. For example if I wanted to create the variable my_string and set it equal to "something" I would use the = operator. These are standard symbols used for the purpose of logical and arithmetic operations. And you are wrong in saying that = can be used in any situation when = works. The simple answer is = is an assignment operator, = is a comparison operator. "2 = 3 probably returns False don't you think?" The only way they can be used the same time is that they can be used in strings: "hi = hello" Can be used in forms like += when variable's value is a number and is already defined. = is an assignment operator: will assign values like strings or numbers to variables. = 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 Basically, they are like this: hi += 1 # is the same as hi = 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. You can combine = with operations like + and - assuming the variable is an integer or float: hi += 1 If the values compared are not equal, then a value of false is returned. If the values compared are equal, then a value of true is returned. It returns either true or false depending on the result of the operation. For example, you wanted the variable hi to have the value of 2. Not Equal Operator in Python The not equal operator is a relational or comparison operator that compares two or more values (operands). Using = will either do nothing or throw an error (if the variable is undefined). You can use = to assign values to variables. When using =, if the two items are the same, it will return True. If you are comparing any two items like these, an error will pop up:įloats 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. 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 = is a comparison operator while = will assign a value to said variable.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |