Nearby lessons
30 of 159Python - Arithmetic Operators
- 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
Example - Addition with Float Values
Addition with Strings
The + operator can also join (concatenate) two strings.
Example - String Concatenation
Important Note
For string concatenation, both operands must be strings.
Example - Invalid String Addition
2. Subtraction Operator (-)
The subtraction operator is used to subtract one value from another.
Example - Subtraction
Example - Subtraction with Float Values
Important Note
The subtraction operator cannot be used with strings.
Example - Invalid String Subtraction
3. Multiplication Operator (*)
The multiplication operator is used to multiply two values.
Example - Multiplication
Example - Multiplication with Float
String Repetition Using *
The * operator can repeat a string multiple times.
Example - String Repetition
Important Note
For string repetition, one operand must be an integer.
Example - Invalid String Multiplication
4. Division Operator (/)
The division operator divides one value by another.
Important Rule: The division operator always returns a floating-point value.
Example - Division
Example - Division with Decimal Result
Division by Zero
Dividing a number by zero causes an error.
Example - Division by Zero
5. Floor Division Operator (//)
The floor division operator returns only the floor (quotient) value.
The decimal part is removed.
Example - Floor Division
Example - Floor Division with Float
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
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
7. Exponent Operator (**)
The exponent operator is used to calculate the power of a number.
Example - Exponent
Explanation
2 ** 3
means
2 × 2 × 2 = 8
Example - Another Exponent
Operator Precedence
Python follows operator precedence while evaluating arithmetic expressions.
Example - Operator Precedence
Explanation
First:
2 * 3 = 6
Then:
10 + 6 = 16
Using Parentheses
Parentheses change the order of execution.
Example - Parentheses
Arithmetic Operators with Variables
Real World Usage
Arithmetic operators are commonly used in:
- Billing systems
- Banking applications
- Marks calculation
- Scientific calculations
- E-commerce applications
- 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