UNIT-II
OPERATORS AVAILABLE IN PYTHON
Operators are special symbols in Python that carry out arithmetic or logical computation. The
value that the operator operates on is called the operand.
For example:
>>>2+3
5
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the
operation.
Arithmetic operator:
Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication, etc.
Operator Meaning Example
+ Add two operands or unary plus x + y+ 2
Subtract right operand from the left
x -y- 2
or unary minus
* Multiply two operands x *y
Divide left operand by the right one
x/y
(always results into float)
x %y
% Modulus-remainder of the division of left operand by
(remainder of
the right
x/y)
// Floor division-division that results into whole number x //y
adjusted to the left in the number line
x**y
** Exponent-left operand raised to the power of right (x to the power
y)
Example 1: Arithmetic operators in Python
x = 15
y=4
# Output: x +y =19
print('x + y =',x+y)
1
,# Output: x-y = 11
print('x -y=',x-y)
# Output: x * y = 60
print('x *y =',x*y)
# Output: x/ y = 3.75
print('x / y=',x/y)
# Output: x//y =3
print('x // y =',x//y)
Output
x + y = 19
x -y = 11
x *y = 60
x / y = 3.75
x //y = 3
x **y = 50625
Comparison operators
Comparison operators are used to compare values. It returns either True or False according to the
condition
Operator Meaning Example
> Greater than –True if left operand is greater than the right x>y
< Less than –True if left operand is less than the right x<y
== Equal to-True if both operands are equal x == y
!= Not equal to-True if operands are not equal x != y
Greater than or equal to- True if left
x >= y
operand is greater than or equal to the right
Less than or equal to-True if left operand is less than or
<= x <= y
equal to the right
Example 2: Comparison operators in Python
x =10
2
,y =12
# Output: x >y is False
print('x > y is' ,x>y)
# Output: x <y is True
print('x < y is', x<y)
# Output: x == y is False
print('x == y is ',x==y)
# Output: x!= y is True
print('x != y is ',x!=y)
# Output: x >= y is False
print('x >= y is' ,x>=y)
# Output: x <= y is True
print('x <= y is', x<=y)
Output
x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True
Logical operators
Logical operators are the and, or, not operators.
Operator Meaning Example
and True if both the operands are true x and y
or True if either of the operands is true x or y
not True if operand is false(complements the not x
operand)
3
, Example 3: Logical Operators in Python
x = True
y = False
print('x and y is' ,x and y)
print('x or y is', x or y)
print('not x is', not x)
Output
x and y is False
x or y is True
not x is False
Bitwise operators
Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit,
hence the name.
For example, 2 is 10 in binary and 7 is 111.
In the table below: Let binary) = 10 (0000 1010 in binary) and = 4 (0000 0100 in binary)
Operator Meaning Example
& Bitwise AND x & y = 0(00000000)
| Bitwise OR x | y = 14(00001110)
~ Bitwise NOT ~x = -11(11110101)
^ Bitwise XOR x ^ y = 14(00001110)
>> Bitwise right shift x >> 2 = 2(00000010)
<< Bitwise left shift x << 2 = 40(00101000)
Assignment operators:-
Assignment operators are used in Python to assign values to variables. a = 5 is a simple
assignment operator that assigns the value 5 on the right to the variable a on the left. There are
4
OPERATORS AVAILABLE IN PYTHON
Operators are special symbols in Python that carry out arithmetic or logical computation. The
value that the operator operates on is called the operand.
For example:
>>>2+3
5
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the
operation.
Arithmetic operator:
Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication, etc.
Operator Meaning Example
+ Add two operands or unary plus x + y+ 2
Subtract right operand from the left
x -y- 2
or unary minus
* Multiply two operands x *y
Divide left operand by the right one
x/y
(always results into float)
x %y
% Modulus-remainder of the division of left operand by
(remainder of
the right
x/y)
// Floor division-division that results into whole number x //y
adjusted to the left in the number line
x**y
** Exponent-left operand raised to the power of right (x to the power
y)
Example 1: Arithmetic operators in Python
x = 15
y=4
# Output: x +y =19
print('x + y =',x+y)
1
,# Output: x-y = 11
print('x -y=',x-y)
# Output: x * y = 60
print('x *y =',x*y)
# Output: x/ y = 3.75
print('x / y=',x/y)
# Output: x//y =3
print('x // y =',x//y)
Output
x + y = 19
x -y = 11
x *y = 60
x / y = 3.75
x //y = 3
x **y = 50625
Comparison operators
Comparison operators are used to compare values. It returns either True or False according to the
condition
Operator Meaning Example
> Greater than –True if left operand is greater than the right x>y
< Less than –True if left operand is less than the right x<y
== Equal to-True if both operands are equal x == y
!= Not equal to-True if operands are not equal x != y
Greater than or equal to- True if left
x >= y
operand is greater than or equal to the right
Less than or equal to-True if left operand is less than or
<= x <= y
equal to the right
Example 2: Comparison operators in Python
x =10
2
,y =12
# Output: x >y is False
print('x > y is' ,x>y)
# Output: x <y is True
print('x < y is', x<y)
# Output: x == y is False
print('x == y is ',x==y)
# Output: x!= y is True
print('x != y is ',x!=y)
# Output: x >= y is False
print('x >= y is' ,x>=y)
# Output: x <= y is True
print('x <= y is', x<=y)
Output
x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True
Logical operators
Logical operators are the and, or, not operators.
Operator Meaning Example
and True if both the operands are true x and y
or True if either of the operands is true x or y
not True if operand is false(complements the not x
operand)
3
, Example 3: Logical Operators in Python
x = True
y = False
print('x and y is' ,x and y)
print('x or y is', x or y)
print('not x is', not x)
Output
x and y is False
x or y is True
not x is False
Bitwise operators
Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit,
hence the name.
For example, 2 is 10 in binary and 7 is 111.
In the table below: Let binary) = 10 (0000 1010 in binary) and = 4 (0000 0100 in binary)
Operator Meaning Example
& Bitwise AND x & y = 0(00000000)
| Bitwise OR x | y = 14(00001110)
~ Bitwise NOT ~x = -11(11110101)
^ Bitwise XOR x ^ y = 14(00001110)
>> Bitwise right shift x >> 2 = 2(00000010)
<< Bitwise left shift x << 2 = 40(00101000)
Assignment operators:-
Assignment operators are used in Python to assign values to variables. a = 5 is a simple
assignment operator that assigns the value 5 on the right to the variable a on the left. There are
4