Introduction To Python
The following article provides an outline for Arithmetic Operators in Python. Python is a popular computer programming language developed by Guido van Rossum in 1991 for the purpose of server-side web development, software development, system scripting. It is an interpreted and high-level language.
Key Takeaways
- Arithmetic operators in Python give you the ability to perform various arithmetic operations like addition, subtraction, multiplication, division, modulus, etc.
- In python, these operators are used directly with the object values as like plain mathematical equation.
- If your program deals with complex and lengthy mathematical operations, you can include all formulae in a function and call that function at the proper place.
- Multiplication operator has the highest precedence in Python.
Why Python Is So Popular?
Python is widely popular among computer programmers because of the following reasons:
- Python syntax is similar to the English language so programmers find it easy to develop code.
- Developers can write code in few lines because of easy syntax.
- Python possesses properties of procedure-oriented, object-oriented, and function-oriented approach.
- It works on almost all leading platforms like Windows, Linux, Mac, etc.
- Python is an interpreted language that yields early prototyping.
Operators In Python
Python is a user-friendly language that performs various operations like arithmetic operations, logical operations, bitwise operations, comparison operations, and others.
Python provides the following operators for the different operations:
- Arithmetic Operators
- Logical Operators
- Assignment Operators
- Comparison Operators
- Membership Operators
- Bitwise Operators
- Identity Operators
Here we will elaborate on arithmetic operators in Python.
Arithmetic Operators in Python
There are different arithmetic operations supported by Python.
Python provides capable operators for arithmetic operations:
Imagine two variables:
- x=40
- y=10
Sr. No | Operator | Symbol | Purpose | Example |
1 | Addition | Adds two values. | x y= 50 | |
2 | Subtraction | – | Subtracts the right-hand operand from the left-hand operand. | x-y=30 |
3 | Multiplication | * | Multiplies the two values on either side of the operator. | x*y= 400 |
4 | Division | / | Left-hand operator is divided by the right-hand operand. | x/y=4 |
5 | Modulus | % | Left-hand operator is divided by the right-hand operand and remainder is returned. | x%y=0 |
6 | Floor Division | // | Division of operands in which result is quotient in that digits after decimal sign are omitted. | x//y=4 |
7 | Exponentiation | ** | Raises the first operand to the power of the second. | x**y |
Example Of Different Arithmetic Operators
Given below is the example mentioned:
Code:
# Program shows arithmetic operators in Python
A1=int (input ("Enter the first number:"))
A2=int (input ("Enter the second number:"))
Add=A1+A2
Sub=A1-A2
Mult=A1*A2
Div=A1/A2
F_Div=A1//A2
Exp=A1**A2
Mod=A1%A2
print("Addition is: ", Add)
print("Subtraction is: ", Sub)
print("Multiplication is: ", Mult)
print("Division is: ", Div)
print("Floor Division: ", F_Div)
print("Exponentiation: ", Exp)
print("Modulus operation: ", Mod)
Output:
Operator Precedence
- The operator precedence decides the sequence of operators that need to be evaluated first.
- In normal calculus, multiplication has the highest precedence over addition.
- For example, the answer of 2 5*3 is 17 and not 21.
- If you want to change the precedence, you need to include the parenthesis in the equation like (2 5) *3.
Conclusion – Arithmetic Operators in Python
Like other leading programming languages, Python also provides a set of arithmetic operators that used to perform various operations on values and variables. This facilitates the developers to perform simple and complex mathematical operations.
Recommended Articles
This is a guide to Arithmetic Operators in Python. Here we discuss the introduction, operators in python, example & operator precedence. You can also go through our other suggested articles to learn more –
Are you preparing for the entrance exam ?
Join our Python test series to get more practice in your preparation
View More