PYTHON PROGRAM • OPERATORS & NUMBERS

Demonstrate Arithmetic Operators in Python

Compare addition, subtraction, multiplication and true division using two Python integers.

BeginnerArithmetic operators

PROBLEM UNDERSTANDING

Input and expected output

Sample input
No input required
Sample output
22 12 85 3.4

COMPLETE PYTHON PROGRAM

Complete Python implementation

python-arithmetic-operators.py
Open in compiler
a, b = 17, 5
print(a + b, a - b, a * b, a / b)

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

22 12 85 3.4
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Assign a = 17 and b = 5; this example does not read keyboard input.
  2. Evaluate a + b = 22, a - b = 12 and a * b = 85.
  3. Evaluate a / b = 3.4; Python true division returns a float for these integer operands.
  4. print displays the four results in order, separated by spaces.

The output 22 12 85 3.4 follows the order of the four expressions passed to print. Changing a or b changes the results without changing the operator meanings. The / operator performs true division, while // performs floor division: 17 // 5 is 3, but -17 // 5 is -4 because the result is rounded toward negative infinity. Dividing by zero raises ZeroDivisionError. The O(1) label describes this fixed-size demonstration; arithmetic on arbitrarily large Python integers can require increasing time and memory.

EFFICIENCY

Time and space complexity

Time complexity

O(1)

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Do not confuse / with //: they have different results and meanings.

Check this

Set b to a non-zero value before evaluating a / b.

Check this

Predict results in expression order; print does not reorder them.

Try it yourself

Practice: Change a and b to 10 and 4: expect 14 6 40 2.5. Then try -17 and 5: expect -12 -22 -85 -3.4. Predict the error when b = 0 and explain why printing the other expressions does not complete.