Bird
Raised Fist0
Pythonprogramming~10 mins

Arithmetic operators in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add two numbers and print the result.

Python
result = 5 [1] 3
print(result)
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' will subtract instead of add.
Using '*' or '/' will multiply or divide, not add.
2fill in blank
medium

Complete the code to multiply two numbers and print the result.

Python
result = 7 [1] 4
print(result)
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds numbers instead of multiplying.
Using '/' divides numbers, not multiply.
3fill in blank
hard

Fix the error in the code to correctly divide two numbers and print the result.

Python
result = 10 [1] 2
print(result)
Drag options to blanks, or click blank then click option'
A+
B*
C/
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' multiplies instead of dividing.
Using '+' or '-' changes the operation to addition or subtraction.
4fill in blank
hard

Fill both blanks to create a dictionary of squares for numbers greater than 3.

Python
squares = {x: x[1]2 for x in range(1, 6) if x [2] 3}
print(squares)
Drag options to blanks, or click blank then click option'
A**
B+
C>
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '**' will add 2 instead of squaring.
Using '<' instead of '>' will select numbers less than 3.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase keys and values greater than zero.

Python
result = [1]: [2] for k, v in data.items() if v [3] 0}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k.upper() keeps keys lowercase.
Using '<' instead of '>' filters wrong values.

Practice

(1/5)
1. Which arithmetic operator in Python is used to find the remainder of a division?
easy
A. **
B. //
C. %
D. +

Solution

  1. Step 1: Understand the meaning of each operator

    The operator % gives the remainder after division, // gives the floor division result, ** is for power, and + is for addition.
  2. Step 2: Identify the operator for remainder

    The remainder operator is %, which returns the leftover part after dividing two numbers.
  3. Final Answer:

    % -> Option C
  4. Quick Check:

    Remainder operator = % [OK]
Hint: Remainder operator looks like a percent sign % [OK]
Common Mistakes:
  • Confusing // (floor division) with %
  • Using ** instead of %
  • Thinking + gives remainder
2. Which of the following is the correct operator syntax to calculate 5 to the power of 3 in Python?
easy
A. 5 ^ 3
B. pow(5, 3)
C. 5 ^^ 3
D. 5 ** 3

Solution

  1. Step 1: Recall Python's power operator syntax

    Python uses ** to calculate powers, so 5 ** 3 means 5 to the power of 3.
  2. Step 2: Check other options for correctness

    5 ^ 3 is bitwise XOR, 5 ^^ 3 is invalid syntax, and pow(5, 3) is a function call but not an operator syntax.
  3. Final Answer:

    5 ** 3 -> Option D
  4. Quick Check:

    Power operator = ** [OK]
Hint: Use ** for power, not ^ or ^^ [OK]
Common Mistakes:
  • Using ^ instead of ** for power
  • Trying ^^ which is invalid
  • Confusing function pow() with operator syntax
3. What is the output of the following code?
result = 17 // 4
print(result)
medium
A. 4.25
B. 4
C. 1
D. Error

Solution

  1. Step 1: Understand floor division operator //

    The operator // divides and returns the largest whole number less than or equal to the result.
  2. Step 2: Calculate 17 // 4

    17 divided by 4 is 4.25, floor division drops the decimal part, so result is 4.
  3. Final Answer:

    4 -> Option B
  4. Quick Check:

    17 // 4 = 4 [OK]
Hint: Floor division drops decimals, so 17//4 is 4 [OK]
Common Mistakes:
  • Confusing // with / which gives float
  • Expecting 4.25 instead of 4
  • Thinking // rounds up
4. Find the error in this code snippet:
num = 10
result = num % 0
print(result)
medium
A. ZeroDivisionError at runtime
B. No error, output is 0
C. SyntaxError due to % operator
D. TypeError because 0 is int

Solution

  1. Step 1: Understand modulo operator with zero

    The modulo operator % cannot divide by zero; it causes an error.
  2. Step 2: Identify the error type

    Dividing or modulo by zero raises a ZeroDivisionError at runtime.
  3. Final Answer:

    ZeroDivisionError at runtime -> Option A
  4. Quick Check:

    Modulo by zero causes ZeroDivisionError [OK]
Hint: Modulo by zero causes runtime error, never allowed [OK]
Common Mistakes:
  • Thinking it causes syntax error
  • Expecting output 0 instead of error
  • Confusing with TypeError
5. You want to calculate the area of a square with side length stored in variable side. Which expression correctly uses arithmetic operators to do this?
hard
A. area = side ** 2
B. area = side // 2
C. area = side + side
D. area = side * 2

Solution

  1. Step 1: Recall formula for square area

    The area of a square is side length multiplied by itself, or side squared.
  2. Step 2: Match formula with operators

    Using side ** 2 calculates side to the power of 2, which is correct for area.
  3. Final Answer:

    area = side ** 2 -> Option A
  4. Quick Check:

    Square area = side squared = side ** 2 [OK]
Hint: Square area = side squared, use ** 2 [OK]
Common Mistakes:
  • Using side * 2 which doubles side, not area
  • Using side + side which adds sides, not area
  • Using floor division // which is incorrect