FAQ: Learn Python: Syntax - Plus Equals

This community-built FAQ covers the “Plus Equals” exercise from the lesson “Learn Python: Syntax”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Plus Equals

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Using print() without concatenation?

Since this lesson involves money, is there a way to format the float output to include the hundredths place, i.e. 109.00?

Can we add string with numeric values we updated using the “+=”, if yes how ?
for eg
if abc =10
abc +=5
abc += 10
abc += “string” ?? (here can we concatenate the value with the number added.)
print(abc)

no, we can’t. What is that suppose to look like then?

you could convert the integer to string, then glue the strings together, that is possible.

Instead of writing number_of_miles_hiked = 12
number_of_miles_hiked += 2
print(number_of_miles_hiked) , can’t we instead just add 2 to the first variable, i.e.,
number_of_miles_hiked = 12 + 2
print(number_of_miles_hiked)

They mention that “we keep a running count of the number of miles a person has gone hiking over time.” If we were to create a lap time for each extra mile, then =+ makes sense. Is that what they’re trying to say? If so, in what other situations will this be used?

Also, can anyone explain with code about this statement: “Instead of recalculating from the start, we keep a grand total and update it when we’ve gone hiking further.”? Thank you.

These concepts are fundamental, you will see that you need them later once you start doing porjects.

Sure, this example is a bit lame, but we can’t make you do a complicated project even though this would show the more practical side of updating existing variables.

Oh, okay. Thank you.

Hi, the += can be used to add more variables at a time as written in the FAQ. Within the python exercise with the following alternative answer I always get a bug… Is someone able to explain the difference? Thanks!!

total_price += nice_sweater += fun_books (=> this leads to an error). INSTEAD of the suggested code:

total_price += nice_sweater
total_price += fun_books

+= is a shorthand, these two statements are the same:

x = x + y
x += y

given increasing the value of a variable is such a common operation.

knowing this, how would you re-write this line:

total_price += nice_sweater += fun_books

?

Hi,

Why does writing

total_price ± fun_books

not change the total price? Can we decrease too?

EDIT: Wrote it wrong, decreasing does work. Should be

total_price -= fun_books

The print statement reads “print(“The total price is”, total_price)” where there is no space after the word “is” in the string, but when you run the code the sentence it outputs contains the proper space. Just a miniscule error I noticed today! :grinning:

1 Like

In the example it’s written:
hike_caption += " #nofilter"
hike_caption += " #blessed"

What does #nofilter and #blessed mean?

1 Like

nothing particular, just an example of what kind of hashtag people would add to there Instagram post.

1 Like

I Don’t use Instagram, that’s why I didn’t know :slightly_smiling_face:
Thank you stetim94.

I don’t either, but I know of the existing of hashtag or tags on sites like twitter and Instagram (and lot of others)

1 Like
total_price = 0

new_sneakers = 50.00

total_price += new_sneakers

nice_sweater = 39.00
fun_books = 20.00

total_price += nice_sweater + fun_books
#Update total_price here:

print(“The total price is”, + total_price)

Why does this last print work? total_price is a float, doesn’t it need to be converted to a string to be concatenated with a string?

print(“The total price is”, + str(total_price))

Why does this code give me an error? TypeError: bad operand type for unary +: ‘str’

1 Like

string concatenation/joining does indeed require strings.

although printing multiple argument should work too. I can’t reproduce your error unless I take out the comma

although printing multiple argument should work too.

I’m a newb so I need some clarification here. Do you mean that there’s no issue printing a string and an integer/float together? For example;

float_variable = 6.5
string_variable = “A random number is “

print(string_variable + float_variable)

Should return: A random number is 6.5

Is that right? Or does the phrase “multiple arguments” mean something else in this context?

there are two things: string concatenation and printing multiple things with the print function.

for string concatenation, both elements need to be string

if you want to print multiple things, you can simple separate the arguments with a comma:

print(string_variable, float_variable)