• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / How to Divide Two Numbers in Python

How to Divide Two Numbers in Python

September 12, 2022 Leave a Comment

To divide two numbers in Python, you can use the division operator /. You can divide integers, floats, and decimal variables.

a = 1
b = 2

c = a / b

print(c)

#Output:
0.5

One of the most fundamental operations in programming is performing different calculations and math.

You can easily divide two numbers in Python with the / division operator.

You can divide integers, floats, and decimal variables in Python with /.

The rest of this article will show you examples of how you can use / to divide two numbers in Python.

How to Divide Two Integers in Python

The most basic use of / is if you have two integers and you want to divide them together.

Below shows you a simple example of using / to divide two integers in Python.

a = 1
b = 2

c = a / b

print(c)

#Output:
0.5

Performing Floor Division with // in Python

In Python, we can perform division of numbers in different ways. You can use both // and / to divide numbers

The difference between // and / is that // performs floor division, and / performs floating point division.

Floating point division is regular division, and floor division truncates the resulting quotient.

Below are a few examples of the difference between // and / in Python.

print(10/3)
print(10//3)

print(93/4)
print(93//4)

#Output:
3.333333333333335
3
23.25
23

How to Divide Two Floats in Python

Another type of number that you can use in Python is a floating point number. Floats allow you to create numbers which have a decimal portion.

Below shows you a simple example of using / to divide two floats in Python.

a = 1.0
b = 2.0

c = a / b

print(c)

#Output:
0.5

How to Divide a Float and an Integer in Python

You can divide a float and an integer together with /. The result will be a float.

Below shows you how to divide a float and an integer together in Python.

a = 1
b = 2.0

c = a / b

print(c)

#Output:
0.5

How to Divide Two Decimals in Python

Another type of number that you can use in Python is a decimal. The decimal module provides support for fast correctly rounded decimal floating point arithmetic.

Below shows you a simple example of using / to divide two decimals in Python.

from decimal import *

a = Decimal('1.01')
b = Decimal('2.02')

c = a / b

print(c)

#Output:
0.5

Hopefully this article has been useful for you to learn how to divide two numbers in Python.

Other Articles You'll Also Like:

  • 1.  Mastering Column Renaming in Pandas: A Step-by-Step Guide
  • 2.  Using Python to Count Items in List Matching Criteria
  • 3.  Replace Character in String in Python
  • 4.  Python turtle write() – Write Text to Turtle Screen
  • 5.  Sort List of Tuples in Python
  • 6.  pandas mad – Calculate Mean Absolute Deviation in Python
  • 7.  Count Number of Files in Directory with Python
  • 8.  Subtract Seconds from Datetime Variable Using Python timedelta() Function
  • 9.  Sorting a List of Tuples by Second Element in Python
  • 10.  Python tan – Find Tangent of Number in Radians Using math.tan()

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Copyright © 2023 · The Programming Expert · About · Privacy Policy