# Python Operators Tutorial

In this tutorial we learn about special operational keywords in Python. We cover arithmetic, comparison, logical, assignment, bitwise and special operators.

Here's a table of contents of what you'll learn in this lesson:

(*click on a link to skip to its section*)

Let's jump right in.

## What is an operator?

Operators are special symbols or keywords in Python that perform various operations on data containers and their values.

As we know, the value or data container that is operated on, is known as an *operand*.

**Example:**arithmetic operation

` print(3 + 4)`

In the example above we perform a simple arithmetic operation where 3 and 4 are the operands and + is the operator.

In Python the operators are divided into the following groups:

- Arithmetic operators
- Assignment operators
- Comparison (Relational) operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators

## Arithmetic Operators

Arithmetic operators in Python are used with numerical values to perform the most common mathematical operations.

The following table lists the Arithmetic Operators available to us in Python:

Operator | Name |
---|---|

+ | Addition |

- | Subtraction |

* | Multiplication |

/ | Division |

% | Modulus (remainder of division) |

** | Exponent (to the power of) |

// | Floor Division |

**Example:**arithmetic operators in Python

```
# Addition
print("3 + 4 =", 3 + 4)
# Subtraction
print("7 - 3 =", 7 - 3)
# Multiplication
print("3 * 4 =", 3 * 4)
# Division (Always results in a float)
print("40 / 30 =", 40 / 30)
# Modulus (Remainder of division)
print("Remainder of 27 / 2 =", 27 % 2)
# Exponent (To the power of)
print("5 raised to the power of 2 = ", 5 ** 2)
# Floor division (Floor the result)
print("Result of 15 / 2, floored to int =", 15 // 2)
```

## Assignment Operators

Assignment operators in Python are used to assign values to data containers, as well as do some mathematical operations.

The following table lists the Assignment Operators available to us in Python:

Operator | Name | Example | Same As |
---|---|---|---|

= | Assignment | x = 7 | x = 7 |

+= | Addition & assign | x += 3 | x = x + 3 |

-= | Subtraction & assign | x -= 3 | x = x - 3 |

*= | Multiplication & assign | x *= 3 | x = x * 3 |

/= | Division & assign | x /= 3 | x = x / 3 |

%= | Modulus & assign | x %= 3 | x = x % 3 |

//= | Floor division & assign | //x = 3 | x = x // 3 |

**= | Exponent & assign | **x = 3 | x = x ** 3 |

**Example:**assignment operators in Python

```
x = 7
print("Assign:", x)
x += 3
print("Add & Assign:", x)
x -= 3
print("Subtract & Assign:", x)
x /= 2
print("Divide & Assign:", x)
x *= 5
print("Multiply & Assign:", x)
x %= 2
print("Remainder of division & Assign:", x)
x **= 6
print("Raise to power of & Assign:", x)
x //= 2
print("Divide, Floor & Assign:", x)
```

## Comparison (Relational) Operators

Comparison operators in Python are used to compare the value of two operands and returns a boolean true or false result.

The following table lists the Comparison Operators available to us in Python:

Operator | Description |
---|---|

== | If the values of the two operands match, the condition is true. |

!= | If the values of the two operands do not match, the condition becomes true. |

< | If the first operand is less than the second operand, the condition becomes true. |

> | If the first operand is greater than the second operand, the condition becomes true. |

<= | If the first operand is less than or matches the second operand, the condition becomes true. |

>= | If the first operand is greater than or matches the second operand, the condition becomes true. |

**Example:**conditional operators in Python

```
# Equal
print("1 == 1:", 1 == 1)
# Not equal
print("1 != 2:", 1 != 2)
# Greater than
print("2 > 1:", 2 > 1)
# Less than
print("1 < 2:", 1 < 2)
# Greater than or equal to
print("1 >= 1:", 1 >= 1)
# Less than or equal to
print("1 <= 1:", 1 <= 1)
```

## Logical Operators

Logical operators in Python are keywords used to combine conditional statements and are primarily used in expression evaluation to make a decision.

The following table lists the Logical Operators available to us in Python:

Operator | Description |
---|---|

and | If one expression and another expression evaluate to true, then this condition will be true. |

or | If one expression or another expression evaluate to true, then this condition will be true. |

not | Reverses result. If result is true, it will return false. If the result is false, it will return true. |

**Example:**logical operators in Python

```
t = True
f = False
# are t and f both
print("t and f = ", t and f)
# is t or f
print("t or f =", t or f)
# what is not t
print("not t =", not t)
# what is not f
print("not f =", not f)
```

## Identity Operators

Identity operators in Python are keywords used to compare if multiple objects share the same memory location. Two objects that are equal, does not imply that they are identical.

The following table lists the Identity Operators available to us in Python:

Operator | Description |
---|---|

is | If both operands are the same object |

is not | If both operands are not the same object |

**Example:**identity operators in Python

```
# ints, same object type
num1 = 3
num2 = 4
num3 = 4
# 3 != 4, objects don't share memory
print(num1 is num2)
# 4 == 4, objects share memory
print(num2 is num3)
# strings, same object type
str1 = "Hello"
str2 = "Hello"
str3 = "World"
# Hello == Hello, objects share memory
print(str1 is not str2)
# Hello != World, objects don't share memory
print(str2 is not str3)
# Different object types, cannot share memory
print(num1 is str1)
print(num1 is not str1)
```

## Membership Operators

Membership operators in Python are keywords that test if an item exists in a sequence, or collection. Membership operators are used with the collections string, list, tuple, set and dictionary.

The following table lists the Membership Operators available to us in Python:

Operator | Description |
---|---|

in | Does the item exist in the sequence. |

not in | Does the item not exist in the sequence. |

**Example:**membership operators in Python

```
message = "Hello World"
# Letter exists in string
print("W" in message)
# Letter does not exist in string
print("C" not in message)
```

## Bitwise Operators

Bitwise operators in Python performs bit by bit operations on operands as if they were binary digits.

The following table lists the Membership Operators available to us in Python:

Operator | Name | Description |
---|---|---|

& | Binary AND | If the bit exists in both operands, copy it to the result. |

| | Binary OR | If the bit exists in either operands, copy it to the result. |

^ | Binary XOR | If the bit exists in one, but not both operands, copy it to the result. |

~ | Binary NOT | Unary, flips bits in the result. |

>> | Binary right shift | Left operand's bit value is moved left by the number of bits specified by the right operand. |

<< | Binary left shift | Left operand's bit value is moved right by the number of bits specified by the right operand. |

### Summary: Points to remember

- Arithmetic operators, like
**+**or**-**, perform common mathematical functions. - Assignment operators, like
**=**or**+=**, assign values and perform some mathematical operations. - Comparison operators, like
**!=**or**>**, compare a condition between two or more operands and returns a result of true or false. - Logical operators, like
**and**, combine conditional statements. - Identity operators, like
**is**, compare multiple objects that share the same memory location. - Membership operators, like
**in**, evaluate if an item exists in a sequence, or collection. - Bitwise operators, like
**>>**, performs bit by bit operations.