Informatics Practices

Find the output of the following code:

p = 5 % 2
q = p ** 4
r = p // q
p+= p + q + r
r+= p + q + r
q-= p + q * r
print(p, q, r)

Python Funda

1 Like

Answer

4 -10 7

Working

p = 5 % 2

The modulo % operator calculates the remainder of the division.

  • 5 \% 2 = 1
  • Thus, p = 1
q = p ** 4

The exponent ** operator raises p to the power of 4.

  • 1 ** 4 = 1
  • Thus, q = 1
r = p // q

The floor division // operator divides and returns the integer quotient of the division.

  • 1 // 1 = 1
  • Thus, r = 1
p += p + q + r

The += operator adds the value on the right to the variable and assigns it to the variable again.

  • Initially, p = 1
  • Calculation inside the parentheses: (p + q + r) = 1 + 1 + 1 = 3
  • p += 3 means p = p + 3
  • So, p = 1 + 3 = 4
r += p + q + r

The += operator adds the value on the right to the variable and assigns it to the variable again.

  • Initially, r = 1
  • Calculation inside the parentheses: (p + q + r) = 4 + 1 + 1 = 6
  • r += 6 means r = r + 6
  • So, r = 1 + 6 = 7
q -= p + q * r

The -= operator subtracts the value on the right from the variable and assigns it to the variable again.

  • Initially, q = 1
  • Calculation inside the parentheses: (p + q * r) = 4 + 1 * 7 = 4 + 7 = 11
  • q -= 11 means q = q - 11
  • So, q = 1 - 11 = -10
print(p, q, r)

The variables now have these final values:

  • p = 4
  • q = -10
  • r = 7

Hence, the output of the program is:

4 -10 7

Answered By

2 Likes


Related Questions