Python Arithmetic Operators

Published by StudyMuch on

Python Arithmetic Operators

Python Arithmetic Operators: Explained with Examples

Arithmetic operations are fundamental in programming, and Python provides a rich set of arithmetic operators to perform various mathematical calculations. In this blog post, we will explore Python’s arithmetic operators in detail, including addition, subtraction, multiplication, division, exponentiation, remainder, floor division, and operator precedence.

In Python there are Seven basic Arithmetic Operators;

Operators Description
+ Addition
Subtraction
* Multiplication
/ Division
** Exponentiation
% Remainder
// Floor Division

Python Arithmetic Operators

Addition (+)

The addition operator (+) is used to add two numbers together. It can be used with both integers and floating-point numbers.

x = 5
y = 3
result = x + y
print(result)  # Output: 8

Subtraction (-)

The subtraction operator (-)
is used to subtract one number from another. Like addition, it can be used with integers and floating-point numbers.

a = 10
b = 4
result = a - b
print(result)  # Output: 6

Multiplication (*)

The multiplication operator (*)
is used to multiply two numbers. It works with both integers and floating-point numbers.

p = 7
q = 3
result = p * q
print(result)  # Output: 21

Division (/)

The division operator (/)
is used to divide one number by another. It always returns a floating-point result.

m = 10
n = 2
result = m / n
print(result)  # Output: 5.0

Exponentiation (**)

The exponentiation operator (**) is used to raise a number to a certain power.

base = 2
exponent = 3
result = base ** exponent
print(result)  # Output: 8

Remainder (%)

The remainder operator (%), also known as the modulus operator. It calculates the remainder when one number is divided by another. It’s often used to check if a number is even or odd.

num = 11
remainder = num % 2
print(remainder)  # Output: 1 (since 11 is odd)

Floor Division (//)

The floor division operator (//)
divides one number by another and rounds down to the nearest integer. It’s useful for obtaining the integer quotient of a division operation.

dividend = 10
divisor = 3
result = dividend // divisor
print(result)  # Output: 3

Operator Precedence

Python follows the standard operator precedence rules. This means that some operators are evaluated before others. Here’s a brief overview:

  • Exponentiation () has the highest precedence.
  • Multiplication (*), Division (/), and Floor Division (//) have the same precedence and are evaluated from left to right.
  • Addition (+) and Subtraction (-) also have the same precedence and are evaluated from left to right.

You can use parentheses to change the order of evaluation when needed.

result = (2 + 3) * 4
print(result)  # Output: 20

In this example, the addition inside the parentheses is evaluated first, and then the multiplication is performed.

Python provides a robust set of arithmetic operators for performing mathematical calculations in your programs. Understanding how these operators work and their precedence is essential for writing effective code. Whether you’re working with integers or floating-point numbers, Python’s arithmetic operators have you covered for all your mathematical needs. I hope you have learned this tutorial welled, but if you have any doubt you can ask in the comment section.

Learn More;


1 Comment

tlover tonet · May 6, 2024 at 12:13 am

Great blog! I am loving it!! Will come back again. I am taking your feeds also

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *