Thursday , September 19 2024

What is the output of the following piece of code? x = 1 // 5 + 1 / 5 print(x)

Questions BankCategory: Python Essentials 1What is the output of the following piece of code? x = 1 // 5 + 1 / 5 print(x)
What is the output of the following piece of code?

x = 1 // 5 + 1 / 5
print(x)
  • 0.4
  • 0.4
  • 0
  • 0.2

Explanation: Remember the principle of Operator Precedence, and the difference between the two Python division operators: // (integer division) and / (floating-point division). Let’s analyze the expression in the first line:

Because the division operators have a higher priority than the addition operator, we can add brackets for readability, and evaluate the expression in the following way: (1 // 5) + (1 / 5) gives 0 + 0.2, which in turn evaluates to 0.2.

More Questions: Python Essentials 1 (PE1) Course Final Test