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 (2015) Python Data Types Strings

python basic (numbers)

Hi. I'm a bit confused with calculating numbers.

In the video, it shows that we can write down such as

age=30 age+=7 age The result is 37

Why do we put = between + and 7? I know that it's not gonna work if I just write down age+7 because it's a combination of a string and a number. But the point of question is that do we have to always put = to calculate that combination?

Thank you.

1 Answer

Hi Jooho!

The code age+7 would actually work in this case because the value that the age variable contains is a string and not an integer. What that code does though is takes what age currently is and temporarily adds 7 to it so that it can be used, it doesn't actually change what is in the age variable. So, for example, the code below prints out the value 37 but the actual age variable remains as 30.

age = 30
print (age + 7)

Now, the code age+=7 does something slightly different. What this does is takes the value that is currently in age and sets it equal to itself + 7. It is really just a more streamlined way of doing the following:

age = age + 7

So, the difference is that the first only temporarily manipulates a value whereas the second will actually overwrite the value that is being stored in the variable.

I hope that I managed to help you out here! If you need any more help then don't hesitate to ask for it.

-Luke

Thank you for the straight-forward and easy to follow explanation. I too wondered about the equals +/-.