Home » Arithmetic operators in Python

Arithmetic operators in Python

Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer I [1Z0-815] Practice Tests
1 Year Subscription
Java SE 11 Programmer II [1Z0-816] Practice Tests
Spring Framework Basics Video Course
Oracle Java Certification

Python supports the following types of operators.

Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators

In this article we will look at Arithmetic operators, here are the supported operators

Arithmetic Operators

 

Operator Name Example
+ Addition a + b
Subtraction a – b
* Multiplication a * b
/ Division b / a
% Modulus b % a
** Exponent a**b
// Floor division a // b

Now for a practical example

Type in the following and save it as math.py

#!/usr/bin/python
​
a = 10
b = 2
c = 0
​
c = a + b
print ("Addition - Value of c is ", c)
​
c = a - b
print ("Subtraction - Value of c is ", c)
​
c = a * b
print ("Multiplication - Value of c is ", c)
​
c = a / b
print ("Division - Value of c is ", c)
​
c = a % b
print ("Modulus- Value of c is ", c)
​
a = 4
b = 5
c = a**b
print ("Exponent- Value of c is ", c)
​
a = 10
b = 5
c = a//b
print ("Floor Division - Value of c is ", c)

Now open up a command prompt and type in python math.py

All going well you should see something like this

math output

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More