Using strings in lists in functions


#1



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.

Thanks

https://www.codecademy.com/courses/python-beginner-nzzVa/3/5?curriculum_id=4f89dab3d788890003000096#

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

#2

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


#3

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


#4

+= 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.


#5

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)

HAVE FUN!


#6

you explained this very very well. Thank you!


#7

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


#8

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


#9

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