Python Jyoti Chandnani
Operators
An operator is a symbol that tells the compiler to perform certain mathematical or logical
manipulations. Operators are used in program to manipulate data and variables.
Python language supports the following types of operators.
1. Arithmetic Operators
2. Assignment Operators
3. Comparison (i.e., Relational) Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Arithmetic Operators
Description Example
Operator
+ Addition :Adds values on either side of the a+b=6
operator
- Subtraction - Subtracts right hand operand from a-b=2
left hand operand
* Multiplication- Multiplies values on either side of a*b=8
the operator
/ Division- Divides left hand operand by right hand a/b = 2
operand
% Modulus - Divides left hand operand by right hand a%b=0
operand and returns remainder
** Exponent - Performs exponential (power) a**b =(a to the power of b)
calculation on operators
// Floor Division - The division of operands where the 9.0//2.0 is
result is the 9//2 is equal to quotient in which the equal to 4.0
digits after the decimal point are removed. 4
1
Python Jyoti Chandnani
Comparison Operators:
Operator Description Example
== Checks if the value of two operands are equal or (a == b)
not, if yes then condition becomes true.
!= Checks if the value of two operands are equal or (a != b)
not, if values are not equal then condition becomes
true.
> Checks if the value of left operand is greater than (a > b)
the value of right operand, if yes then condition
becomes true.
< Checks if the value of left operand is less than the (a < b)
value of right operand, if yes then condition
becomes true.
>= Checks if the value of left operand is greater than (a >= b)
or equal to the value of right operand, if yes then
condition becomes true.
<= Checks if the value of left operand is less than or (a <= b)
equal to the value of right operand, if yes then
condition becomes true.
Eg:
>>> a=10
>>> b=5
>>> a==b ➔ False
>>> a=='c' ➔ False
>>> a<10➔ False
>>> a<=10➔ True
>>> a>5 ➔True
>>> b==5 ➔True
>>> a!=10 ➔False
>>> b!=5➔ False
2
Python Jyoti Chandnani
Assignment Operators
Operator Description Example
= Simple assignment operator, Assigns values from right c = a + b will assign value of
side operands to left side operand a + b into c
+= Add AND assignment operator, It adds right operand to c += a is equivalent to
the left operand and assign the result to left operand c=c+a
-= Subtract AND assignment operator, It subtracts right c -= a is equivalent to
operand from the left operand and assign the result to c = c -a
left operand
*= Multiply AND assignment operator, It multiplies right c *= a is equivalent to
operand with the left operand and assign the result to c=c*a
left operand
/= Divide AND assignment operator, It divides left operand c /= a is equivalent to
with the right operand and assign the result to left c=c/a
operand
%= It takes modulus using c %= a is equivalent to
two operands and assign the result to left operand c=c%a
**= Exponent AND assignment operator, Performs c **= a is equivalent to
exponential (power) calculation on operators and assign c = c ** a
value to the left operand
//= Floor Division and assigns a value, Performs floor division c //= a is equivalent to
on operators and assign value to the left operand c = c // a
Eg1:
a = 20
b = 10
c=0
print('a value is',a)
print('b value is',b)
c += a
print ("Add AND - Value of c is ", c )
c *= a
print ("Multiply AND - Value of c is ", c )
c /= a
print ("Divide AND - Value of c is ", c )
c=2
c %= a
print ("Modulus AND - Value of c is ", c)
c **= a
print ("Exponent AND - Value of c is ", c)
c //= a
print ("Floor Division AND - Value of c is ", c)
c -= a
print ("Substract AND - Value of c is ", c )
3
Python Jyoti Chandnani
Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b = 13; Now in
binary format they will be as follows:
a = 0011 1100
b = 0000 1101
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
There are following Bitwise operators supported by Python language
Operator Description Example
& Binary AND Operator copies a bit to the result (a & b) will give 12 which is
if it exists in both operands. 0000 1100
| Binary OR Operator copies a bit if it exists in either (a | b) will give 61 which is
operand. 0011 1101
^ Binary XOR Operator copies the bit if it is set (a ^ b) will give 49 which is
in one operand but not both. 0011 0001
~ Binary Ones Complement Operator is unary (~a ) will give -61 which is
and has the effect of 'flipping' bits. 1100 0011
in 2's complement form due
to a signed binary number.
<< Binary Left Shift Operator. The left operands a << 2 will give 240 which is
value is moved left by the number of bits 1111 0000
specified by the right operand.
>> Binary Right Shift Operator. The left operands a >> 2 will give 15 which is
value is moved right by the number of bits 0000 1111
specified by the right operand.
Eg:
>>> 10<<2 ➔40
>>> 12<<2 ➔ 48
>>> 2<<2 ➔8
>>> 1<<2 ➔4
>>> 10>>2 ➔2
>>> 11>>2 ➔2
>>> 13>>2 ➔3
>>> 20>>2 ➔5
>>> 30>>3 ➔3
>>> 30>>4 ➔1
4
Python Jyoti Chandnani
Logical Operators
There are following logical operators supported by Python language.
Those are NOT, AND and OR
NOT
X Not X
True False
False True
AND
X Y X AND Y
True False False
False True False
True True True
False False False
OR
X Y X OR Y
True False True
False True True
True True True
False False False
Eg1:
>>> a=10
>>> b=20
>>> a==10 and b==20 ➔True
>>> a==20 and b==10 ➔False
>>> a==10 and b==10 ➔False
>>> not a==20 and b==20 ➔True
>>> not a==11 and not b==21 ➔True
>>> a==20 or b==20 ➔True
>> not a==12 and not b==10 ➔True
Eg2:
>>> a=10
>>> b=20
>>> a>=15 and b==20 ➔ False
>>> not a>=15 and b==20 ➔True
>>> not a>=15 and not b==20 ➔False
>>> not(not a>=15 and not b==20) ➔True
5
Python Jyoti Chandnani
Membership Operators
In addition to the operators discussed previously, Python has membership operators, which test for
membership in a sequence, such as strings, lists, or tuples. There are two membership operators
explained below:
Operator Description Example
In Evaluates to true if it finds a variable in the x in y, here in results in a 1 if x is a
specified sequence and false otherwise. member of sequence y.
not in Evaluates to true if it does not finds a variable x not in y, here not in results in a 1 if
in the specified sequence and false otherwise. x is not a member of sequence y.
Eg1:
>>> lst=[1,2,3,4,’Python’,True]
>>> 4 in lst ➔True
>>> 10 in lst ➔False
>>> 3 not in lst ➔False
>>> 20 not in lst ➔True
Eg2:
>>> st="Python Jyoti"
>>> 't' in st ➔True
>>> 'p' in st False
>>> 'p' not in st True
>>> 'P' in st True
>>> 'Na' in st True
>>> 'Nr' in st False
>>> ' ' in st True
>>> 'aa' in st False
>>> 'aa' not in st True
Eg3:
>>> d={'Id':100,'Name':'Jyoti','Loc':'Hyd'}
>>> 'Id' in d True
>>> 100 in d False
>>> 'Sal' not in d True
>>> 'Sal' in d False
>>> 'Loc' in d True
>>> 'Hyd' in d False
6
Python Jyoti Chandnani
Identity Operators
Identity operators compare the memory locations or references of two objects. There are two Identity
operators explained below:
Operator Description Example
is Evaluates to true if the variables on either side of x is y, here is results in 1 if id(x)
the operator point to the same object and false equals id(y).
otherwise.
is not Evaluates to false if the variables on either side of x is not y, here is not results in 1
the operator point to the same object and true if id(x) is not equal to id(y).
otherwise.
Eg 1:
>>> st="Sai"
>>> id(st) 57264032
>>> st1="Sai"
>>> id(st1) 57264032
>>> st is st1➔True
>>> st is not st1 ➔False
Eg 2:
>>> lst=[1,2,3,4]
>>> id(lst) 57297136
>>> lst1=[1,2,3,4]
>>> id(lst1) 57240512
>>> lst is lst1 ➔False
>>> lst is not lst1 ➔ True
7
Python Jyoti Chandnani
Operators Precedence
The following table lists all operators from highest precedence to lowest.
Operator Description
** Exponentiation (raise to the power)
~ ,+,- complement, unary plus and minus (method
names for the last two are +@ and -@)
* ,/, %, // Multiply, divide, modulo and floor division
+, - Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^| Bitwise exclusive `OR' and regular `OR'
<=, < ,> ,>= Comparison operators
<> ,== ,!= Equality operators
= ,%=, /= ,//=, -=, += ,*= ,**= Assignment operators
is ,is not Identity operators
in ,not in Membership operators
not ,or ,and Logical operators
8
Python Jyoti Chandnani
Exercise on operators:
1. True and True
2. False and True
3. 1 == 1 and 2 == 1
4. "test" == "Test"
5. 1 == 1 or 2 != 1
6. True and 1 == 1
7. False and 0 != 0
8. True or 1 == 1
9. "test" == "testing"
10. 1 != 0 and 2 == 1
11. test" != "testing"
12. "test" == 1
13. not (True and False)
14. not (1 == 1 and 0 != 1)
15. not (10 == 1 or 1000 == 1000)
16. not (1 != 10 or 3 == 4)
17. not ("testing" == "testing" and "Zed" == "Cool Guy")
18. 1 == 1 and not ("testing" == 1 or 1 == 0)
19. "chunky" == "bacon" and not (3 == 4 or 3 == 3)
20. 3 == 3 and not ("testing" == "testing" or "Python" == "Fun")
The None Value
In Python there is a value called None, which represents the absence of a value. None is the only value of
the NoneType data type. (Other programming languages might call this value null, nil, or undefined.) Just
like the Boolean True and False values, None must be typed with a capital N.
This can be helpful when you need to store something that won’t be confused for a real value in a variable.
One place where None is used is as the return value of print(). The print() function displays text on the
screen, but it doesn’t need to return anything in the same way len() or input() does. But since all function
calls need to evaluate to a return value, print() returns None.
Eg:
>>> a=print('Jyoti')
Jyoti
>>> None==a ➔True
Raw string:
We can place an r before the beginning quotation mark of a string to make it a raw string. A raw string
completely ignores all escape characters and prints any backslash that appears in the string.
>>> print('This \is \python \neena')
This \is \python
eena
Here, interpreter treated '\n' is a new line escaping character, so ‘eena’ came in new line.
>>> print(r'This \is \python \Jyoti') ➔ This \is \python \Jyoti
here, interpreter ignores all escaping characters because of 'r'. Because this is a raw string, Python
considers the backslash as part of the string and not as the start of an escape character. Raw strings are
helpful if we are typing string values that contain many backslashes, such as the strings used for regular
expressions.