0% found this document useful (0 votes)
5 views13 pages

Python Operators

Uploaded by

anandsb8302
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Python Operators

Uploaded by

anandsb8302
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 13

Operators in Python

The operator is a symbol which performs a


particular operation.
Python supports the following types of
operators.
 Arithmetic operators
 Comparison operators
 Assignment Operators
 Logical Operators
 Bitwise Operators
 Membership Operators
 Identity Operators

Prepared by Dr. K. Akilandeswari


Arithmetic operators

Arithmetic operators are used to perform


arithmetic operations between two operands.
It includes
 + (addition),
 - (subtraction),
 * (multiplication),
 / (division),
 % (reminder),
 // (floor division)
 ** (exponent).

Prepared by Dr. K. Akilandeswari


Arithmetic operators
Example
Operator Description
Assume x=20 and y=10

Adds values of the operator on both


+ sides.
x + y = 30

Subtracts value of right hand operand


- from left hand operand value.
x – y = 10

Multiplies values of the operator on


* both sides.
x * y = 200

Divides left hand operand value by


/ right hand operand value
x/y=2

% Divides and returns remainder. x%y=0

** Calculates power on the operators 10**2 =100

Performs floor division of operands.


10//2 = 5
The decimal point are truncated. But if
// one of the operands is negative, the
Prepared by Dr. K. Akilandeswari
10.0//3.0 = 3.0
result is rounded. 10//3 = 3 -11//3 = -4
Python Logical Operators

Logical operators are used to combine two or


more relational expressions.

and (Logical AND)


or (Logical OR),
not (Logical NOT)

are the logical operators are supported by


Python language.

Prepared by Dr. K. Akilandeswari


Assume variable x=20 ; y=10; z=30

Operator Description Example

If expressions on both sides are (x >y) and (y<z) is


and true then the condition becomes true.
true.

If any of the two expressions is (x >y) and (y>z) is


or true then the condition becomes true
true.

Used to reverse the logical state Not(z > x) is false.


not of expression

Prepared by Dr. K. Akilandeswari


Python Bitwise Operators

The bitwise operators perform bit by bit


operation on the values of the two operands.
The python bitwise operators are

 & (Binary AND),


 | (Binary OR),
 ^ (Binary XOR),
 ~ (Binary Ones Complement),
 << (Binary Left Shift)
 >> (Binary Right Shift)

Prepared by Dr. K. Akilandeswari


Operator Description Example
a=5( 0101) b= 3 (0011)
Operator copies a bit to the
& result if it exists in both (a & b) = 1(0001)
operands
It copies a bit if it exists in
| either operand. (a | b) = 7 (0111)
It copies the bit if it is set in
^ one operand but not both. (a ^ b) =
It is unary and has the effect of
~ 'flipping' bits. (~a ) = -
The left operands value is
<< moved left by the number of
bits specified by the right a << 2 =
operand.
The left operands value is
>> Prepared bymoved right by the number of
Dr. K. Akilandeswari
Python assignment operators
Operator Description Example
Assigns values from right side operands z = x + y adds value of x and y
= to left side operand and assigns to z
It adds right operand to the left operand z += x is equivalent to z = z +
+= and assign the result to left operand x
It subtracts right operand from the left
-= operand and assign the result to left z -= x is equivalent to z = z – x
operand
It multiplies right operand with the left
*= operand and assign the result to left
z *= x is equivalent to z = z *
x
operand
It divides left operand with the right
/= operand and assign the result to left z /= x is equivalent to z = z / x
operand
It takes modulus using two operands and z %= x is equivalent to z = z
%= assign the result to left operand %x
Performs exponential calculation on the
**= operands and assign value to the left
Prepared by Dr. K. Akilandeswari
z **= x is equivalent to z =z **
x
operand
Python Comparison Operators

Comparison operators compare the values on


either sides of them and return Boolean true or
false.

They are also called Relational operators.

The expressions formed using relational operators


are called relational expressions.

The Comparison operators are ==(Equal), !


=(not equal), <>(not equal), >( greater than), <
(less than), >=(greater than or equal to), and
<=(less than or equal to).
Prepared by Dr. K. Akilandeswari
Comparison Operators
Operato Description Example
r Assume x=20 and
y=10
If the value of two operands is equal, then the (x == y) is not true.
== condition becomes true.
If values of two operands are not equal, then (x != y) is true.
!= the condition becomes true.
If values of two operands are not equal, then This is similar to !=
<> the condition becomes true. operator.
If the value of left operand is greater than the (x > y) is true.
> value of right operand, then condition becomes
true.
If the value of left operand is less than the (x < y) is not true.
< value of right operand, then condition becomes
true.
If the value of left operand is greater than or (x >= y) is true.
>= equal to the value of right operand, then
condition becomes true.
Prepared by Dr. K. Akilandeswari
If the value of left operand is less than or (x <= y) is not true.
Python Membership Operators

 Python membership operators are used to test the


membership of value inside a Python data structure
such as strings, lists, or tuples.
 If the value is present, then the resulting value is
true otherwise it returns false.
Description
Operator

It is evaluated to be true if the first


in operand is found in the second
operand (list, tuple, or dictionary).
It is evaluated to be true if the first
not in operand is not found in the second
operand (list, tuple, or dictionary).

Prepared by Dr. K. Akilandeswari


Python Identity Operators

 Identity operators compare the memory locations


of two objects.
 There are two Identity operators supported by
python are explained as below
Operator Description

is Evaluates to true , if the variables on


either side of the operator point to the
same object and false otherwise.

is not Evaluates to false, if the variables on


either side of the operator point to the
same object and true otherwise.
Prepared by Dr. K. Akilandeswari
Python Operators Precedence
S.N Operator & Description S.No Operator & Description
o. .

1 ** Exponentiation 8
<= < > >= Comparison
operators
2
~ + - Complement,
unary plus and minus
9
<> == != Equality
operators
3 * / % //
= %= /= //= -= +=
4 +- Addition and 10 *= **= Assignment
subtraction operators

5 >> << Right and left


11
is is not Identity
bitwise shift operators
6 & Bitwise 'AND'
12
in not in Membership
operators
7 ^ Bitwise exclusive `OR'
and regular `OR' 13 not or and Logical operators
Prepared by Dr. K. Akilandeswari

You might also like