# Python Operators [Everything You Need to Know]

Learning a new syntax or other complex stuff in any programming language is great!

But,

It would become useless if you are not able to solve real world problems.

Because,

The Ultimate goal of any programmer is to solve real world problems.

So,

In this article, You are going to learn about one of the fundamental tool through which you can solve any complicated problem within a second.

And,

The tool which I am talking about are Python Operators.

Along with that, You’ll learn about their types, their syntax and you’ll also learn how to use them with examples.

So, Let’s dive right in!

## What are Operators in Python?

Python Operators are special symbols (characters) used to perform arithmetic or logical manipulations (calculations) on Operands.

Or,

In other words: Python Operators are special symbols which tells the python interpreter to carry out some mathematical or logical operations (calculations) on Operands.

Now, you might be wondering that,

What is an Operand?

An operand is a value or a variable on which the operator performs the operations.

It means:

• Operands may be constants
• Operands may be variables

But,

What are Arithmetic and Logical calculations?

Don’t Worry!

But, Before going into that,

Let’s Understand what actually Operators and Operands are with the help of an example.

Example:

```# addition of two numbers (this complete line is a comment in Python, which will not be executed by the Python interpreter)

3 + 5
```

Or,

```x = 3
y = 5

x + y

```

Output:

```     8
```

In the above example, The symbol (+) is the operator that performs addition, Values 3 and 5 and variables x and y are the operands and 8 is the output ( i.e., result) of the operation .

## Types of Operators in Python

Python language has 7 different types of operators:

• Arithmetic Operators
• Assignment Operators
• Comparison (Relational) Operators
• Logical (Boolean) Operators
• Bitwise (Binary) Operators
• Identity Operators
• Membership Operators

### Arithmetic Operators:

Arithmetic Operators are used to perform mathematical operations on numeric values, such as:- Addition, Subtraction, Multiplication, Division and many more…

### Assignment Operators:

Assignment Operators are used to assign values to variables.

Then,

What does assigning a value to a variable mean?

It simply means that giving or storing a value to a variable.

Example:

x = 2

Here we are assigning 2 to the variable x with the help of an assignment operator (=).

So,

Assigning a value to a variable in Python is so simple, You just have to write the variable name on the left hand side of equal to operator (=) and the value on the right hand side, as shown above.

### Comparison (Relational) Operators:

Comparison Operators are used to compare values on either side of the operator and find out the relationship between them.

These are also called as relational operators.

These operators basically return True or False according to the condition.

Which means:- It return True if the condition is satisfied otherwise it return False.

### Logical (Boolean) Operators:

Logical Operators are used to combine `True` or `False` values of variables, statements or any other expressions to carry out their resultant truth value.

### Bitwise (Binary) Operators:

Bitwise Operators are used to perform bitwise calculations on integers.

Yes, Only integers Python Bitwise Operators work only on integers.

These are also called as Binary Operators.

Now,

You might be wondering that what does bitwise calculation means?

It simply means that operations (calculations) that involves working with individual bits,

Bits are the smallest units of data in a computer. Each bit has a single binary value: `0` or `1`.

Bitwise operators consider operands (integers) as sequences of binary digits (i.e., `0` and `1`),

The integers are first converted into `binary` format and then operations are performed bit by bit, Then, the result is returned back in `decimal` (integers) format.

Example:

Let’s consider two Variable named X and Y which consists of two integer values as: X = `12` and Y = `13`.

And,

we know that, binary of `12 = 0b1100` and binary of `13 = 0b1101`,

And, If you don’t know How to convert a decimal number into binary then, You can visit this video.

Let’s Understand all the above Operators one by one with examples:

Bitwise AND:

Bitwise OR:

Bitwise NOT:

Bitwise XOR:

Bitwise Right Shift:

Bitwise Left Shift:

### Identity Operators:

As the name indicates, Identity is related to the term identicalness, sameness which simply means that the quality or fact of [several specified things (here it is Operands )] being the same,

As well as, Identity is also related to id ( i.e., address) of the memory locations where the data (values) are stored in the memory.

So,

Basically,

Identity Operators are used to check whether two Operands (i.e., values or variables) are equal and also if they stored at the same memory location ( i.e., both have same memory address) or not.

In the above example, We can see that the 1st (i.e., is operator) example gives the output as: True,

Because, the value of a and b are equal as well as they both have the same memory location (i.e., both the values are stored at the same memory address).

Let’s Check, It is true or not:

```a = 2
b = 2

a is b
True

id(a)    # id is used for printing the memory address of the variable (here it is a)
1443428288

id(b)
1443428288
```

And,

In the 2nd (i.e., is not) example, The Output is again: True,

Because,

However, both the variables have the same values but they do not have stored at the same memory locations, which means they both have the different id (i.e., address) in the memory location.

Let’s Check:

```x = (5,6,7)
y = (5,6,7)

x is not y
True

id(x)
62687368

id(y)
62686504    # id is not same as that of the id of x
```

### Membership Operators:

Membership Operators are used to check whether a value or a variable is a member of a particular sequence or not.

The sequence might be a `string` (i.e., sequence of characters), a `list`, a `tuple`, a `set `or a` dictionary` that you will learn about in later tutorials.

There are two membership Operators in Python:

• `in`
• `not in`

Let’s understand the above concept with the help of a real world example:

Suppose, a person named “John” belongs to a family named X and another person named “Harry” does not belongs to the family X.

You can think of families as a list of family members.

Here, We can write the list of John’s family members as: [ John, John’s Father, John’s Mother, John’s Brother, John’s Sister, … ]

And,

If we want to check who is not belonging to the family X then we can use either `in` or `not in` Operator.

I know you are thinking that we already know about who belongs to the family X and who’s not.

Then, What is the need for checking?

It’s just for the sake of better understanding about the membership operators that it is working or not.

So,

If the `in` Operator returns `True` then we will conclude that the person belongs to the family X otherwise not belonging to the family X.

Example:

```"Harry" in X
False    # it means Harry is not belonging to the family X.
```

Or,

If the `not` `in` Operator returns `True` then we will conclude that the person does not belongs to the family X otherwise belonging to the family X.

Example:

```"Harry not in X
True     # it means Harry is not belonging to the family X.
```

So, That’s it from my side

Now, I’d like to hear from you:

Did I Miss Anything?

Or,

Is this post helpful for you?

Let me know by leaving a quick comment below.