 Basic Arithmetic Operations in Python3

Note: In Python language, we use # for writing comments in the code:

Now, let us check the syntax for a few basic arithmetics operations in Python:

Program starts

print(3+4)

#or

c= 3 + 4
print(c)

#or

variable1 = 4
variable2=3
sum_value = variable1 + variable2
print(sum_value)

output

7
7
7

Subtraction>>

print(7-4)

print(7-3)

Output
3
4
Multiplication >>

print(4*2)
print(2*3)

Output
8
6
Division >>

print(4/2) # returns a value 4 divided by 2 i.e ans=2

print(21/4) # returns a decimal value of the resultant answer i.e ans=5.25

print(21//4) # returns the integer part of the answer i.e ans=5

print(21%4) # returns the remainder after division i.e ans =1

Output
2
5.25
5
1
Power>>

print(2**4) # results the value of 2^4 i.e ans=16.

# there is another method of computing the power of a number by importing math library (math.pow(x,y)). The use of libraries and functions are discussed in further articles.

Output
16
BODMAS rule:

print((8+4)*3/6) # to compute such questions, Python uses BODMAS rule meaning Brackets >> Of >>Division >> Multiplication >> Addition >>Subtraction.
# Hence here, (8+4) is computed first since its inside brackets. Later 3/6 is calculated and later both calculated components are multiplied.

Output
6

Relevant exercises

POST A NEW COMMENT

• Input (stdin)

Output (stdout)

Input (stdin)

Expected Output

Compiler Message

Input (stdin)

2    3