# Operators in Python | Arithmetic, Example

Alike other programming languages such as C++ or Java, Python also offers a rich set of many operators for working with and comparing types of data or information.

The definition of an operator is simply a special symbol that tells the interpreter to perform a specific operation or action, usually on numbers (operands).

For example, when we add two values, e.g. (10 + 20 = 30). Here, 10 and 20 are numbers also called operands. The plus (+) sign is an arithmetic operator and (10 + 20) is a numeric expression.

The operand in the expression can be a literal, variable, or any expression that has to be calculated.

When the operand and operator combine to perform a certain operation, it forms an expression that is evaluated according to operators.

In Python, an expression is a statement that consists of any combination of operators, constants, variables, or functions.

For example, a simplest expression is x + y. Here, x and y are the variables and the plus (+) sign is an operator. When this expression will evaluate by the Python interpreter, it produces a single value.

Programmers extensively use operators in Python program to perform some sort of calculation, comparison, or assignment on one or more values.

For example, some common calculations might be finding the sum of two or more numbers, dividing two numbers or combining two strings, etc.

Moreover, an operator provides a shortcut to reduce the length of code so that we need to type less.

## Types of Operators in Python

Python language supports the following operators that are as follows:

In Python, we can further classify the operators into two groups. They are:

• Unary operators
• Binary operators

Unary operators are those that require only a single operand to operate upon. Some of the unary operators used in Python are:

• + Unary plus
• – Unary minus
• ~ Bitwise NOT

Binary operators are those that require two operands to operate upon. Some of the binary operators used in Python are:

• Arithmetic operators
• Relational operators
• Assignment operators, etc.

Let us have a look on the first arithmetic operators and, in the further tutorials, we will discuss remaining operators one by one.

## Arithmetic (Mathematical) Operators in Python

Operators that are used to perform the most common mathematical calculations or arithmetic operations are called arithmetic operators in Python.

All arithmetic operators are binary operators because they do operations on two operands (two or more values). Python supports seven types of arithmetic operators. They are:

• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulus (%)
• Floor division (//)
• Exponential (**)

These operators are also called mathematical operators. Let’s have a look at all Python arithmetic operators one by one with examples.

This operator is the most commonly used operator in Python. We can use it to add two or more numbers of any type, or to join two objects when applied to two strings, lists, tuples, or dictionaries.

Let’s write a simple Python program to add two numbers using (+) operator and display its result.

```# Declare variables x and y with values.
x = 20
y = 30
sum = x + y # Adding two numbers with + operator.
print("Sum: ", sum) # Displaying the result.
```
```Output:
Sum: 50
```

In this example, we have calculated the sum of two numbers and stored the result into a variable sum.

### Type Conversion in Addition Calculation

Python automatically performs type conversion when working with mathematical operators. When we use addition or other mathematical operators, the Python interpreter automatically converts different values, such as an integer (a non-decimal numeric value) and float (a decimal numeric value) to appropriate type. For example:

```x = 20 # an integer value.
y = 30.60 # a float value.
sum = x + y # Adding two numbers with + operator.
print("Sum of two numbers: ", sum) # Displaying the result.
```
```Output:
Sum of two numbers:  50.6
```

As you observe the result, Python added two numbers (integer and float) together and gave back the result in float type.

### String Concatenation using (+) Operator

The plus sign operator (+) also performs string concatenation. It joins two or more strings into a single string. For example, the expression “Python” + “ Language” gives the result “Python Language” string.

We can concatenate (or join) any number of strings or literals together using the plus sign (+) operator. Consider the following example code where we will concatenate two strings using + operator.

```firstName = "John"
lastName = "Michael"
fullName = firstName + lastName # Joining two strings with + operator.
print("Full name: ", fullName) # Displaying the result.
```
```Output:
Full name: John Michael
```

### List Concatenation using (+) operator

The plus sign (+) operator also allows us to concatenate two or more lists. Let’s take an example on it.

```x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
result = x + y # Adding the lists x and y.
print(result)
```
```Output:
[1, 2, 3, 4, 5, 6, 7, 8]
```

Q. Is the code have any error? If not, what will be the output?

```x = ('Hello', 20)
y = (20.50, 'Hi')
z = (20, 89)
result = x + y + z
print(result)
```

### Subtraction Operator (-)

The minus operator (-) subtracts one number from another. That is, it subtracts the right value from the left. For example:

```x = 20
y = 30.60
sub = x - y
print("Subtraction: ", +sub)
```
```Output:
Subtraction:  -10.600000000000001
```

In the above code, the third statement subtracts 30.60 from 20 and the result -30 stored into the variable sub.

### Multiplication Operator (*)

The multiplication operator multiplies the values on either side of the operator. When the values on either side of the expression are integer or float numbers, they multiplied together. For example:

```x = 20
y = 20.20
result = x * y # Two variables multiplied using multiplication operator.
print("Multiplication: ", result)
```
```Output:
Multiplication:  404.0
```

When we apply the multiplication operator (*) to objects like a string, a list, a tuple or a dictionary, it repeats the object a number of times pursuant to the number mentioned after the * operator. Consider the below example code.

```text = 'Hello'
y = 2
result = text * y
print("Result: ", result)
```
```Output:
Result:  HelloHello
```

Try It Yourself

```list = ['Python', 'Programming']
y = 2
result = list * y
print("Result: ", result)
```

### Division Operator (/)

The division operator divides the left value by right. The value to the left side is a dividend and the value to the right side is a divisor. For example, the code 6/2 means 6 divided by 2 and returns the result of 3.0.

The division operator result is always float value in Python. Right side operand cannot be zero. For example:

```x = 5 # Integer value.
y = 5 # Integer value
z = x / y
print("Division: ", z)

x = 2.4 # float value.
y = 1.2 # float value.
z = x / y
print("Division: ", z)
```
```Output:
Division:  1.0
Division:  2.0
```

### Modulus Operator (%)

Modulus operator divides the left-hand operand by the right-hand operand and returns the remainder. The sign of the result will be the sign of quotient. The following example code is based on the modulus operator.

```num1 = 23
num2 = 5
num = num1 % num2
print("Remainder after division: ", num)

x = 2.6
y = 1.2
z = x % y
print("Remainder after division: ", z)
```
```Output:
Remainder after division:  3
Remainder after division:  0.20000000000000018
```

### Exponential Operator (**)

This operator is the most powerful operator in Python. It is used to find the power of a value written on the left side of the operator. The left side value raised to the power of the right. For example:

```num1 = 2
num2 = 4
exp = num1 ** num2
print("Result: ", exp)

x = 1.2
y = 3
exp = x ** y
print("Result: ", exp)
```
```Output:
Result: 16
Result: 1.7279999999999998
```

### Floor Division (//)

This operator returns the integer values obtained after applying the division operator. We mostly used it when we want to skip the value after the decimal part. For example:

```num1 = 5
num2 = 2
num = num1 // num2
print("Result: ", num)

x = 12.2
y = 3.1
z = x // y
print("Result: ", z)
```
```Output:
Result:  2
Result:  3.0
```

Let’s write a Python program in which we will perform all mathematical or arithmetic operations by using all Python mathematical operators. Look at the following script code.

```num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

print("Addition (num1 + num2): ", (num1 + num2))
print("Subtraction (num1 - num2): ", (num1 - num2))
print("Multiplication (num1 * num2): ", (num1 * num2))

print("Division (num1 / num2): ", (num1 / num2))
print("Floor division (num1 // num2): ", (num1 // num2))

print("Modulus (num1 % num2): ", (num1 % num2))
print("Exponential (num1 ** num2): ", (num1 ** num2))
```
```Output:
Enter the first number: 5
Enter the second number: 2