sec01 - Operators used in arithmetic operations and calculations

In Python, various operators are available, allowing you to write complex calculations easily. In this lecture, we will introduce frequently used operators for calculations.

Addition, subtraction, multiplication, division, remainder, floor division, and exponentiation

This article explains how to perform addition, subtraction, multiplication, division, remainder, floor division, and exponentiation in Python.
(Feel free to create new files and test the code to see how it behaves.)

Addition, subtraction, multiplication, and division

The operators used for addition, subtraction, multiplication, and division are +(plus), -(hyphen), *(asterisk), and /(slash). Try changing the combinations of values in the following code expressions and observe the results. (Try combinations of values such as “integers,” “floating point numbers,” “integers and floating point numbers,” and “positive and negative numbers.”)

result1 = 5 + 2.4   # Addition: 7.4
result2 = 5 - 2.4   # Subtraction: 2.6
result3 = 5 * 2.1   # Multiplication: 10.5
result4 = 5.5 / 2.5   # Division: 2.2

print('Addition:', result1)
print('Subtraction:', result2)
print('Multiplication:', result3)
print('Division:', result4)
Tips for printing output

Enter the values separated by commas inside the parentheses of “print()”. This will output the values separated by single spaces.

In the example code above, “print(‘Addition:’, result1)” is written. When this code is executed, the text “Addition:” and the value of the “result1” variable will be output as concatenated information with a single space between them.

Example output to TERMINAL

Remainder 

In Python, you can output the remainder value of a division. For example, “5/2” becomes “2 remainder 1.” In this case, if you use the operator used for remainders, Python returns “1”.

The operator used for the remainder is “%” (percent).

result1 = 5 % 1   # 5...0 -> 0
result2 = 5 % 2   # 2...1 -> 1
result3 = 5 % 3   # 1...2 -> 2
result4 = 5 % 4   # 1...1 -> 1
result5 = 5 % 5   # 1...0 -> 0
result6 = 5 % 6   # 0...5 -> 5

print('5 % 1 =', result1)
print('5 % 2 =', result2)
print('5 % 3 =', result3)
print('5 % 4 =', result4)
print('5 % 5 =', result5)
print('5 % 6 =', result6)

Floor division

Floor division returns the closest integer value in the negative infinity direction when the result of division is not an integer. For example, if the result of division is “2.5,” ‘2’ is returned. Since values are floor-rounded toward negative infinity, the closest integer value less than “2.5” is returned. If the calculation result is a negative value (e.g., “-2.5”), the nearest integer value smaller than “-2.5” (“-3”) is returned.

(Note: Please note that this is not rounding, but floor rounding. Also, the type obtained in the result will be float if either the divisor or the dividend is a floating point number, and int if both are integers.)

The operator used for floor division is “//” (two slashes).

result1 = 10 // 4   # 2.5 -> 2
result2 = -10 // 4   # -2.5 -> -3
print('10 // 4 =', result1)
print('-10 // 4 =', result2)

Exponentiation

Exponentiation (also known as “power”) is an operation that involves multiplying a number repeatedly. For example, “2 to the power of 3” is calculated as “2 * 2 * 2.”

The operator used for exponentiation is “**” (two asterisks).

result1 = 2 ** 2   # 2 * 2     = 4
result2 = 2 ** 3   # 2 * 2 * 2 = 8
result3 = 3 ** 2   # 3 * 3     = 9
result4 = 3 ** 3   # 3 * 3 * 3 = 27
print('2 ** 2 =', result1)
print('2 ** 3 =', result2)
print('3 ** 2 =', result3)
print('3 ** 3 =', result4)
おすすめの記事