Simple Arithmetic
➕➖ Simple Arithmetic in Python
Python provides basic arithmetic operators to perform calculations with numbers. These can be used with integers, floats, and even NumPy arrays for vectorized arithmetic.
✅ 1. Basic Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ |
Addition | 5 + 3 = 8 |
- |
Subtraction | 5 - 3 = 2 |
* |
Multiplication | 5 * 3 = 15 |
/ |
Division (float) | 5 / 2 = 2.5 |
// |
Floor Division | 5 // 2 = 2 |
% |
Modulus (remainder) | 5 % 2 = 1 |
** |
Exponentiation | 5 ** 2 = 25 |
✅ 2. Examples with Numbers
✅ 3. Using Arithmetic with Floats
-
Division
/always returns float -
Floor division
//truncates to integer
✅ 4. Arithmetic with NumPy Arrays
NumPy allows element-wise arithmetic on arrays:
-
Operations are vectorized → very fast
✅ 5. Using Parentheses for Precedence
-
Use parentheses
()to control order of operations
🎯 Practice Exercises
-
Compute
(15 + 5) * 2 - 8 / 4using Python arithmetic operators. -
Create two NumPy arrays
[1,2,3]and[4,5,6]and compute their sum, difference, and product. -
Compute square, cube, and modulus of numbers from 1 to 5 using Python or NumPy.
