+= and -=


Aloha, i am new in coding and need if someone can explain this to me…

this is a code from ‘‘Using strings in lists in functions’’ :

n = [“Michael”, “Lieberman”]

Add your function here

def join_strings(words):
result = ""
for word in words:
result += word
return result

print join_strings(n)

Now, my question is what is this ’ += ’ doing to the code? i see this code before but have no idea how and when to use it :confused: and this one ’ -= '
pls if someone can help me.


they are shorthands, both this codes are the same:

x -= 5
x = x - 5

it saves us typing, to decrease or increase a variable.

many languages (python doesn’t) have ++ and -- to increase or decrease by one (given you need that a lot, a while or for loop for example.


thank you very much. :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.