- How to Divide Two Numbers in Python
- How to Divide Two Integers in Python
- Performing Floor Division with // in Python
- How to Divide Two Floats in Python
- How to Divide a Float and an Integer in Python
- How to Divide Two Decimals in Python
- Other Articles You’ll Also Like:
- About The Programming Expert
- How to divide two numbers in Python
- Python program to divide numbers user input
- Python program to divide numbers using functions
- Python program to divide numbers using recursion
- Python program to divide complex numbers
- Program to divide two float number using a function
- How to divide a variable by a number in Python
- Python Division: Integer Division and Float Division
- Python Division – The different ways
- Python Floor Division
- Python Float Division
- Conclusion
How to Divide Two Numbers in Python
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. Python Print List – Printing Elements of List to the Console
- 2. Combine Sets in Python
- 3. Python Random Boolean – How to Generate Random Boolean Values
- 4. How to Write CSV File to AWS S3 Bucket Using Python
- 5. Remove Specific Word from String in Python
- 6. Get Difference Between datetime Variables in Python
- 7. Find Quotient and Remainder After Division in Python
- 8. Check if Variable is String in Python
- 9. Find Last Occurrence in String of Character or Substring in Python
- 10. Python Even or Odd – Check if Number is Even or Odd Using % Operator
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.
How to divide two numbers in Python
Here, we can see how to write program to divide two numbers in python.
- In this example, I have taken two numbers as number1 = 64, number2 = 8.
- To divide the numbers “/” operator is used.
- I have used print(result) to get the output.
number1 = 64 number2 = 8 result = number1/number2 print(result)
The below screenshot shows the output.
This code, we can use how to divide two numbers in Python.
Python program to divide numbers user input
Now, we can see how to write program to divide numbers user input in python.
- In this example, I have used the input() method. To take the inputs from the user.
- The result=number1/number2 is used to divide the two numbers.
- I have used print(result) to get the output.
number1=int(input("Enter the first number: ")) number2=int(input("Enter the second number: ")) result=number1/number2; print(result)
You can refer to the below screenshot for the output.
This is the code to divide numbers with user input in Python.
Python program to divide numbers using functions
Here, we can see how to write program to divide numbers using function in python.
- In this example, I have defined a function called div as def div(a,b).
- The function is returned as return a/b.
- The values to be divided are passed as the parameter in the function.
def div(a,b): return a/b print(div(27,9))
We can the division of 27 and 9 is 3 as the output. You can refer to the below screenshot for the output.
This is how to divide numbers using functions in Python.
Python program to divide numbers using recursion
Now, we can see how to write program to divide numbers using recursion in python.
- In this example, I have used the input() method. To take the inputs from the user.
- I have defined a function as def Div(number1,number2).
- The if condition is used, If the condition is true it returns as return 0. if the condition is not true it returns return 1 + Div(number1-number2, number2).
- To get the output, I have used print(“result “,Div(number1,number2)).
print("Enter the two Number:") number1=int(input()) number2=int(input()) def Div(number1,number2): if number1
We can see the division of two numbers as the output. You can refer to the below screenshot for the output.
This is how to divide numbers using recursion in Python.
Python program to divide numbers using class
- In this example, I have used the input() method. to take the inputs from the user.
- I have created a class using a constructor to initialize the value of the class
- I have created a method to divide the numbers.
- The object is created for the class to pass the parameter.
- The self is the keyword used to pass the attributes and methods to the class.
- To get the output, I have used print(“Result: “,obj.num()).
a=int(input("Enter first number: ")) b=int(input("Enter second number: ")) class div(): def __init__(self,a,b): self.a=a self.b=b def num(self): return self.a/self.b obj=div(a,b) print("Result: ",obj.num())
The below screenshot shows the division of 24 and 6 is 4 as the output.
This is the Python program to divide numbers using class.
Python program to divide complex numbers
Here, we can see how to write program to divide complex numbers in Python.
- In this example, I have taken two complex number as a = complex(16, 36) and b = complex(4, 6).
- I have defined a function as def subComplex(a,b).
- The defined function is returned as return a / b.
- To get the output, I have used print( “Result is : “, subComplex(a, b)).
a = complex(16, 36) b = complex(4, 6) def subComplex( a, b): return a / b print( "Result is : ", subComplex(a, b))
We can see the division of complex numbers as the output. You can refer to the below screenshot for the output.
This is how to divide complex numbers in Python.
Program to divide two float number using a function
Here, we can see how to write a program to divide two float number using a function in python.
- In this example, I have used the input() method. To take the inputs from the user.
- I have defined a function as def division(a,b).
- The function is returned as div=a/b.
- I have used print(“Result is: “,division(number1,number2)) to get the output.
number1=float(input("Enter first number: ")) number2=float(input("Enter second number: ")) def division(a,b): div=a/b return div print("Result is: ",division(number1,number2))
We can the division of two numbers as the output. You can refer to the below screenshot for the output.
This is how to divide two float number using a function in Python.
How to divide a variable by a number in Python
Here, we can see how to divide a variable by a number in python.
- In this example, I have taken a variable as variable1 = 100.
- To divide the variable by the number, I have used variable = variable1 / 10. Here 10 is the number that I am using to divide a variable.
- I have used print(variable) to get the output.
variable1 = 100 variable = variable1 / 10 print(variable)
You can refer to the below screenshot for the output.
The above code, we can use to divide a variable by a number in Python.
You may like the following Python tutorials:
In this Python tutorial, we have learned about the Python programs to divide two numbers. Also, we covered these below topics:
- Python program to divide two numbers
- Python program to divide numbers user input
- Python program to divide numbers using functions
- Python program to divide numbers using recursion
- Python program to divide numbers using class
- Python program to divide complex numbers
- Program to divide two float number using a function
- How to divide a variable by a number in python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.
Python Division: Integer Division and Float Division
In this post, you’ll learn Python 3 division, as well as some of its unexpected quirks. You’ll learn how to use both integer and floor methods, as well as how to interpret some of its less expected results.
Python Division – The different ways
Python has two different division operators, / and // . Which one you use depends on the result that you want to achieve.
- The single forward slash / operator is known as float division, which returns a floating point value.
- On the other hand, the double forward slash // operator returns a floored value, specifically either a floored integer or floating point value.
Python Floor Division
Python floor division, using the // operator, works by returning the floored value of its quotient. This works different than integer division which would round the number. Instead, this works by returning the floor value.
Let’s see how this works in action, by looking at a few examples:
# Two float values >> 7.0 // 2.0 3.0 # A single float value >> 7 // 2.0 3.0 # Two integers >> 7 // 2 3
One interesting note about this is what happens with negative numbers:
Logically, this makes sense. The result will be rounded down (i.e., floored), meaning that while we may expect it be equal to -2.0 , rounded down, the value is correctly -3.0 .
Python Float Division
Python float division uses the / operator and returns, well, a floating point value. This, perhaps, is more of how you’d expect division to work. Let’s look at a few more examples:
# Two integers >> 7 / 3 2.33 # One floating point value >> 7.0 / 3 2.33 # Two floating point values >> 7.0 / 3.0 2.33
As you can see, the results return values you’d expect, regardless of whether you’re dividing integers, floats, or a mix of both.
Conclusion
Dividing in Python offers different ways to, well, divide numbers. Having a firm understanding of these operators makes you a much better programmer by having a solid understanding of the basics. To learn more about these, check out the official documentation.
To learn more about related topics, check out the tutorials below: