In this python tutorial, you will learn about how to divide two numbers in Python and also we will check:
- 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
Python program to divide two numbers
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.
Example:
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.
Example:
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.
Example:
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)).
Example:
print("Enter the two Number:")
number1=int(input())
number2=int(input())
def Div(number1,number2):
if number1<number2:
return 0
return 1 + Div(number1-number2, number2)
print("result ",Div(number1,number2))
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()).
Example:
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)).
Example:
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.
Example:
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.
Example:
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:
- How to Set Background to be an Image in Python Tkinter
- How to add two variables in Python
- How to add two numbers in Python
- Python program to reverse a string with examples
- Python program to find sum of n numbers with examples
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
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.