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. By IncludeHelp Last updated : December 18, 2023

Python Arithmetic Operators

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

7 Different Python Arithmetic Operators

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

Python Arithmetic Operators: Examples

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 with 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]

Python Tutorial

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.