# 7 Types of Python Operators that will ease your programming

Python has 7 types of operators. In this Python Operators article, we will discuss all of them in detail with examples.

*Python, the fastest-growing major programming language, has risen in the ranks of programming languages, edging out Java this year and standing as the second most loved language (behind Rust) – Stack Overflow*

First, let’s discuss what are operators.

### What are Operators?

An operator is a symbol that will perform mathematical operations on * variables *or on values. Operators operate on operands (values) and return a result. Python has 7 types of operators that you can use:

- Arithmetic Operators
- Relational Operators
- Assignment Operators
- Logical Operators
- Membership Operators
- Identity Operators
- Bitwise Operators

Let’s take an example:

2+3

Here, + is an operator for addition. It adds 2 and 3 and prints 5 in the interpreter. This is an arithmetic operator.

## Types of Python Operators

### 1. Python Arithmetic Operators

Let’s discuss arithmetic operators- they are the most common. You have done this earlier.

Python has seven arithmetic operators for different mathematical operations. They are:

- + (Addition)
- – (Subtraction)
- * (Multiplication)
- / (Division)
- ** (Exponentiation)
- // (Floor division)
- % (Modulus)

#### 1.1. Addition Operator

The addition operator [+] adds two values and gives their sum.

**Example:**

>>> num1=7 >>> num2=4 >>> num1+num2

**Output:**

>>> 7+4.1

**Output:**

In this example, we added integers to integers and integers to floats.

#### 1.2. Subtraction Operator

The subtraction operator [-] subtracts second value from first and gives their difference.

**Example:**

>>> num1=7 >>> num2=4 >>> num1-num2

**Output:**

>>> 7-4.1

**Output:**

In this example, we subtracted integers from integers and floats from integers.

#### 1.3. Multiplication Operator

The multiplication operator [*] multiplies two values and gives their product.

**Example:**

>>> num1=7 >>> num2=4 >>> num1*num2

**Output:**

>>> 7*4.1

**Output:**

In this example, we multiplied integers by integers and floats by integers.

#### 1.4. Division operator

The division operator [/] divides one value by second and gives their quotient.

**Example:**

>>> num1=7 >>> num2=4 >>> num1/num2

**Output:**

>>> 7/4.1

**Output:**

In this example, we divided integers by integers and integers by floats. Dividing in Python 3 always gives a float result.

#### 1.5. Exponentiation

The exponentiation operator [**] raises one value to power of second.

**Example:**

>>> num1=7 >>> num2=4 >>> num1**num2

**Output:**

>>> 7**4.1

**Output:**

In this example, we raise integers to the power of integers and integers to the power of floats.

#### 1.6. Floor Division

The floor division operator [//] divides one value by second and gives their quotient rounded to the next smallest whole number.

**Example:**

>>> num1=7 >>> num2=4 >>> num1//num2

**Output:**

>>> 7**4.1

**Output:**

In this example, we floor-divided integers by integers and integers by floats.

#### 1.7. Modulus

The modulus operator [%] divides one value by second and gives their remainder.

Example:

>>> num1=7 >>> num2=4 >>> num1%num2

**Output:**

>>> 7%4.1

**Output:**

In this example, we floor-divided integers by integers and integers by floats.

*Any doubts in TechVidvan’s Python Operators article till now? Mention them in the comment section.*

### 2. Python Relational Operators

Now, let’s talk about relational operators. They are also called comparison operators and they compare values. Python has 6 relational operators:

- > (Greater than)
- < (Less than)
- == (Equal to)
- != (Not equal to)
- >= (Greater than or equal to)
- <= (Less than or equal to)

#### 2.1. Greater than

The greater than operator [>] returns True if the first value is greater than the second.

**Example:**

>>> num1=7 >>> num2=4 >>> num1>num2

**Output:**

>>> 7>4.1

**Output:**

In this example, we compared integers to integers and integers to floats.

#### 2.2. Less than

The less than operator [<] returns True if the first value is less than the second.

**Example:**

>>> num1=7 >>> num2=4 >>> num1<num2

**Output:**

>>> 7<4.1

**Output:**

In this example, we compared integers to integers and integers to floats.

#### 2.3. Equal to

The equal to operator [==] returns True if the first value is equal to the second.

**Example:**

>>> num1=7 >>> num2=4 >>> num1==num2

**Output:**

>>> 7==4.1

**Output:**

In this example, we compared integers to integers and integers to floats.

#### 2.4. Not equal to

The not equal to operator [!=] returns True if the first value is not equal to the second.

**Example:**

>>> num1=7 >>> num2=4 >>> num1!=num2

**Output:**

>>> 7!=4.1

**Output:**

In this example, we compared integers to integers and integers to floats.

#### 2.5. Greater than or equal to

The greater than or equal to operator [>=] returns True if the first value is greater than or equal to the second.

**Example:**

>>> num1=7 >>> num2=4 >>> num1>=num2

**Output:**

>>> 7>=4.1

**Output:**

In this Python operator example, we compared integers to integers and integers to floats.

#### 2.6. Less than or equal to

The less than or equal to operator [<=] returns True if the first value is smaller than or equal to the second.

**Example:**

>>> num1=7 >>> num2=4 >>> num1<=num2

**Output:**

>>> 7<=4.1

**Output:**

In this example, we compared integers to integers and integers to floats.

**Time to implement the knowledge of Python Operators with TechVidvan’s Tic Tac Toe Python Project.**

### 3. Python Assignment Operators

Now, let’s talk about assignment operators. They perform an operation and assign a value. Python has 8 assignment operators:

- = (Assign)
- += (Add and assign)
- -= (Subtract and assign)
- *= (Multiply and assign)
- /= (Divide and assign)
- %= (Modulus and assign)
- **= (Exponentiation and assign)
- //= (Floor-divide and assign)

#### 3.1. Assign

The assign operator [=] puts the value on the right in the variable on left.

**Example:**

>>> num1=7 >>> print(num1)

**Output:**

#### 3.2. Add and assign

The add and assign operator [+=] adds two values and assigns the result to the variable on left.

**Example:**

>>> num1=7 >>> num2=4 >>> num+=num2 >>> print(num1)

**Output:**

num1 is num1+num2 and this is 11.

#### 3.3. Subtract and assign

The subtract and assign operator [-=] subtracts second value from first and assigns to first.

**Example:**

>>> num1=7 >>> num2=4 >>> num1-=num2 >>> print(num1)

**Output:**

num1-num2 is 3, and num1 is now 3.

#### 3.4. Multiply and assign

The multiply and assign operator [*=] assigns the product to the variable on left.

**Example:**

>>> num1=7 >>> num2=4 >>> num1*=num2 >>> print(num1)

**Output:**

num1*num2 is 28, and num1 is 28 now. (7*4=28)

#### 3.5. Divide and Assign

The divide and assign operator [/=] assigns the division of two values to the first.

**Example:**

>>> num1=7 >>> num2=4 >>> num1/=num2 >>> print(num1)

**Output:**

num1/num2 is 7/4=1.75. So num1 is 1.75.

#### 3.6. Modulus and Assign

The modulus and assign operator [%=] performs modulus on two values and assigns to first.

**Example:**

>>> num1=7 >>> num2=4 >>> num1%=num2 >>> print(num1)

**Output:**

7%4 is 3, so num1 is 3.

#### 3.7. Exponentiation and Assign

The exponentiation and assign operator [%=] performs exponentiation on two values and assigns to first.

**Example:**

>>> num1=7 >>> num2=4 >>> num1**=num2 >>> print(num1)

**Output:**

7**4 is 2401, so num1 is 2401.

#### 3.8. Floor divide and Assign

The floor divide and assign operator [//=] performs floor division on two values and assigns to first.

**Example:**

>>> num1=7 >>> num2=4 >>> num1//=num2 >>> print(num1)

**Output:**

7//4 is 1, so num1 is 1.

*Next in Python operators, we will discuss logical operators in Python.*

### 4. Python Logical Operators

They can combine conditions. Python has 3 logical operators:

- and (Logical and)
- or (Logical or)
- not (Logical not)

#### 4.1. Logical and

The logical and operator returns True if both values are True. Otherwise, it returns False.

**Example:**

>>> True and False

**Output:**

>>> 3 and 4

**Output:**

For 3 and 4, it doesn’t give True, it gives the last value.

#### 4.2. Logical or

The logical or operator returns True if even one value is True. It returns False if both values are False.

**Example:**

>>> True or False

**Output:**

>>> 3 or 4

**Output:**

3 or 4 gives the first value.

#### 4.3. Logical not

The logical not operator returns True if an expression is True, otherwise returns False.

**Example:**

>>> not True

**Output:**

>>> not 4

**Output:**

4 is True, so it prints False.

### 5. Python Membership Operators

Membership operators check whether a value is in another. Python has 2 membership operators:

- in
- not in

#### 5.1. in operator

The in operator returns True if the first value is in second. Otherwise, it returns False.

**Example:**

>>> 2 in [1, 2, 3]

**Output:**

>>> ‘help’ in ‘stupidity’

**Output:**

2 is in the list [1, 2, 3]. ‘help’ is not in the string ‘stupidity’.

#### 5.2. not in operator

The not in operator returns True if the first value is not in second. It returns False otherwise.

**Example:**

>>> 2 not in (1, 2, 3)

**Output:**

>>> ‘help’ not in ‘stupidity’

**Output:**

### 6. Python Identity Operators

Identity operators check whether two values are identical. Python has 2 identity operators as well:

- is
- is not

#### 6.1. is operator

The is operator returns True if the first value is the same as the second. Otherwise, it returns False.

**Example:**

>>> 2 is 2.0

**Output:**

>>> a=8 >>> b=a >>> a is b

**Output:**

>>> c=8 >>> a is c

**Output:**

It returns True if two objects have the same identity.

#### 6.2. is not operator

The is not operator returns True if the first value is not identical to the second. It returns False otherwise.

**Example:**

>>> 0 is not False

**Output:**

0 is not identical to False.

### 7. Python Bitwise Operators

Finally, let’s talk about bitwise operators. They operate on values bit by bit. Python has 6 bitwise operators:

- & (Bitwise and)
- | (Bitwise or)
- ^ (Bitwise xor)
- ~ (Bitwise 1’s complement)
- << (Bitwise left-shift)
- >> (Bitwise right-shift)

#### 7.1. Bitwise and

The bitwise and operator [&] performs logical AND on corresponding bits in values.

**Example:**

>>> 3&4

**Output:**

3&4 is 011 & 100. This is 000 (0).

#### 7.2. Bitwise or

The bitwise or operator [|] performs logical OR on corresponding bits in values.

**Example:**

>>> 3 | 4

**Output:**

3|4 is 011|100. This is 111, which is 7.

#### 7.3. Bitwise xor

The bitwise xor operator [^] performs logical XOR on corresponding bits in values.

**Example:**

>>> 3^4

**Output:**

3^4 is 011^100. This is 111, which is 7.

#### 7.4. Bitwise 1’s complement

The bitwise 1’s complement operator [~] returns the bitwise negation of a value. Each bit is inverted.

**Example:**

>>> ~3

**Output:**

3 is 011- negation of this is 100, the result is -4. For x, its bitwise 1’s complement is -(x+1).

#### 7.5. Bitwise left-shift

The bitwise left-shift operator [<<] shifts bits for a value by a given number of places left. It adds 0s to new positions.

**Example:**

>>> 4<<2

**Output:**

4<<2 is 100<<2. This is 10000. This is 16.

#### 7.6. Bitwise right-shift

The bitwise right-shift operator [>>] shifts bits for a value by given number of places right. Some bits are lost.

**Example:**

>>> 4>>2

**Output:**

4>>2 is 100>>2. This is 1, which is 1 in decimal.

**Learn how to start operating on bits of numbers using Python bitwise operators.**

### Python Operator Precedence

Which operator evaluates first can be confusing. So we have some rules for this too. This is the precedence table that denotes which operator evaluates first:

Higher priority operators evaluate first. We can use this table to get the result of this expression:

>>> 4**2*4/3+4-2%4

**Output:**

## Summary

So, this was all about TechVidvan’s Python operators article. Today, we learned about 7 types of operators in Python and their subtypes. These are arithmetic, relational, assignment, logical, membership, identity and bitwise. We also saw some examples of Python operators. And last, we studied operator precedence.

**Now jump into our next tutorial, that is, Python comparison operators.**

If you find our Python operators’ article helpful, do share it with your friends on social media.

Good explanation.