Use Python’s / operator to divide two numbers and get the ordinary quotient. For a program that accepts input, convert the entries to numbers, check that the divisor is not zero, then divide and print the result.
Python program to divide two numbers
This version reads two values, handles a zero divisor, and prints the quotient:
first = float(input("Enter the first number: "))
second = float(input("Enter the second number: "))
if second == 0:
print("Cannot divide by zero.")
else:
print("Quotient:", first / second)
For example, entering 10 and 2 displays Quotient: 5.0. Python’s tutorial explains that ordinary division with / returns a floating-point value, including when both operands are integers.
Algorithm for dividing two numbers
- Read or assign the dividend, the number being divided.
- Read or assign the divisor, the number you divide by.
- Check whether the divisor is zero if it can come from variable input.
- Divide the dividend by the divisor with
/. - Display or return the quotient.
Use a function when you need reusable division
A function can return the result so other parts of a program can use it. This example chooses to raise ValueError when the divisor is zero:
Recommended Free Tools
#1 Best Overall
def divide(a, b):
if b == 0:
raise ValueError("divisor cannot be zero")
return a / b
print(divide(10, 2)) # 5.0
This is a validation choice made by the function. If you divide by zero without that check, Python raises ZeroDivisionError; see the documentation on arithmetic expressions. A program may instead catch that exception if it needs to recover from invalid input.
Choose the right division operator
| Operator | What it returns | Example | Negative-number behavior |
|---|---|---|---|
/ |
Ordinary quotient; integer operands produce a float | 10 / 2 gives 5.0 |
Returns the ordinary signed quotient |
// |
Floor quotient | 17 // 3 gives 5 |
For built-in integers, floors toward negative infinity: -7 // 3 gives -3, not -2 |
% |
Remainder | 17 % 3 gives 2 |
Its result follows Python’s quotient-and-remainder rules |
Use // only when you want floor division rather than a fractional quotient. Use % when you need the remainder. The built-in divmod(a, b) returns both values as (a // b, a % b); for example, divmod(17, 3) returns (5, 2). These operator rules are described in the Python documentation on numeric types.
Rank #2
Floating-point precision and other number types
Python’s ordinary float values use binary floating-point representation. Many decimal fractions cannot be represented exactly in binary, so a calculation may display a tiny approximation rather than the decimal value you expect. The floating-point tutorial explains why.
- For ordinary beginner exercises, integers or floats with
/are appropriate. - For exact rational values such as one third, use
fractions.Fraction, documented under fractions. - For decimal arithmetic that needs a defined precision and rounding policy, use
decimal.Decimal, documented under decimal. Construct decimal values from strings and decide how the result should be rounded.
Calling round(result, 2) can format a result to two decimal places, but it does not make earlier floating-point calculations exact. Also note a type-specific difference: built-in integer // floors toward negative infinity, while Decimal division with // truncates toward zero.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsQuick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




