Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?

2 Answers

Steven Parker
Steven Parker
210,872 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
210,872 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
210,872 Points

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