Python Arithmetic Operators

Python | Arithmetic operators: Here, we are going to learn about the various arithmetic operators in Python with their usages, syntaxes, and examples.
Submitted by IncludeHelp, on May 31, 2020

Arithmetic operators are used to perform various arithmetic / mathematical operations and they are binary operators means they require two operands for the calculations.

Following are the arithmetic operators,

Operator Name Descriptions
+ Addition Operator It returns the addition of two expressions.
- Subtraction Operator It returns the subtraction of two expressions.
* Multiplication Operator It returns the product of two expressions.
** Power Operator It returns the value of the expression raised to the given power i.e. it returns the expression1 raised to the power of expression2.
/ Division Operator It returns the quotient of two expressions.
// Floor Division Operator It returns the integral part of the quotient of two expressions.
% Modulus Operator It returns the decimal part of the quotient i.e. remainder of the division of two expressions.

Syntax:

    exp1  +  exp2
    exp1  -  exp2
    exp1  *  exp2
    exp1 **  exp2
    exp1  /  exp2
    exp1 //  exp2
    exp1  %  exp2

Programs to demonstrate the example of arithmetic operators

Example 1:

# Python program to demonstrate 
# the example of arithmetic operators

a = 10
b = 3

# printing the values
print("a:", a)
print("b:", b)

# operations
print("a + b :", a + b)
print("a - b :", a - b)
print("a * b :", a * b)
print("a ** b:", a ** b)
print("a / b :", a / b)
print("a // b:", a // b)
print("a % b :", a % b)

Output:

a: 10
b: 3
a + b : 13
a - b : 7
a * b : 30
a ** b: 1000
a / b : 3.3333333333333335
a // b: 3
a % b : 1

Example 2:

# Python program to demonstrate 
# the example of arithmetic operators

a = 10.10
b = -2.5

# printing the values
print("a:", a)
print("b:", b)

# operations
print("a + b :", a + b)
print("a - b :", a - b)
print("a * b :", a * b)
print("a ** b:", a ** b)
print("a / b :", a / b)

# // operator rounds the result 
# down to the nearest whole number
print("a // b:", a // b)

print("a % b :", a % b)

Output:

a: 10.1
b: -2.5
a + b : 7.6
a - b : 12.6
a * b : -25.25
a ** b: 0.0030845837443758094
a / b : -4.04
a // b: -5.0
a % b : -2.4000000000000004

Example 3:

# Python program to demonstrate 
# the example of arithmetic operators
# Operations on lists, strings, etc

x = "Hello"
y = "World"

# '+', '*' with strings
print("x + y:", x + y)
print("x + y + x:", x + y + x)
print("x * 2:", x * 2)
print("x * 10:", x * 10)
print()

# '+', '*' with lists
x = [10, 20, 30, 40]
y = [40, 30, 20, 10]

print("x + y:", x + y)
print("x + y + x:", x + y + x)
print("x * 2:", x * 2)
print("x * 3:", x * 3)
print()

Output:

x + y: HelloWorld
x + y + x: HelloWorldHello
x * 2: HelloHello
x * 10: HelloHelloHelloHelloHelloHelloHelloHelloHelloHello

x + y: [10, 20, 30, 40, 40, 30, 20, 10]
x + y + x: [10, 20, 30, 40, 40, 30, 20, 10, 10, 20, 30, 40]
x * 2: [10, 20, 30, 40, 10, 20, 30, 40]
x * 3: [10, 20, 30, 40, 10, 20, 30, 40, 10, 20, 30, 40]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.