Arithmetic

Interview Questions for Python Arithmetic Operators

below are some commonly asked interview questions along with answers focused on Python arithmetic operators.

Question 1: What are the basic arithmetic operators in Python?

Answer:

In Python, the basic arithmetic operators are:

  • Addition +
  • Subtraction -
  • Multiplication *
  • Division /
  • Modulus %
  • Exponentiation **
  • Floor Division //

Question 2: How do you perform integer division in Python?

Answer:

To perform integer division in Python, you can use the floor division operator //. This will round down the result to the nearest integer.

Python
result = 10 // 3  # Output will be 3

Question 3: What is the difference between / and //?

Answer:

The / operator performs floating-point division, returning a float as the output. The // operator performs integer division, rounding down the result to the nearest integer.

Python
result1 = 10 / 3  # Output: 3.3333
result2 = 10 // 3  # Output: 3

Question 4: What does the modulus operator % do?

Answer:

The modulus operator % returns the remainder of the division of the first operand by the second operand.

Python
remainder = 10 % 3  # Output: 1

Question 5: What is the use of the exponentiation operator **?

Answer:

The exponentiation operator ** raises the number to the power of the exponent specified.

Python
result = 2 ** 3  # Output: 8

Question 6: How do you perform multiple arithmetic operations in a single expression?

Answer:

You can chain multiple arithmetic operations in a single expression, and Python will follow the order of operations (PEMDAS/BODMAS) to evaluate the expression.

Python
result = (2 + 3) * 4 / 2  # Output: 10.0

Question 7: Can you use arithmetic operators with data types other than numbers?

Answer:

Yes, some arithmetic operators can work with other data types. For example, the + operator can concatenate strings and the * operator can repeat a string.

Python
text = "Hello " + "World"  # Output: "Hello World"
repeat = "Hi " * 3  # Output: "Hi Hi Hi "