0

I know this is a very simple question, but I couldn't find it. Basically what I'm trying to is have a=2, b=3, and I need a+b=b so when I add a and b what it equals needs to be b.

1
  • 3
    b = a + b is what you're looking for, or b += a Commented Nov 8, 2014 at 21:33

4 Answers 4

2

Either:

b = a + b
# make b equal to a + b

Or

b += a
# increase the value of b by a
Sign up to request clarification or add additional context in comments.

Comments

2

declare variables

a = 2
b = 3

operates

b = a+b

1 Comment

You can even recommend the usual b += a.
1

You can use the += shorthand for summation on variable.

For instance, in order to add in the value of a in the b variable, and retaining the last value of b:

b += a

This is the same thing as saying: b = a + b. Both works fine. Choose as you feel is readable.

Comments

0

Here is a quick example:

First, initialize your variables:

a = 2
b = 3
print a, b 

Output: 2 3

You can add a to b, this way:

b += a
print b

Output: 5

You can also add to the actual value of b 10

b = 10+b
print b

Output: 15

1 Comment

Side note: I encourage you to read and follow PEP 8: it contains Python style recommendations that are widely followed. This will make your code more legible (I fixed the style in your answer so that it follows the common convention).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.