Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Types and Branching Strings and Operators

mohan Abdul
PLUS
mohan Abdul
Courses Plus Student 1,453 Points

reassigning the variable? the teacher reassigned the variable three times. when reassigning the old variable, do you typ

do you type the previous label?

eg. """dessert = ' choc & candy' dessert = dessert + ' ben & jerries' dessert += yum' dessert += ' !' * 20""" so each time i want to change the variable or make addition I type/input the previous label? i am I correct in that? how do i make a further addition to the variable?

3 Answers

Steven Parker
Steven Parker
231,271 Points

A normal assignment ("=") replaces the contents of a variable. It creates a new variable if one doesn't exist.

A concatenating assignment ("+=") adds on to what's already there. This form can only be used on variables that have already been created.

mohan Abdul
mohan Abdul
Courses Plus Student 1,453 Points

Steven Parker , so there is no way to delete a variable? or you can only change it? In the video teacher only made additions, how do you completly change an already existing variable so its completly different instead of making additions? please code examples if you can. thanks?

Steven Parker
Steven Parker
231,271 Points
sample = "yes";      # this creates and assigns the variable
sample += ", sir";   # this adds on to the variable so it contains "yes, sir"
sample = "no";       # this completely changes the existing variable to "no"
del sample;          # this deletes the variable (it becomes undefined)
mohan Abdul
PLUS
mohan Abdul
Courses Plus Student 1,453 Points

Steven Parker, thanks for the speedy response its cleared up a few long standing questions, there is one thing thats nagging me from your response though. Top line """var sample""" should i ignore the "var" as the rest of the labels are just """sample""". once again thank you.

Steven Parker
Steven Parker
231,271 Points

Sorry, I got confused for a moment after answering a question about a different language. :speak_no_evil: I've revised my answer.

Steven Parker your answer answers a lot of the questions being asked on the forum about this reassigning process.

Thank you!

Steven Parker
Steven Parker
231,271 Points

It's nice to know the old answers are still helping folks. Happy coding!