# Operators in Python

An operator is a symbol that tells the Python interpreter to perform specific arithmetic or logical operations on variables and values.

For example –

```# Here + and = are examples of operators in Python
sum = 5+6
print(sum)```

Python has the following type of built-in operators.

• Arithmetic operators
• Relational operators
• Logical operators
• Bitwise operators
• Assignment operators
• Python special operators

## Arithmetic operators

These operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, modulus, etc. The list of arithmetic operators used in Python is given below.

OperatorDescriptionUsage
`+`Addition`a+b`
`-`Subtraction`a-b`
`*`Multiplication`a*b`
`/`Division`a/b`
`%`Modulus`a%b`
`**`Exponentiation`a**b`
`//`Floor division`a//b`

#### Floor division –

This can be new to many of you when you use `//` operator it performs floor division. The division which results into the whole number is known as floor division. You can differentiate between normal and floor by understanding the given example.

```x=18
y=5
# Normal division
print(f"x/y is {x/y}")
# Floor division
print(f"x//y is {x//y}")```

You can see the output of this code in the given image –

### Example of using arithmetic operators –

```a=10
b=4
print(f"Addition of a and b is {a+b}")
print(f"Subtraction of a and b is {a-b}")
print(f"Multiplication of a and b is {a*b}")
print(f"Division of a and b is {a/b}")
print(f"The remainder when a is divided by b is {a%b}")
print(f"a to the power b is {a**b}")
print(f"Floor division of a and b is {a//b}")```

Execute this code on your own system to see the output.

## Relational operators

A relational or comparison operator is used to compare two values. The following table shows the list of relational operators used in Python.

OperatorDescriptionUsage
`>`Greater than`a>b `
`<`Less than`a<b`
`==`Equal (Check if two values equal or not)`a==b`
`>=`Greater than or equal to`a>=b`
`<=`Less than or equal to`a<=b`
`!=`Not equal`a!=b`

It compares two values if the condition goes true it returns `True` otherwise, it returns `False`.

For example –

```a=4
b=5
#Since a is not greater than b this will return False
print("a >b is", a>b)
```

See the output in the image below – Similarly, you can use other comparison operators in Python.

## Logical operators

Logical operators in Python are used to combine two conditional statements. The list of logical operators is given in the table below.

OperatorDescriptionExample
`and`Returns True if both the conditional statements are true`a>10 and b<7`
`or`Returns True if both the conditional statements are true`a>5 or b>2`
`not`It returns False if condition is True and vice versa`not(a>5 or b<2)`

For example –

```a=4
b=5
# Here both the statements are true the and operation will return True
print("a<10 and b>4 is",a<10 and b>4 )```

You can see the output in the given image – ## Bitwise operators

The bitwise operators work on bits these perform operation bit by bit. The following table shows the list of bitwise operators that are used in Python.

OperatorDescriptionUsage
`&`AND`a&b`
`|`OR`a|b`
`~`NOT`~a`
`^`XOR`a^b`
`>>`Right Shift`a>>2`
`<<`Left Shift`a<<2`

For example-

Lets a=4 and b=5  that means in 4-bit binary a will be 0100 and b will be 0101

```a          b          a&b

0          0           0

1          1           1

0          0           0

0          1           0```

After performing bitwise and operation i.e. a&b will be 4 you can see the output in the given image- ## Assignment operators

Assignment operators are used to assigning a value to a variable. Equal to i.e. `=` is a simple assignment operator we can combine this with other types of operators to calculate and assign a value to a variable. For example, `+ =` will add and assign the calculated value to the variable.

The given table shows the list of assignment operators used in Python.

OperatorsExampleExample is same asDescription
`=``x=10``x=10`Assign 10 to variable x
`+=``x+=10``x=x+10`Add and assign to variable x
`-=``x-=10``x=x-10`Subtract and assign to variable x
`*=``x*=10``x=x*10`Multiply and assign to variable x
`/=``x/=10``x=x/10`Divide and assign to variable x
`%=``x%=10``x=x%10`Find remainder and assign to x
`//=``x//=10``x=x//10`Divide and assign a whole number to x (remove fractional part)
`**=``x**=10``x=x**10`Find x to the power 10 and assign to the variable x
`&=``x&=10``x=x&10`Perform bitwise AND operation and assign the value to variable x
`|=``x|=10``x=x|10`Perform bitwise OR operation and assign the value to variable x
`^=``x^=10``x=x^10`Perform bitwise XOR operation and assign the value to variable x
`>>=``x>>=10``x=x>>10`Shift value 10 bit right and assign it to variable x
`<<=``x<<10``x=x<<10`Shift value 10 bit left and assign it to variable x

## Python special operators

Python has some special operators i.e. identity and membership operators.

### Identity operators

Identity operators check if the two values or variables are equal and at the same location in memory. There are two identity operators, you can see them in the given table.

OperatorDescriptionUsage
`is`Return True if both the values are identicala is b
`is not`Return True if both the values are not identicala is not b

For example –

```a=5
b=5
c=[6,7,8]
d=c
# b is identical to a so the given statement will be True
print("b is a is",b is a)
# c is identical to d so the given statement will be False
print("c is not d", c is not d)```

You can see the output in the image below – ### Membership operators

You can use membership operators to test whether a value or variable is in the sequence or not. You can see membership operators in the given table.

OperatorDescriptionUsage
`in`Return True if value found in the sequencea in b
`in not`Returns True if value not found in the sequencea not in b
```a="Hello World!"
b="World"
c=[6,7,8]
d=
# b is in a so the given statement will be True
print("b in a is",b in a)
# d is not in c so the given statement will also return True
print("d not in c", d not in c)```

You can see the output in the given image – ## Operator precedence and associativity

An expression in programming is the combination of operators and operands. The 5+2*3 is an example of a valid Python expression. Python interpreter performs the calculation on the basis of operator precedence and associativity.

For example in the expression 5+2*3 Python interpreter will first perform multiplication and later it will perform addition. That means * operator has higher precedence over +.

The following table shows the operator precedence and associativity. It is in descending order that means upper groups have higher precedence than lower ones.

OperatorsDescription
`()`Parentheses
`**`Exponentiation(raise to the power)
`+`, `-`, `~`Unary plus, Unary minus, Bitwise NOT
`*`, `/`,`//`,`%`Multiplication, Division, Floor division, Modulus
`+`, `-`Addition, Subtraction
`<<`,`>>`Left and right shift
`&`Bitwise AND
`^`Bitwise XOR
`|`Bitwise OR
`==`,` !=`, `>`,` >=`, `<`,` <=`, `is`,` is not`, `in`,` not in`Comparisons, Identity, Membership operators
`not`Logical NOT
`and`Logical AND
`or`Logical OR

Now which operator will be calculated first if they fall under the same group? Here associativity helps to determine the order of operations. So almost all the groups have left to right associativity that means the operator on the left side will be calculated first.

## Conclusion

For better understanding try to evaluate Python expressions on your own and then try to find them with code. Now If you find any difficulty then write us in the comments below.

Previous Keywords In Python #### Python List vs Array

Like in many other programming languages Python also provides some inbuilt data types this includes list, tuple, sets, dictionaries, etc. In this article, we will discuss the similarities and differences

#### Escape sequences in Python

An escape sequence or escape character allows you to insert a special character in a string. For example, you can insert a new line in a string using \n character.

#### Python if else and nested if else statements

In programming conditional statements provides a way in which the computer takes a decision based on whether a condition is true or false. The if and else statements are common