|
| 1 | +--- |
| 2 | +id: arithmetic-operation |
| 3 | +slug: /arithmetic-operation |
| 4 | +title: Arithmetic Operations with Basic Data Types |
| 5 | +sidebar_label: Arithmetic Operations |
| 6 | +--- |
| 7 | + |
| 8 | +Arithmetic operators in Python are used to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. |
| 9 | + |
| 10 | +### Task |
| 11 | + |
| 12 | +Create a program that defines two variables of number data types and performs various arithmetic operations on them. The program should perform the following tasks: |
| 13 | + |
| 14 | +1. Add the two numbers and print the sum. |
| 15 | +2. Subtract the first number from the second number and print the difference. |
| 16 | +3. Multiply the two numbers and print the product. |
| 17 | +4. Divide the first number by the second number and print the quotient. |
| 18 | +5. Calculate the remainder when the first number is divided by the second number and print the result. |
| 19 | +6. Raise the first number to the power of the second number and print the result. |
| 20 | +7. Increment the value of the first number by 1 and print the updated value. |
| 21 | +8. Decrement the value of the second number by 1 and print the updated value. |
| 22 | + |
| 23 | +#### JavaScript implementation |
| 24 | +```javascript |
| 25 | +let num1 = 10; |
| 26 | +let num2 = 5; |
| 27 | + |
| 28 | +// 1. Addition |
| 29 | +let sum = num1 + num2; |
| 30 | +console.log("Sum:", sum); |
| 31 | + |
| 32 | +// 2. Subtraction |
| 33 | +let difference = num1 - num2; |
| 34 | +console.log("Difference:", difference); |
| 35 | + |
| 36 | +// 3. Multiplication |
| 37 | +let product = num1 * num2; |
| 38 | +console.log("Product:", product); |
| 39 | + |
| 40 | +// 4. Division |
| 41 | +let quotient = num1 / num2; |
| 42 | +console.log("Quotient:", quotient); |
| 43 | + |
| 44 | +// 5. Remainder |
| 45 | +let remainder = num1 % num2; |
| 46 | +console.log("Remainder:", remainder); |
| 47 | + |
| 48 | +// 6. Exponentiation |
| 49 | +let power = num1 ** num2; |
| 50 | +console.log("Power:", power); |
| 51 | + |
| 52 | +// 7. Increment |
| 53 | +num1++; |
| 54 | +console.log("Updated num1 after increment:", num1); |
| 55 | + |
| 56 | +// 8. Decrement |
| 57 | +num2--; |
| 58 | +console.log("Updated num2 after decrement:", num2); |
| 59 | +``` |
| 60 | + |
| 61 | +#### Python implementation |
| 62 | +```python |
| 63 | +num1 = 10 |
| 64 | +num2 = 5 |
| 65 | + |
| 66 | +# 1. Addition |
| 67 | +sum = num1 + num2 |
| 68 | +print("Sum:", sum) |
| 69 | + |
| 70 | +# 2. Subtraction |
| 71 | +difference = num1 - num2 |
| 72 | +print("Difference:", difference) |
| 73 | + |
| 74 | +# 3. Multiplication |
| 75 | +product = num1 * num2 |
| 76 | +print("Product:", product) |
| 77 | + |
| 78 | +# 4. Division |
| 79 | +quotient = num1 / num2 |
| 80 | +print("Quotient:", quotient) |
| 81 | + |
| 82 | +# 5. Remainder |
| 83 | +remainder = num1 % num2 |
| 84 | +print("Remainder:", remainder) |
| 85 | + |
| 86 | +# 6. Exponentiation |
| 87 | +power = num1 ** num2 |
| 88 | +print("Power:", power) |
| 89 | + |
| 90 | +# 7. Increment |
| 91 | +num1 += 1 |
| 92 | +print("Updated num1 after increment:", num1) |
| 93 | + |
| 94 | +# 8. Decrement |
| 95 | +num2 -= 1 |
| 96 | +print("Updated num2 after decrement:", num2) |
| 97 | +``` |
| 98 | + |
| 99 | +### Code Highlight |
| 100 | +- The syntax for addition, subtraction, multiplication, division, remainder, and exponentiation in Python is the same as in JavaScript. |
| 101 | +- Python does not have the `++` and `--` operators. Instead, `+=` and `-=` are used to increment or decrement variables. |
| 102 | + |
| 103 | +### Difference Quick View |
| 104 | + |
| 105 | +| Feature | JavaScript | Python | |
| 106 | +|---------|------------|--------| |
| 107 | +| Addition | `+` | `+` | |
| 108 | +| Subtraction | `-` | `-` | |
| 109 | +| Multiplication | `*` | `*` | |
| 110 | +| Division | `/` | `/` | |
| 111 | +| Integer Division | - | `//` | |
| 112 | +| Remainder | `%` | `%` | |
| 113 | +| Exponentiation | `**` | `**` | |
| 114 | +| Increment | `++` <br /> `+=1` | `+= 1` | |
| 115 | +| Decrement | `--` <br/> `+=1` | `-= 1` | |
| 116 | + |
| 117 | +:::danger |
| 118 | +In Python, arithmetic operations can only be performed between **compatible** data types. For example, the result of a calculation between a floating-point number and an integer will be a floating-point number. However, performing arithmetic operations between a string and a number will result in an error. To perform arithmetic operations between a number and a string, one of them must be converted to the appropriate data type. |
| 119 | +::: |
| 120 | + |
| 121 | +### Resources |
| 122 | + |
| 123 | +- https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex |
0 commit comments