Operators in python programming are essential to perform various operations with more than one operands. For example, to perform addition operation between two the values of two variables we use addition operator. Similarly for subtraction we have subtraction operator. Lets know all these operator one by one.
Addition Operator +
Addition operator performs addition operation. The sign of addition operator is +
Example
num1=100
num2=200
print(num1+num2)
Output: 300
Subtraction Operator –
Subtraction operator performs subtraction operation. The sign of subtraction operator is –
Example:
num1=200
num2=100
print(num1-num2)
Output: 100
Multiplication Operator *
Multiplication operator performs multiplication operation. The sign of multiplication operator is *
Example:
num1=10
num2=5
print(num1*num2)
Output: 50
Division Operator /
Division operator performs division operation. The sign of division operator is /
Example:
num1=50
num2=10
print(num1/num2)
Output: 5
Modulus Operator %
Modulus operator performs modulus operation. Modulus operation returns the remainder of division of two values. The sign for modulus operator is %
Example
num1=105
num2=10
print(num1%num2)
Output:5
Note: Addition Operator, Subtraction Operator, Multiplication Operator, Division Operator and Modulus operator are also known as Arithmetic operators.
There are few more and very helpful operators exist in Python programming. They are listed below.
Assignment Operator =
Assignment operator assigns value to the variable. In one of the previous tutorial, I had mentioned about variable declaration. In variable declaration we have learned about assigning values. The sign for assignment operator is =
Example
number=1000
age=26
name=”Srikant”
weight: 76.8
Addition Assignment Operator +=
Addition assignment operator adds the assigned value to the value of the variable. The sign for addition assignment operator is +=
Example
number=1000
Suppose a requirement comes to increase the value of the variable number by 500 and print the new value.
Solution 1
number=number+500
print(number)
Output: 1500
Solution 2
Using addition assignment operator
number+=500
print(number)
Output: 1500
Subtraction Assignment Operator -=
Subtraction assignment operator deducts the assigned value from the value stored in the variable. The sign of subtraction assignment operator is -=
Example
number=1000
Deduct the value of number variable by 500.
Solution
number-=500
print(number)
Output: 500
Multiplication Assignment Operator *=
Multiplication Assignment Operator multiplies the assigned value with the value stored in a variable. The sign of multiplication assignment operator is *=
Example
number=50
Increase the value of number variable by 5 times.
Solution
number*=5
print(number)
Output:250
Division Assignment Operator /=
Division assignment operator divides the stored value in variable by assigned value. The sign of division assignment operator is /=
Example
number = 1000
Print the one fourth value of the variable number
Solution
number/=4
print(number)
Output: 250
Modulus Assignment Operator %=
Modulus assignment operator returns the remainder after the division of the value stored in the variable with the assigned value. The sign of modulus assignment operator is %=
Example
number=1005
Print the remainder after division by 25.
Solution
number%=25
print(number)
Output: 5