Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why don't Python's assignment operators work if they're written with the equals sign on the left?

I’ve tried this, and I’d like to know more about why this doesn’t work. count += 1 works as intended, but count =+ 1 does not.

list = ["one", "two", "three"]
count = 0
for x in list:
    count =+ 1
    print(x)
    print(count)

The above example doesn’t work properly. However, the one below does.

list = ["one", "two", "three"]
count = 0
for x in list:
    count += 1
    print(x)
    print(count)

The intended output is to have it print each iteration all the way up to the number 3.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because that’s not proper syntax. The operator is += ("plus equals"), not =+ ("equals plus").

count =+ 1 is the same thing as count = (+1), where + is a unary operator on 1.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading