Menu Close

Python Operators Tutorial

Python Operators

In this article, you will learn about Python operators as well as you will learn different types of operators in Python using appropriate examples. In previous we have seen all about Python comments and its usages. Any programming languages, Operators play a major role using Operators we can perform different types of operations.
In Python, operators are used to performing a specific task using variables and values.

Before going through this tutorial, Let’s understand what is operators in Python?

What is Python operators ?

In Python, the operator is a symbol which performs a specific task using variable and values. The value that the operator operates is called an operand.

Example

x = 12
y = 23
print(x + y)

In the above example + is the operator that performs addition. 12 and 23 is the operand in this example and + is the operator.

In Python, There are different types of operators available:

Types of Python Operators

  • Arithmetic Operator
  • Assignment Operator
  • Comparison Operator
  • Logical Operator
  • Identity Operator
  • Membership Operator
  • Bitwise Operator

Python Arithmetic Operators:

In Python, Arithmetic Operators are used to performing Mathematical operations like addition, division, multiplication, and subtraction.

+Add two operands or unary plus
Subtract right operand from left operand or unary minus
*Multiply two operands
/Divide left operand by right one
%Modulus – the remainder of the division.
//Floor division
**Exponent

Example

x = 18
y = 5

print('x + y:- ', x + y)
print('x -y:- ', x - y)
print('x * y:- ', x * y)
print('x / y:- ', x / y)
print('x % y:- ', x % y)
print('x // y:- ', x // y)
print('x ** y:- ', x ** y)

Output

x + y:-  23
x -y:-  13
x * y:-  90
x / y:-  3.6
x % y:-  3
x // y:-  3
x ** y:-  1889568

Python Assignment Operator:

In Python, the assignment operator is used to assign the value to variables.

OperatorsExamplesEquivalent To
=x = 10x = 10
+=x += 20x = x + 20
-=x -= 19x = x – 10
*=x *= 20x = x * 20
/=x /= 5x = x / 5
//=x //= 20x = x // 20
**=x /= 20x = x / 20
%=x %= 20x = x % 20
&=x &= 20x = x & 20
|=x |= 20x = x | 20
=x >>= 20x = x >> 20
<<=x <<= 20x = x << 20

Comparison Operator:

Python Comparison operators are used to compare one operand to another operand.

OperatorsMeaning
>Greater Than
<Less Than
<=Less Than or Equal To
>=Greater Than or Equal To
!=Not Equal To
==Equal To

Examples

x = 25
y = 19

print(x > y)
print(x < y)
print(x != y)
print(x <= y)
print(x >= y)
print(x == y)

Output

True
False
True
False
True
False

Logical Operators in Python:

In Python, Logical operators are used to combine conditional statements.

OperatorsMeaning
orReturn True if one of the statements is true
andReturn True if both the statement are true
notReverse the result, return False if the result is true.

Examples

x = 12
print(x < 20 or x < 3)
print(x < 30 and x  > 4)
print(not(x  < 30 and x > 4))

Output

True
True
False

Python Identity Operators:

is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal do not imply that they are identical.

OperatorsMeaning
isReturn True if the object is identical
is notReturn true if the object is identical

Example

x1 = 23
y1 = 23

x2 = 'Python'
y2 = 'Python'

x3 = [1,2,3]
y3 = [1,2,3]


print(x1 is y1)
print(x2 is not y2)
print(x3 is y3)

Output

True
False
False

Python Membership Operator:

Membership operators is used to check if a sequence present in the object.

OperatorsMeaning
inReturn True if a sequence present in the object
not inReturn True if a sequence is not present in the object.

Examples

x = 'Programming Funda'

print('P' in x)
print('Funda' not in x)

Output

True
False

Python Bitwise Operator

Bitwise operators are used to compare binary numbers.

OperatorsMeaning
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise not
>>Bitwise ZERO FILL RIGHT SHILT
<<Bitwise ZERO FILL LEFT SHILT

Conclusion

So, In this article, you have learned what is Python operators and Python operators types and usages. In Python, Operators are used to perform any particular task for example addition, division, comparison, and so many more operations can perform by operators.

I hope this tutorial will have helped you. If you like this Python operators tutorial, if you have any doubt regarding Python operators, Then you can comment. if you like this Python operators tutorial, Please share it with your friends who want to learn Python from scratch to advanced.

Python List Tutorial
Python Break Statement

Related Posts