Python Operators : Types of Operators in Python

Python Operators: Types of Operators in Python

Here in this blog, you will know about the python operators and also the operators types in detail. And you will get the proper knowledge about how to use the python operators because we will tell you all about the python operators and types of operators in python with example. Students often get their python operators assignment from college, and they always face many issues, so they always prefer to hire experts for python homework help, but we are here to provide you complete detail about python operators so that you can do your python assignment by itself. 

What are Operators in Python?

Operators are the special symbols that are used to carry out logical, arithmetic or bitwise computations. To assign a value to a variable operators are used. And the operator operates on the value which is called the operand. 

Types of Operators in Python with Example

Here, we will cover all Python operators with examples and also the shorthand assignment operators in python with the help of python operators examples. There are many types of python operators, which are shown below in detail.

Arithmetic operators

To perform mathematical operations such as addition, subtraction, multiplication, etc, arithmetic operators are used.

operatordescriptionexample
*It is used to multiply two operandsa * b

It is used subtract right operand from the left or unary minusa – b – 4
/It is used to divide left operand by the right onea / b
+It is used to add the two operands or unary plusa + b + 4
//In the Floor division the division that results into whole number adjusted to the left in the number linea // b
**In the Exponent the left operand raised to the power of righta**b (a to the power b)
%Modulus is used to find out the remainder by the division of left operand by the righta % b (remainder of a/b)

Example : Arithmetic operators in Python

a = 16
b = 4

# Output: a * b = 64
print('a * b =',a*b)

# Output: a - b = 12
print('a - b =',a-b)

# Output: a / b = 4
print('a / b =',a/b)

# Output: a + b = 20
print('a + b =',a+b)

# Output: a ** b = 65536
print('a ** b =',a**b)

Output

a * b = 64
a - b = 12
a / b = 4
a + b = 20
a ** b = 65536

Comparison operators

Comparison operators are used to comparing the values. And according to the conditions, it returns either True or False.

operatordescriptionexample
<It is the Less than operator – It will be True if left operand is less than the right onea < b
==It is Equal to operator – It will be True if both the operands are equala == b
>It is the Greater than operator – It will be True if left operand is greater than the right onea > b
<=It is the Less than or equal to operator  – It will be True if left operand is less than or equal to the righta <= b
!=It is Not equal to – It will be True if the operands are not equala != b
>=It is the Greater than or equal to operator  – It will be True if left operand is greater than or equal to the righta >= b

Example : Comparison operators in Python

a = 7
b = 11

# Output: a < b is True
print('a < b is',a<b)

# Output: a == b is False
print('a == b is',a==b)

# Output: a > b is False
print('a > b is',a>b)

# Output: a <= b is True
print('a <= b is',a<=b)

# Output: x != y is True
print('a != b is',a!=b)

# Output: x >= y is False
print('a >= b is',a>=b)

Output

a < b is True
a == b is False
a > b is False
a <= b is True
a != b is True
a >= b is False

Logical operators

In Python And, Or, Not are the logical operators. And it gives the output / result in the form of True or False.

operatordescriptionexample
NotIf operand is false, then it will show True i.e., complements of the operandnot a
OrIf either of the operands is true, then it will show True a or b
AndIf both the operands are true, then it will show True a and b

Example : Logical Operators in Python

a = True

b = False


print('not a is',not b)
print('a or y is',a or b)

print('a and b is',a and b)

Output

not a is False
a or b is True
a and b is False

Bitwise operators

Bitwise operators mainly work on operands because binary digits are their strings. They perform bit by bit, hence the name.

Like for example, 7 is 111 in binary digit.

The table shown below:

Let x=10 (0000 1010 in binary) and y=4 (0000 0100 in binary)

operatordescriptionexample
~Bitwise NOT~a = -11 (1111 0101)
|Bitwise ORa | b = 14 (0000 1110)
^Bitwise XORa ^ b = 14 (0000 1110)
>>Bitwise right shifta >> 2 = 2 (0000 0010)
&Bitwise ANDa & b = 0 (0000 0000)
<<Bitwise left shifta << 2 = 40 (0010 1000)

Assignment operators

In Python, to assign the values to variable assignment operators are used. There are various assignment operators in the Python, which are shown below in the table:

operatordescriptionexample
=To left side operandassign the right side of expression valuea = b + c
+=It adds the right side operand with the left side operand and then assign it to the left side operanda += bi.e.,(a = a+b)
-=It subtract the right side operand from the left side operand and then assign it to the left side operand: True, If operands of the both side are are equala -= bi.e.,(a = a-b)
*=It multiplies the right side operand with left side operand and then assign it to the left side operanda *= bi.e.,(a = a*b)
/=It divides the left side operand with right side operand and then assign it to the left side operanda /= bi.e.,(a = a/b)
%=It takes the modulus using the left and right side operands and assign its result to the left side operanda %= bi.e.,(a = a % b)
//=It divides the left side operand with the right side operand and then assign it to the value(floor) to the left side operanda //= bi.e.,(a = a // b)
**=It calculates the exponent (raise power) value by using the operands and assign its value to the left side operanda **= bi.e.,(a = a ** b)
&=It performs the Bitwise AND on the operands and assign its value to the left side operanda &= bi.e.,a = a & b
|=It performs the Bitwise OR on the operands and assign its value to the left side operanda |= bi.e.,a = a | b
^=It performs the Bitwise xOR on the operands and assign its value to the left side operanda ^= bi.e.,a = a ^ b
>>=It performs the Bitwise right shift on the operands and assign its value to left side operanda >>= bi.e.,a = a >> b
<<=It performs the Bitwise left shift on the operands and assign its value to the left side operanda <<= bi.e.,a = a << b

Example : Assignment Operators in Python

Assign operator

# Assigning values using 
# Assignment Operator

a = 2 
b = 4

c = a + b 

# Output 
print(c)

Output

c = 6

Examples of Other Assignment Operators in Python

a = 2
b = 4

# Output: a + b = 6
print('a += b',a+b)

# Output: a - b = -2
print('a -= b',a-b)

# Output: a * b = 8
print('a *= b',a*b)

# Output: a / b = 0.5
print('a /= b',a/b)

# Output: a % b = 2
print('a %= b',a%b)

# Output: a // b = 0
print('a //= b',a//b)

# Output: a ** b = 16
print('a **= b',a**b)

# Output: a &= b = 0
print('a &= b',a&b)

# Output: a |= b = 6
print('a |= b',a|b)

# Output: a ^= b = 6
print('a ^= b',a^b)

# Output: a >>= b = 0
print('a >>= b',a>>b)

# Output: a <<= b = 32
print('a <<= b',a<<b)

Output

a += b : 6
a -= b : -2
a *= b : 8
a /= b : 0.5
a %= b : 2
a //= b : 0
a **= b : 16
a &= b : 0
a |= b : 6
a ^= b : 6
a >>= b : 0
a <<= b : 32

Special operators

There are some special types of operators in the python language, such as the identity operator or the membership operator, which are shown below:

Identity operators

The identity operators in the python language are the “is” and “is not”. These are used when there are two variables/values that are located on the same memory part. And also, the two equal variables do not imply that they are identical.

operatordescriptionexample
isIf the operands are identical, It will be True (refer to the same object)a is True
is notIf the operands are not identical, It will be True (do not refer to the same object)a is not True

Example : Identity operators in Python

a1 = 8
b1 = 8
a2 = 'Heya'
b2 = 'Heya'
a3 = [1,2,3]
b3 = [1,2,3]

# Output: False
print(a1 is not b1)

# Output: True
print(a2 is b2)

# Output: False
print(a3 is b3)

Output

False
True
False

Membership operators

The membership operators in the python language are the “in” and “not in”. These are utilized to test whether a value or variable is found in a sequence (dictionary, set, list, tuple, and string). 

operatordescriptionexample
inIf the value/variable is found in the sequence then it will be True 5 in a
not inIf value/variable is not found in the sequence then it will be True5 not in a

Example : Membership operators in Python

a = 'Hello'
b = {1:'x',2:'y'}

# Output: True
print('H' in a)

# Output: True
print('hello' not in a)

# Output: True
print(1 in b)

# Output: False
print('x' in b)

Output

True
True
True
False

Conclusion

In this article, we covered almost all about the python operators and the Types of operators in python in detail with examples. This article will enhance your knowledge. Besides this, you can get the best python homework help service from us. So, if you want any kind of help with python homework, then feel free to contact us or comment below. 

Leave a Comment

Your email address will not be published. Required fields are marked *