How to write basic arithmetic operations in Python

In programming, arithmetic operations are one of the basic process. Numerical calculations are performed in various cases, such as calculating amounts or the number of days.

Basically, all programming languages have methods to perform arithmetic operations, and Python is no exception. It allows you to write code for arithmetic operations.

Basic arithmetic operations are performed as follows:

  • addition with "+"
  • subtraction with "-"
  • multiplication with "*"
  • division with "/"
print(1 + 2)      #output result:3
print(5 - 3)      #output result:2
print(2 * 3)      #output result:6
print(6 / 3)      #output result:2

For division, there are separate methods to obtain the integer part of the result and to get the remainder. To obtain the integer part, use the "//" operator, and to get the remainder, use the "%" operator.

print(9 // 2)      #output result:4
print(9 % 2)       #output result:1

To perform exponentiation, use the "**" operator.

result = 2 ** 3      #output result:8

おすすめの記事