In Python, division can be done by using the ‘/’ operator on float and int datatype values.
As a consequence of division, the ‘//’ division operator returns an integer value, i.e. only the integer part of the quotient value.
Python Division Operation:
Python includes a number of built-in operators and functions for performing mathematical calculations.
The ‘/’ operator is used to do division operations on data values of both ‘float’ and ‘int’ datatypes.
The Python ‘/’ operator has the advantage of being able to handle both decimal and negative values.
Syntax:
num1/num2
The operator works with numbers and returns a floating-point value as a result. The result of the division operation is the Quotient, which is represented as a floating-point value.
Example1: (Dynamic input)
# Give the first number as user input using the int(input()) function and # store it in a variable. gvnnum_1 = int(input("Enter some random number = ")) # Give the second number as user input using the int(input()) function and # store it in another variable. gvnnum_2 = int(input("Enter some random number = ")) # Calculate the value of Division of the given first number by second number # using the '/' operator and store it in another variable. div_rslt = gvnnum_1/gvnnum_2 # Print the division of the given first number by second numnber print("The division of {", gvnnum_1, "/", gvnnum_2, "} =", div_rslt)
Output:
Enter some random number = 15 Enter some random number = 3 The division of { 15 / 3 } = 5.0
Example2: (Static input)
# Give the first number as static input and store it in a variable. gvnnum_1 = -25 # Give the second number as static input and store it in another variable. gvnnum_2 = 4 # Calculate the value of Division of the given first number by second number # using the '/' operator and store it in another variable. div_rslt = gvnnum_1/gvnnum_2 # Print the division of the given first number by second numnber print("The division of {", gvnnum_1, "/", gvnnum_2, "} =", div_rslt)
Output:
The division of { -25 / 4 } = -6.25
Tuple Division Operation in Python:
The Python floordiv() method, in combination with the map() function, can be used to divide data values contained in a Tuple data structure.
The floordiv() method in Python is used to divide all of the elements of a data structure, i.e. it divides the data structure elementwise. Furthermore, the Python map() function applies any specified function or operation on a set of iterables such as tuples, lists, and so on.
The floordiv() method divides the elements and returns only the integer part of the quotient, skipping the decimal part.
Syntax:
tuple(map(floordiv, tuple_1, tuple_2))
Example
# Import floordiv function from the operator module using the import keyword. from operator import floordiv # Give the first tuple as static input and store it in a variable. gvn_fsttupl = (12, 15, -25, 40) # Give the second tuple as static input and store it in another variable. gvn_scndtupl = (3, 5, 5, 10) # Pass the floordiv() function, given first and second tuples as the arguments to the # map() function and convert the result into a tuple using the tuple() function. # Store it in another variable. div_rslt = tuple(map(floordiv, gvn_fsttupl, gvn_scndtupl)) # Print the element-wise division of given first tuple by second tuple print("The element-wise division of given first tuple by second tuple =", div_rslt)
Output:
The element-wise division of given first tuple by second tuple = (4, 3, -5, 4)
Dictionary Division Operation in Python:
Python division operations can be performed on dictionary elements using the Counter() function and the ‘//’ operator.
The Counter() function stores the dictionary key-value data as dictionary keys and the dictionary element count as related values.
The ‘//’ operator divides the data elements at the integer level.
Syntax:
Counter({key : dict1[key] // dict2[key] for key in dict1})
Example
# Import Counter function from collections module using the import keyword. from collections import Counter # Give the first dictionary as static input and store it in a variable. gvn_fstdictnry = {'hello': 100, 'this is': 40, 'btechgeeks': 36} # Give the second dictionary as static input and store it in another variable. gvn_scnddictnry = {'hello': 5, 'this is': 2, 'btechgeeks': 3} # Divide the values of the two dictionary for every key in the two dictionaries and # apply counter function. div_rslt = Counter( {key: gvn_fstdictnry[key] // gvn_scnddictnry[key] for key in gvn_fstdictnry}) # Print the above division result. print("The division of given first dictionary by second dictionary = ", dict(div_rslt))
Output:
The division of given first dictionary by second dictionary = {'hello': 20, 'this is': 20, 'btechgeeks': 12}
Differences: ‘/’ vs. ‘//’ Operators
The fundamental and most likely only difference between the ‘/’ and ‘//’ division operators is:
- ‘/’ operator returns float values as the result of division i.e, returns the full quotient ( integer as well as the decimal part).
- ‘//’ operator returns integer values as the result of division. i.e returns only integer part.
Example
print(25/4) print(25//4)
Output:
6.25 6