In general, the data type of an expression is determined by the types of its arguments. This rule applies to the majority of operators: for example, when we add two numbers, the result should be an integer. However, this does not function effectively in the case of division because there are two separate expectations. Sometimes we want division to produce an exact floating point number, but other times we want a rounded-down integer result.
If you tried to split an unequal number of candies equally, you would only have a few remaining candies. Those who remain after division are referred to as remainders.
In this post, we will look at an arithmetic operation called Python Division.
Examples:
Input:
number1=17 number2=4
Output:
number1/number2=4.25 number1//number2=4
Performing Division Operation in Python
- Division Operation
- Division Operation on Two Tuples
- Division Operation on two dictionaries
- Difference between “/” and “//” operators
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
1)Division Operation
Python includes a number of built-in operators and functions for performing mathematical calculations.
The ‘/’ operator is used to execute division operations on data values of both the float and int data types.
The Python ‘/’ operator has the advantage of being able to handle both decimal and negative values.
Syntax:
numb1/numb2
Example1:
Dividing two positive numbers
Below is the implementation:
# given two numbers # given first number numb1 numb1 = 73 # giveen second number numb2 numb2 = 25 # dividing the given two numbers numb1 and numb2 print(numb1, "/", numb2, "=", numb1/numb2)
Output:
73 / 25 = 2.92
Example2:
Dividing one positive number and one negative number
Below is the implementation:
# given two numbers # given first number numb1 numb1 = -73 # giveen second number numb2 numb2 = 25 # dividing the given two numbers numb1 and numb2 print(numb1, "/", numb2, "=", numb1/numb2)
Output:
-73 / 25 = -2.92
Example3:
Dividing two negative numbers
Below is the implementation:
# given two numbers # given first number numb1 numb1 = -73 # giveen second number numb2 numb2 = -25 # dividing the given two numbers numb1 and numb2 print(numb1, "/", numb2, "=", numb1/numb2)
Output:
-73 / -25 = 2.92
Example4:
Dividing one decimal number with the integer
Below is the implementation:
# given two numbers # given first number numb1 numb1 = 73.153 # giveen second number numb2 numb2 = 25 # dividing the given two numbers numb1 and numb2 print(numb1, "/", numb2, "=", numb1/numb2)
Output:
73.153 / 25 = 2.92612
Example5:
Dividing two decimal numbers
Below is the implementation:
# given two numbers # given first number numb1 numb1 = 22.5 # giveen second number numb2 numb2 = 4.5 # dividing the given two numbers numb1 and numb2 print(numb1, "/", numb2, "=", numb1/numb2)
Output:
22.5 / 4.5 = 5.0
2)Division Operation on Two Tuples
Tuple:
Python Tuple is a type of data structure that is used to store a sequence of immutable Python objects. The tuple is similar to lists in that the value of the items placed in the list can be modified, however the tuple is immutable and its value cannot be changed.
The Python floordiv() method, in conjunction 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 element by element. Furthermore, the Python map() function applies any passed/provided function or operation on a set of iterables such as tuples, lists, and so on.
Syntax:
tuple(map(floordiv, tuple1, tuple2))
The floordiv() method divides the items using integer division, which returns only the integer portion of the quotient and ignores the decimal portion.
Below is the implementation:
from operator import floordiv # given two tuples # given tuple1 tuple1 = (17, 27, 13, 25) # given tuple2 tuple2 = (7, 4, 2, 6) # performing division on tuple 1 and tuple 2 division = tuple(map(floordiv, tuple1, tuple2)) # printing the division of tuple 1 and tuple 2 print("printing the division of tuple 1 and tuple 2 : " + str(division))
Output:
printing the division of tuple 1 and tuple 2 : (2, 6, 6, 4)
3)Division Operation on two dictionaries
Python division operations can be performed on dictionary components using the Counter() function and the ‘//’ operator.
The Counter() function stores the dictionary key-value data as dict keys and the dict element count as related values.
The ‘//’ operator divides the data elements at the integer level.
Syntax:
{key: dictionary1[key] // dictionary2[key] for key in dictionary1}
Below is the implementation:
# importing collections from Counter from collections import Counter # given two dictionaries dictionary1 = {'hello': 830, 'This': 250, 'BTechGeeks': 64} dictionary2 = {'hello': 420, 'This': -5, 'BTechGeeks': 4} # using counter to count frequency of elements in two dictionaries dictionary1 = Counter(dictionary1) dictionary2 = Counter(dictionary2) # dividing two dictionaries based on keys division = Counter( {key: dictionary1[key] // dictionary2[key] for key in dictionary1}) # print the result after dividing two dictionaries print("Dividing two dictionaries gives =" + str(dict(division)))
Output:
Dividing two dictionaries gives ={'hello': 1, 'This': -50, 'BTechGeeks': 16}
Explanation:
In the preceding example, we used the Counter() method to store the key-value pairs of the input dictionary in such a way that the input dictionary now has the key as the dictionary items and the value as the count of items existing in the dict.
In addition, we provided the keys to the ‘//’ operator, who performed the division operation.
4)Difference between “/” and “//” operators
The fundamental and most likely only distinction between the ‘/’ and ‘//’ division operators is that the ‘/’ operator returns float values as the result of division, whereas the ‘//’ operator returns the full quotient ( the integer as well as the decimal part).
The ‘//’ division operator, on the other hand, returns an integer value as a result of division, i.e. only the integer component of the quotient value.
Below is the implementation:
print(17/4) print(17//4)
Output:
4.25 4
Conclusion:
As a result of this article, we now understand how to do division operations in Python and ways to perform division operation in Python language.
Related Programs:
- Python Program to Find the Largest Among Three Numbers
- Python Program to Find the Factorial of a Number
- Python Program to Display the multiplication Table
- Python Program to Find the Sum of Natural Numbers
- Python Program to Compute the Power of a Number
- How to Reverse a String in Python
- Python Program to Count the Number of Each Vowel