Nearby lessons

30 of 159

Python - Arithmetic Operators

📌 What You Will Learn
  • Identify the seven arithmetic operators in Python
  • Use +, -, *, and / on numbers
  • Use % for remainder and // for floor division
  • Calculate powers with the ** operator
  • Concatenate and repeat strings with + and *
  • Apply operator precedence and parentheses in expressions

Introduction

Arithmetic operators are used to perform mathematical calculations in Python.

These operators work with numbers and, in some cases, with strings.

Arithmetic Operators List

Operator Meaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
//Floor Division
**Exponent

1. Addition Operator (+)

The addition operator is used to add two values.

Example - Addition

🐍Code Cell
1a = 10
2b = 20
3 
4print(a + b)
Output
30

Example - Addition with Float Values

🐍Code Cell
1print(10.5 + 20.5)
Output
31.0

Addition with Strings

The + operator can also join (concatenate) two strings.

Example - String Concatenation

🐍Code Cell
1print("Hello" + "Python")
Output
HelloPython

Important Note

For string concatenation, both operands must be strings.

Example - Invalid String Addition

🐍Code Cell
1print("Hello" + 10)
Output
TypeError

2. Subtraction Operator (-)

The subtraction operator is used to subtract one value from another.

Example - Subtraction

🐍Code Cell
1print(20 - 10)
Output
10

Example - Subtraction with Float Values

🐍Code Cell
1print(20.5 - 10.2)
Output
10.3

Important Note

The subtraction operator cannot be used with strings.

Example - Invalid String Subtraction

🐍Code Cell
1print("Hello" - "Python")
Output
TypeError

3. Multiplication Operator (*)

The multiplication operator is used to multiply two values.

Example - Multiplication

🐍Code Cell
1print(10 * 5)
Output
50

Example - Multiplication with Float

🐍Code Cell
1print(10.5 * 2)
Output
21.0

String Repetition Using *

The * operator can repeat a string multiple times.

Example - String Repetition

🐍Code Cell
1print("Python" * 3)
Output
PythonPythonPython

Important Note

For string repetition, one operand must be an integer.

Example - Invalid String Multiplication

🐍Code Cell
1print("Python" * "3")
Output
TypeError

4. Division Operator (/)

The division operator divides one value by another.

Important Rule: The division operator always returns a floating-point value.

Example - Division

🐍Code Cell
1print(10 / 2)
Output
5.0

Example - Division with Decimal Result

🐍Code Cell
1print(10 / 3)
Output
3.3333333333333335

Division by Zero

Dividing a number by zero causes an error.

Example - Division by Zero

🐍Code Cell
1print(10 / 0)
Output
ZeroDivisionError

5. Floor Division Operator (//)

The floor division operator returns only the floor (quotient) value.

The decimal part is removed.

Example - Floor Division

🐍Code Cell
1print(10 // 2)
2print(10 // 3)
Output
5
3

Example - Floor Division with Float

🐍Code Cell
1print(10.5 // 2)
Output
5.0

Difference Between / and //

/ //
Returns a floating-point result. Returns the floor value.
Example: 10 / 3 = 3.3333... Example: 10 // 3 = 3

6. Modulus Operator (%)

The modulus operator returns the remainder after division.

Example - Modulus

🐍Code Cell
1print(10 % 2)
2print(10 % 3)
Output
0
1

Explanation

Expression Result
10 % 2 0
10 % 3 1

Usage of Modulus Operator

The modulus operator is commonly used to:

  • Check even numbers.
  • Check odd numbers.
  • Check divisibility.

Example - Check Even Number

🐍Code Cell
1num = 10
2 
3print(num % 2 == 0)
Output
True

7. Exponent Operator (**)

The exponent operator is used to calculate the power of a number.

Example - Exponent

🐍Code Cell
1print(2 ** 3)
Output
8

Explanation

2 ** 3

means

2 × 2 × 2 = 8
        

Example - Another Exponent

🐍Code Cell
1print(5 ** 2)
Output
25

Operator Precedence

Python follows operator precedence while evaluating arithmetic expressions.

Example - Operator Precedence

🐍Code Cell
1print(10 + 2 * 3)
Output
16

Explanation

First:

2 * 3 = 6

Then:

10 + 6 = 16
        

Using Parentheses

Parentheses change the order of execution.

Example - Parentheses

🐍Code Cell
1print((10 + 2) * 3)
Output
36

Arithmetic Operators with Variables

🐍Code Cell
1a = 20
2b = 10
3 
4print(a + b)
5print(a - b)
6print(a * b)
7print(a / b)
Output
30
10
200
2.0

Real World Usage

Arithmetic operators are commonly used in:

  • Billing systems
  • Banking applications
  • Marks calculation
  • Scientific calculations
  • E-commerce applications
📝 Key Takeaways
  • The seven arithmetic operators are +, -, *, /, %, //, and **
  • The division operator / always returns a floating-point value
  • Dividing a number by zero raises a ZeroDivisionError
  • The % operator returns the remainder after division
  • The * operator repeats a string, and + concatenates strings
  • Parentheses change the order in which expressions are evaluated

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8