Division Operators in Python
Division operators in Python are used to perform various types of division operations between numbers. Python provides three main division-related operators: standard division (/), floor division (//), and modulus (%). Here’s an overview of each, with examples to illustrate their use.
1. Division (/)
The standard division operator returns a float.
a = 10
b = 3
result = a / b
print(result) # Output: 3.3333333333333335
2. Floor Division (//)
The floor division operator returns the largest integer less than or equal to the result (essentially performs integer division).
a = 10
b = 3
result = a // b
print(result) # Output: 3
3. Modulus (%)
The modulus operator returns the remainder of the division.
a = 10
b = 3
result = a % b
print(result) # Output: 1
Examples of using division operators with negative numbers
Division (/)
a = -10
b = 3
result = a / b
print(result) # Output: -3.3333333333333335
Floor Division (//)
a = -10
b = 3
result = a // b
print(result) # Output: -4
Modulus (%)
a = -10
b = 3
result = a % b
print(result) # Output: 2
Python Combining Division Operators
We can combine these operators to perform more complex calculations.
Example: Calculating Quotient and Remainder
a = 10
b = 3
quotient = a // b
remainder = a % b
print("Quotient:", quotient) # Output: Quotient: 3
print("Remainder:", remainder) # Output: Remainder: 1
Example: Using Division in a Complex Expression
x = 20
y = 6
z = 4
result = (x / y) + (x // y) - (x % y) * z
print(result) # Output: 6.333333333333333
Related Articles
Trigonometric functions Python Numpy
Python convert dataframe to numpy array
Convert binary to decimal in Python
Multiply all elements in list Python
Multiply each element of a list by a number in Python
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Python Pandas Plotting
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python take screenshot of specific window
Read data from excel file using Python Pandas
Quick Introduction to Python Pandas
Python requests GET method
Python Convert XML to CSV
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Bouncing ball game code in Python
Remove character from string Python
Python raise keyword