Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (240 points)
Hi all. As per the above title, I want some help with the expected output when I go about using the += operation in Python.

All help appreciated.

2 Answers

0 votes
by (300 points)

Hi. So, it is very simple. The += adds another single value to the existing value. So, you can guess what happens later. This values gets added to the variable and it is used. 

Check out the code below:
 

>>> x = 3

>>> x += 2

>>> print x

5

You can have similar operations for subtraction, division and even multiplication.

Hope this helped!

0 votes
by (106k points)

While working with Python x += 5  is not exactly the same as saying x = x + 5. It varies where and in which context you are using it I will show you two examples regarding this:-

Example 1:-

In this example you will see += adds another value with the variable's value and assigns the new value to the variable.

x = 3

x += 2

print(x)

Example 2:-

When we do x+=[] it just merged the elements of the array with the assigned values and make a combined array including those values.

x = [2,3,4]    

y = x    

x += 7,8,9 

print(x)

print(y)

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...