CodeSutraHub

Arithmetic and Assignment Operators in Python

In Python, arithmetic operators are used to perform mathematical operations on numbers. Using these operators, you can perform actions such as addition, subtraction, multiplication, and division. Assignment operators are used to assign values to variables.

Arithmetic Operators

The main arithmetic operators in Python are:
Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
// Floor Division a // b
% Modulus a % b
** Exponentiation a ** b
Here
Floor Division (//) returns only the integer part of the division result.
For example, in 7 // 3, when 7 is divided by 3, the quotient is 2 and the remainder is 1. After using the // operator, the result will be the quotient (2).

Modulus (%) returns the remainder after division.
For example, in 7 % 3, when 7 is divided by 3, the quotient is 2 and the remainder is 1. After using the % operator, the result will be the remainder (1).

Exponentiation (**) represents raising one number to the power of another. For example, 2 ** 3 means multiplying 2 three times (2 × 2 × 2), which results in 8.

Examples of Arithmetic Operations


Addition (+)
a = 10
b = 3
result = a + b
print("Addition:", result)  # Output: 13

Subtraction (-)
a = 10
b = 3
result = a - b
print("Subtraction:", result)  # Output: 7

Multiplication (*)
a = 10
b = 3
result = a * b
print("Multiplication:", result)  # Output: 30

Division (/)
a = 10
b = 3
result = a / b
print("Division:", result)  # Output: 3.3333333333333335

Note:
The division operator (/) always returns a float value.

Floor Division (//)
a = 10
b = 3
result = a // b
print("Floor Division:", result)  # Output: 3

Note:
Floor division (//) returns the largest integer less than or equal to the result.

Modulus (%)
a = 10
b = 3
result = a % b
print("Modulus:", result)  # Output: 1

Exponentiation (**)
a = 2
b = 3
result = a ** b
print("Exponentiation:", result)  # Output: 8

Assignment Operators in Python

In Python, assignment operators are used to assign values to variables.
Operator Description Example
= Assigns a value to a variable a = 5
+= Adds and assigns value a += 3 (equivalent to a = a + 3)
-= Subtracts and assigns value a -= 2 (equivalent to a = a - 2)
*= Multiplies and assigns value a *= 4 (equivalent to a = a * 4)
/= Divides and assigns value a /= 2 (equivalent to a = a / 2)
//= Floor divides and assigns value a //= 3 (equivalent to a = a // 3)
%= Modulus and assigns value a %= 2 (equivalent to a = a % 2)
**= Exponentiates and assigns value a **= 3 (equivalent to a = a ** 3)

Examples of Assignment Operators


a = 10
print("Initial value of a:", a)  # Output: 10

a += 5
print("After a += 5:", a)  # Output: 15

a -= 3
print("After a -= 3:", a)  # Output: 12

a *= 2
print("After a *= 2:", a)  # Output: 24

a /= 4
print("After a /= 4:", a)  # Output: 6.0

a //= 2
print("After a //= 2:", a)  # Output: 3.0

a %= 2
print("After a %= 2:", a)  # Output: 1.0

a **= 3
print("After a **= 3:", a)  # Output: 1.0

Here is what we learned

  • Arithmetic Operators in Python
  • Assignment Operators in Python
  • Conditional Statements in Python will be covered in the next topic

Watch the following video for Arithmetic and Assignment Operators in Python