October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Divide Two Numbers in Python: Program and Algorithm

A simple Python program for dividing two numbers, with the algorithm, zero-divisor handling, operator differences, and precision guidance.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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

  1. Read or assign the dividend, the number being divided.
  2. Read or assign the divisor, the number you divide by.
  3. Check whether the divisor is zero if it can come from variable input.
  4. Divide the dividend by the divisor with /.
  5. 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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.