How to use Python Arithmetic Operators
Python arithmetic operators are used to perform mathematical operations between two numeric values. In this article, we will discuss how to use arithmetic operator in python with the help of examples.
In any programming language, there are different types of operators that are used to perform mathematical, relational, and logical operations.
Explore How to Find an Armstrong Number Using Python
In Python, there are mainly eight different types of operators that are generally used to perform different operations:
- Arithmetic Operator
- Assignment Operator
- Comparison Operator
- Logical Operator
- Membership Operator
- Identity Operators
- Bitwise Operators
- Ternary Operators
Must Check: All about Operators in Python
So, without further delay, letโs learn how to use arithmetic operators in Python.
What is an Arithmetic Operator?
An Arithmetic Operator is a mathematical function that takes two input values, performs calculations, and produces a single result as an output. The most common arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). In Python, there are eight different types of arithmetic operators.
Name | Operator | Example |
Addition | + | x + y |
Subtraction | โ | x โ y |
Multiplication | * | x * y |
Division | / | x / y |
Modulus | % | x % y |
Exponential | ** | x ** y |
Floor Division | // | x // y |
Must Read: Numbers in Python
Arithmetic operators in Python follow a basic order of precedence known as PEMDAS, which stands for
P: Parentheses
E: Exponential
M: Multiplication
D: Division
A: Addition
S: Subtraction
Note:
- Parentheses will be evaluated first, then exponential, and subtraction at the end.
- In case, two operators whose precedence is equal in the expression, then associativity is followed.
- Associativity Rule: It states all the arithmetic operators (except exponential (**)) follow the left-to-right associativity.
Example: Solve (57 + 13 โ 24/3 * 8)
Here, in this case, the precedence of / and * are equal, but also the
precedence (/, *) > precedence (+, -)
=> 24/3 * 8 = 8 * 8 = 64 (since both / and * have the same precedence so follow left to right)
Hence, the next expression becomes: 57 + 13 โ 64
Now, again following left-to-right associativity, we get
70 โ 64 = 6.
Hence, the solution of (57 + 13 โ 24/3 * 8) is 6.
Until now, we clearly understand arithmetic operators and how to use them to solve numerical problem. Now, we will see how to use them in Python.
Addition
The addition operator (+) in python is used to find the sum of two values.
Example: 55 + 45 = 100
Letโs see how it is implemented in python.
Code
# Arithmetic Operator - Addition (+)a = 55b = 45add = a + bprint ('The sum of a and b is:', add)
Output
Subtraction
The subtraction operator (-) in python is used to find the difference between two numeric values.
Example: 55 โ 45 = 10
Letโs see how it is implemented in python.
Code
# Arithmetic Operator - Subtraction (-)a = 55b = 45subtract = a - bprint ('The subtraction of a from b is:', subtract)
Output
Recursion Function in Python | count() Function in Python |
len() Function in Python | float() Function in Python |
range() Function in Python | lambda() Function in Python |
Multiplication
The multiplication operator (*) in python is used to find the product between two numeric values.
Example: 10 * 5 = 50
Letโs see how it is implemented in python.
Code
# Arithmetic Operator - Multiplication (*)a = 10b = 5product = a * bprint('The multiplication of a and b is:', product)
Output
Division
The division operator (/) in python is used to find the quotient when the first value is divided by the second value.
Example: 30 / 7 = 4.28
Letโs see how it is implemented in python.
Code
# Arithmetic Operator - Division (/)a = 30b = 7division = a / bprint('The division of a and b is:', division)
Output
Modulus
The modulus operator (%) in python is similar to the Division operator. But dissimilar to the division operator it returns the remainder when the first value is divided by the second value.
Example: 10 % 3 = 1
Letโs see how it is implemented in python.
Code
# Arithmetic Operator - Modulus (%)a = 10b = 3mod = a % bprint('The modulus value of a and b is:', mod)
Output
Programming Online Courses and Certification | Python Online Courses and Certifications |
Data Science Online Courses and Certifications | Machine Learning Online Courses and Certifications |
Exponential
The exponential operator (**) in python is used to find the power of first value to the second value.
Answer: 2**3 = (2)3 = 2 *2*2 = 8
Letโs see how it is implemented in python.
Code
# Arithmetic Operator - Exponential (**)a = 2b = 3power = a**bprint('The exponential value of a and b is:', power)
Output
Floor Division
The // operator is applied to two numerical values first division will be performed and then the result obtained from the division will be rounded down to the nearest integer.
Example: 30 // 7 = 4
# Arithmetic Operator - Floor Division (//)a = 30b = 7fd = 30//7print('The floor division between a and b is:', fd)
Output
Must Read: Floor Division in Python
Best-suited Python courses for you
Learn Python with these high-rated online courses
Conclusion
In this article, we have briefly discussed how to use arithmetic operator in python with the help of examples.
Hope, you will like the article.
Keep Learning!!
Keep Sharing!!
Top Trending Article
Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python
Interview Questions
Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio