python - whats the difference between = and ==? [duplicate]
2024/11/8 0:28:19
I wonder know what's the difference between a = 1 and a == a?
i got two examples as following:
a = 2
def test():print ("a=", a)a == 3test()
and the result :
a = 2
the other example:
a = 2
def test():print ("a=", a)a = 3test()
and it turned out:
UnboundLocalError: local variable 'a' referenced before assignment
Is there anyone could explain the difference between "=" and "=="?
Answer
The difference between the two are:
== is the operator to check if two objects are equivalent.
= is the operator to assign value(s) to a variable.
Example:
>>> a = 5 # `=` operator
>>> a
5
>>> a == 5 # `==` operator
True
>>>
Also the reason why your code returned an error, is because you already have a variable called a outside the function, so there you want to assign it again, it won't work.
Thanks to @SpencerWieczorek for a much better explanation of the part of explanation below the code:
Note: The second example the local a and the global a are entirely different variables. In order to use the global a you defined you would want to add global a at the start of the function. The local variable doesn't have anything to do with the global one and is not the reason for the error.
I know I asked this before, but Im still not sure why I just get an empty list when I test thisdef sorted_images(image_dict):
(dict) -> list of strGiven an image dictionary return a list of the file…
This question already has answers here:How to downcase the first character of a string?(9 answers)Closed 6 years ago.how do I convert the first letter of each of the below from upper case to lowere ca…
Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…
This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 7 months ago.My code is the following:def value(one,two):
if one < two:
retu…
Why are the values of print len() different for both functions? Are they not the same?The file this script is opening was a text file with three lines of text. i named it test.txt and inside it was J…
Im using an input function where I want to convert any spaces in the input to +s. So for example, if the user inputs iphone 7 black, I want to convert this to iphone+7+black.
Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…
Im looking for a piece of code that will print the average for each users score from a csv.It needs to read all scores and then work out an average across the row for each users.It also needs to calcul…
Write a function named calculate_expenses that receives a filename as argument. The file contains the information about a persons expenses on items. Your function should return a list of tuples sorted …