Divide function in python

What is the Integer and Float Division in Python

To divide float values in Python, you can use the “/” operator. The Division operator “/” takes two parameters and returns the float division. Float division produces a floating-point conjecture of the result of a division.

If you are working with Python 3 and need to perform a float division, use the division operator.

Only a specific number of values after the decimal can be saved, so storing an exact binary description of many floating-point numbers is impossible.

Float division for Python 2

In Python 2, the only standard division operator is “/”. If both values are integers, the result is an integer. If either of the values is the float, the return is the float value.

To perform float division in Python 2, import the division package __future__ module and then use the “\\” operator to get the result.

from __future__ import division print(10 // 9)

In Python 3, the single slash (/) means proper division. The // operator is used for truncating division.

Читайте также:  Распечатать значения словаря python

Adding a dot (.) operator indicates the floating-point numbers.

Integer division ( // )

The integer division operation // is used when you need to get the number as a whole number.

main_int_1 = 21 main_int_2 = 10 print("Result of integer division of two integers: ", main_int_1//main_int_2) 
Result of integer division of two integers: 2 

Источник

Python Program to Divide Two Numbers using Function

We will develop a Python program to divide two numbers using Function. The division is one of the four basic mathematical operations, the other three being addition, subtraction, and multiplication. In simple words, division can be defined as the splitting of a large group into equal smaller groups.

We will give two numbers num1 and num2. Then, divide numbers using the division operator (/). We can take the help of a function to divide two numbers. A function is a block of code that performs a specific task.

How to divide two number:
Product = a / b

Mathematically,

Inputs: a=50, b=5
Product = a / b = 50 / 5 = 10

How to Divide Two Numbers in Python using Function

This is the simplest and easiest way to divide two numbers in Python. We will take two numbers while declaring the variables and divide numbers using a user-defined function. Its value will be stored in the division variable using a function call and finally, the division value will be displayed on the screen.

# Python program to divide two numbers using function def div_Num(num1, num2): #user-defined function div = (num1/num2) #divide numbers return div #return value # take inputs num1 = 25 num2 = 5 # function call division = div_Num(num1, num2) # print value print("The division of and is " .format(num1,num2,division)) 

The division of 25 and 5 is 5.0

Python Program to Divide Two Numbers using Function

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user. Inputs are scanned using the input() function and stored in variable num1, and num2.

# Python program to divide two numbers using function def div_Num(num1, num2): #user-defined function div = (num1/num2) #divide numbers return div #return value # take inputs num1 = float(input('Enter first number: ')) num2 = float(input('Enter second number: ')) # function call division = div_Num(num1, num2) # print value print("The division of and is " .format(num1,num2,division)) 

Output for the different input values:-

Enter first number: 42
Enter second number: 6
The division of 42.0 and 6.0 is 7.0

Enter first number: 256
Enter second number: 31
The division of 256.0 and 31.0 is 8.26

Enter first number: 124
Enter second number: 3.2
The division of 124.0 and 3.2 is 38.75

Python Program to Divide Two Numbers using Recursion

In this program, divide two numbers using recursion. A function/method that contains a call to itself is called the recursive function/method. A technique of defining the recursive function/method is called recursion.

# Python program to divide two numbers using recursion def div_Num(x,y): #user-defined function if (y==0): return 0; elif (x-y==0): return 1; elif (x and is " .format(num1,num2,division)) 

Enter first number: 240
Enter second number: 80
The division of 240 and 80 is 3

Источник

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.

Python program to divide two numbers

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.

Python program to divide numbers user input

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.

Python program to divide numbers using functions

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.

Python program to divide numbers using recursion

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.

Python program to divide numbers using class

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.

Python program to divide complex numbers

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.

Program to divide two float number using a function

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.

how to divide a variable by a number in python

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.

Источник

Оцените статью