Python Operators

Python Operators – All types with examples.

An operator is a special symbol that works over variables, values, or other types and returns a result. In this tutorial, you will learn, all types of operators along with examples.

What are all types of operators in Python?

  • Arithmetic Operators ( +, – , * etc.) – For basic mathematical operations, add, subtract etc.
  • Relational Operators (>, < , == etc.) – For comparing values.
  • Logical Operators (and, or etc) –
  • Assignment Operators ( =, +=, etc) –
  • Bitwise operators (|, & etc.) –
  • Identity Operator –
  • Membership Operator.

What Arithmetic Operators do?

They are used for basic mathematical functionality, such as additional, multiplication, subtraction, etc. The following tables list all arithmetic operators.


Operator MeaningPython CodeOutput
+ , -, /, *, %, Works on two Operands
+ Adds two numbers
– Subtract two numbers
/ Divides one number from another
* Multiply two numbers
% Modulus operator returns the remainder on division.
x = 5;
y = 2;

print(x + y);
print(x-y);
print(x/y);
print(x*y);
print(x%y);
7
3
2.5
10
1
//, ** // Floor division – Returns the smallest integer of the result.
** Exponential, One operand is the power of another
x = 5;
y = 2;
print(x//y);
print(x**y);
2
25
Tables for Arithmetic operators in Python

Relational Operators – Compare two Operands

With a relation operator, you can compare two values. The result is a boolean value, either True or False. The following table lists all compare operators in Python.

Relation OperatorsMeaningCodeOutput
>, < , ==, != , >=, <= > , more than
< , less than
== , equal
!= , not equal
>= , more than or equal
<= , less than or equal
x = 5;
y = 2;

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

print(x >= y);
True
False
False
True
False
True
Relation Operators in Python

Assignment Operators – Update or Initialize the content of a variable.

With assignment operators, you can set the value of a variable. Works on two (left and right) operands. The left side is a variable, the right side could be, expression, a constant, or another variable. Following is the list of all assignment operators in Python.

Assignment OperatorsDescriptionPython ExampleOutput
= Assigns a value from left to rightX = 5;
Y = X;
Z = X + 2;
print(X);
print(Y);
print(Z);
5
5
7
+=, -= etc.We can also use an assignment operator with another operator. The x += y, expends x = x + y;X = 5;
Y = 2;
Y += X;
Z = X + 2;
print(X);
print(Y);
print(Z);
5
7
7
Assignment Operators

Bitwise operators – Works ar bit level

A bit operator works on individual bit level. For example when we do a bitwise or of two variables, each bit at same positions from each variable is ored and resulting bits are returned. Below is the list of all bitwise operators.

Bitwise OperatorsDescriptionCode exampleOutput
&, |, >>, >>& End,
| Or,
>> Right Shift,
<< Left Shift
X = 5;
Y = 9 ;
print (" X is {0:b}".format(X));
print (" Y is {0:b}".format(Y));
print (" X&Y is {0:b}".format(X&Y));
print (" X|Y is {0:b}".format(X | Y));
print (" X >> 1 is {0:b}".format(X >> 1));
print (" Y << 1 is {0:b}".format(Y << 1));
X is 101
Y is 1001
X&Y is 1
X|Y is 1101
X >> 1 is 10
Y << 1 is 10010
^, ~^ Bitwise XOR, ~Bitwaise NotX = 5;
Y = 9 ;
print (" X is {0:b}".format(X));
print (" Y is {0:b}".format(Y));
print (" X^Y is {0:b}".format(X^Y));
print (" ~Y is {0:b}".format(~Y));
X is 101
Y is 1001
X^Y is 1100
~Y is -1010
Bitwise Operators Table

Logical Operators – Operands values are Ored, ends, or not.

Logical OperatorsDescription Example CodeOutput
and, or, not end and or, Works on two Operands (e.g X and Y).
and, Returns true, if both the operands are true (not zero).
or, Returns true, if any of the operands is true (not zero).
not , works on a single operand, it
returns the complement of an operand.
X = 5;
Y = 9 ;
Z = True;
M = False;
print (" X and Y is" , X and Y);
print (" X and Z is" , X and Z);
print (" X and M is" , X and M);
print (" X or Y is" , X or Y);
print (" X or Z is" , X or Z);
print (" X or M is" , X or M);
print( "not X ", not X);
print( "not Y ", not Y);
print( "not M ", not M);
X and Y is 9
X and Z is True
X and M is False
X or Y is 5
X or Z is 5
X or M is 5
not X False
not Y False
not M True
Logical Operators Table

Identity and Membership Operators:

Identity operators are special operators. There are two identity operators, is and is not. With is not we compare the two memory locations. Following is an example.

X = 5;
Y = 6;
Z = 6;

print(X is not Y)
print(X is Z)
print(Y is Z)

Output->

True
False
True

Membership Operators – To check if a value belongs to a sequence?

in and not in are the two membership operators, to find the presence of a value in the sequence e.g string, tuple, dictionary, etc.
in – Returns true if a value is present.
not in – Returns True if a value is absent.

String = 'Hello Python';

print('P' in String);
print('K' not in String);
print('Z' in String);

Output->

True
True
False