Using strings in lists in functions


Hey there. I am getting really frustrated at this... it seems like urary is a math problem thats supposed to be done on the "str" but thats all I understand.


join_strings(['x', 'y', 'z', 'a']) resulted in an error: bad operand type for unary +: 'str'

n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
    result = ""
    for i in words:
        words[i] =+ result
    return result

apparently I fixed it using this. Can someone explain why the order of += matters. Or is =+/+= different operations all together?

n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
    result = ""
    for i in range(len(words)):
        result += words[i]
    return result


I would just do the x = x + 1. the +=, *=, /= are just quick ways to get around orders of operations. IMO


so if there is not a long math problem, where order of operations is not being used, += is kinda silly?


+= and =+ is not the same operation. In fact there isn't a =+ operation. Instead it gets interpreted as

words[i] = (+result)

if that formatting makes more sense. 'words[i]' is assigned the string 'result' modified by the unary operator '+'. I'll freely admit I have no idea what you use a unary + for in python, but as the error message say it is not allowed on strings at least.


I just use it to get comfortable with syntax. When i get better at this language i'll start using other quicker methods. I just feel imo to try and remember these rules:

x = x + i <---> x += i (addition)
x = x - i <----> x -= i (subtraction)
x = x / i <----> x /= i (division)
x = x * i <----> x*= i (multiplication)
x = x**i <---> x**= i (exponential)
x = x % i <---> x %=i (modular arithmetic)



you explained this very very well. Thank you!


so its always the math thing you wanna do (ex. **,+,%%) then the = sign. Thanks


No problem! Enjoy doing the problems, they are pretty fun so far.


except when stuff does not work like I want it too and I get frustrated haha