KnowledgeBoat Logo
|

Informatics Practices

Find the output of the following code:

a=5       
b=2*a         
a+=a+b        
b*=a+b        
print (a,b)  

Python Funda

5 Likes

Answer

20 300

Working

a = 5
b = 2 * a
  • a is initialized to 5.
  • b is set to 2 * a, which means b = 2 * 5 = 10.
a += a + b

The += operator adds the value on the right to a and assigns the result back to a.

  • Current values are:
    • a = 5
    • b = 10
  • Calculation: a + b = 5 + 10 = 15
  • Updating a: a += 15 means a = a + 15
  • So, a = 5 + 15 = 20.
b *= a + b

The *= operator multiplies b by the value on the right and assigns the result back to b.

  • Current values are:
    • a = 20
    • b = 10
  • Calculation: a + b = 20 + 10 = 30
  • Updating b: b *= 30 means b = b * 30
  • So, b = 10 * 30 = 300.
print(a, b)

The final values of the variables are:

  • a = 20
  • b = 300

Hence, the output of the program is:

20 300

Answered By

1 Like


Related Questions