Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

PART – 1

Operators
Operators are the symbols to do computation or check.

1) Unary Operator
The operator having one operand called unary operator.

Example:
+  Unary plus (Positive sing)
Example: + 5
-  Unary minus (Negative sing)
Example: - 10
~  Bitwise complement (Flip the bit from 1 to 0 or 0 to 1)
x, y = -10, 20 print(~x, ~y) Output: 9 -21

not  Logical Negation (If the result is true then converts it to


false. If result is false then converts it to true)

6/19/2020 Pyari Mohan Sahu - 7008146305 2


Operators
1) Binary Operator
The operator having two operand called binary operator.

a) Arithmetic Operators /  Division


+  Addition print(5 / 2) Output: 2.5
print(10 + 20) Output: 30
//  Floor division
-  Subtraction print(5 // 2) Output: 2
print(50 - 5) Output: 45
%  Modulus / Remainder
*  Multiplication print(5 % 2) Output: 1
print(5 * 2) Output: 10

**  Exponent (power)
print(2 ** 4) Output: 16

6/19/2020 Pyari Mohan Sahu - 7008146305 3


Operators

b) Relational Operators
>  Greater than ==  Equals to
print(10 > 20) Output: False print(10 == 10) Output: True
print(20 > 10) Output: True print(20 == 10) Output: False

<  Less than !=  Not Equals to


print(8 < 5) Output: False print(20 != 10) Output: True
print(5 < 8) Output: True print(10 != 10) Output: False

<=  Less than or equals to


print(8 <= 8) Output: True
print(5 <= 8) Output: True
print(25 <= 8) Output: False

>=  Grater than or equals to


print(8 >= 8) Output: True
print(25 >= 8) Output: True
print(5 >= 8) Output: False

6/19/2020 Pyari Mohan Sahu - 7008146305 4


Operators

c) Logical Operators
and  logical AND

First Second Result


True True True
True False False
False True False
False False False

or  logical OR

First Second Result


True True True
True False True
False True True
False False False

6/19/2020 Pyari Mohan Sahu - 7008146305 5


Operators

d) Augmented Assignment x += 2
(Shorthand) Operators print(x)  Output: 12
=  Assignment
x -= 7
print(x)  Output: 3
+=  Assign Sum
x *= 3
-=  Assign Difference
print(x)  Output: 30
*=  Assign Product x **= 3
print(x)  Output: 1000
**=  Assign Exponent
x /= 7
/=  Assign Quotient print(x)  Output: 1.4285714285714286

//=  Assign Floor Division x //= 7


print(x)  Output: 1
%=  Assign Remainder
x %= 7
print(x)  Output: 3

6/19/2020 Pyari Mohan Sahu - 7008146305 6


Operators
e) Membership Operators
f) Identity Operator
in  Checks whether variable in
is  Is identity same?
sequence
if('This' is 'this'):
print(“Same")
not in  Checks whether variable else:
not in sequence print(“Not same")

myList = [10, 5, 7, 78] Output: Not same


if(7 in myList):
print('Found') Is not  Is identity not same?
else: if('This' is not 'this'):
print('Not Found') print(“Not same")
else:
Output: Found
print(“Same")

if(27 not in myList): Output: Not same


print('Not Found')
else:
print('Found‘)
Output: Not Found
6/19/2020 Pyari Mohan Sahu - 7008146305 7
Operators

g) Bitwise Operators
Bitwise operators are used to perform bitwise calculations on integers.

The integers are converted into binary format and then operations are performed
bit by bit, hence the name bitwise operators.

Bitwise operators work on integers only and the final output is returned in the
decimal format.

Bitwise operators are also called binary operators.

&  Bitwise AND


Bitwise and operator returns 1 if both the bits are 1, otherwise 0.

a = 10  1010 (Binary) b = 7  0111


c=A&B 1 0 1 0
0 1 1 1
-------------------
0 0 1 0  2 (Decimal)
print(c)  Output: 2

6/19/2020 Pyari Mohan Sahu - 7008146305 8


Operators

|  Bitwise OR
Bitwise or operator returns If both the bits are 0, then it returns 0, otherwise 1.

a = 10  1010 (Binary) b = 7  111


c=A|B 1 0 1 0
0 1 1 1
-------------------
1 1 1 1  15 (Decimal)

print(c)  Output: 15

^  Bitwise Exclusive OR(XOR)


Bitwise XOR operator
Returns If both the bits are same, then it returns 0, otherwise 1

a = 10  1010 (Binary) b = 7  111


c=A^B 1 0 1 0
0 1 1 1
-------------------
1 1 0 1  13 (Decimal)
print(c)  Output: 13
6/19/2020 Pyari Mohan Sahu - 7008146305 9
Operators

h) Bitwise Shift Operators

Left Shift
Bitwise left shift operator shifts the left operand bits towards the left side for
the given number of times in the right operand.

In simple terms, the binary number is appended with 0s at the end.

a = 10  1010 (Binary)

a << 2  1 0 1 0 << 2
 1 0 1 0 0 0
 40 (decimal)

6/19/2020 Pyari Mohan Sahu - 7008146305 10


Operators

Right Shift
The left side operand bits are moved towards the right side for the given
number of times.

In simple terms, the right side bits are removed.

a = 10  1010 (Binary)

a >> 2  1 0 1 0 >> 2
 1 0
 2 (decimal)

6/19/2020 Pyari Mohan Sahu - 7008146305 11


a = 20
b = int(input(“Enter a number: ”))

a is b

a b

10 11 12 13

20 1 0 1 1 0

1 1 0 0
Special Case

>>> a = 10 >>> a = 10
>>> b = 10 >>> b = int(input("Enter a number: "))
>>> a == b Enter a number: 10
True >>> a == b
>>> a is b True
True >>> a is b
True
>>> a, b = 2.5, 2.5
>>> a == b >>> a = 2.5
True >>> b = float(input("Enter a number: "))
>>> a is b Enter a number: 2.5
True >>> a == b
True
>>> a, b = "this", "this" >>> a is b
>>> a == b False
True
>>> a is b
True

6/19/2020 Pyari Mohan Sahu - 7008146305 13


Special Case

a = "pyari" >>> a = 5+2.3j


>>> b = input("Enter a name: ") >>> b = 5+2.3j
Enter a name: pyari >>> a == b
>>> a == b True
True >>> a is b
>>> a is b False
False

In case of float and string, accepting the data will give different
reference location.

So, the result of is operator is false.

In case of complex number either assignment or accepting the data


will give different reference location.

So, the result of is operator is false.

6/19/2020 Pyari Mohan Sahu - 7008146305 14


Special Case

The reason behind this behavior is in Python creates two different


objects that both stores the same value.

1) Input of strings from the console

2) Writing integers literals with many digits (very big integer)

3) Writing floating – point and complex literals

6/19/2020 Pyari Mohan Sahu - 7008146305 15


Special Case

True and False represent the Boolean values.


true and false represents truth values.

The or operator will test the second operand only if the first operand is
false, otherwise ignore it.

Operation Result Reason

0 or 0 0 First expression 0 has falsetval hence second expression 0 is returned.


0 or 9 9 First expression 0 has falsetval hence second expression 9 is returned.
7 or 0.0 7 First expression 7 has truetval hence first expression 7 is returned.
‘ok’ or ‘’ ‘ok’ First expression ‘ok’ has truetval hence first expression ‘ok’ is returned.
‘’ or ‘s’ ‘s’ First expression ‘’ has falsetval hence second expression ‘s’ is returned.
‘’ or ‘’ ‘’ First expression ‘’ has falsetval hence second expression ‘’ is returned.
‘b’ or ‘t’ ‘b’ First expression ‘b’ has truetval hence first expression ‘b’ is returned.

6/19/2020 Pyari Mohan Sahu - 7008146305 16


Special Case
In an expression x and y.

If first operand, (x) has falsetval, then return first operand x as result.

Otherwise return y.

Operation Result Reason

0 and 0 0 First expression 0 has falsetval hence first expression 0 is returned.


0 and 9 0 First expression 0 has falsetval hence first expression 0 is returned.
7 and 0.0 0.0 First expression 7 has truetval hence second expression 0.0 is returned.
‘ok’ and ‘’ ‘’ First expression ‘ok’ has truetval hence second expression ‘’ is returned.
‘’ and ‘s’ ‘’ First expression ‘’ has falsetval hence first expression ‘’ is returned.
‘’ and ‘’ ‘’ First expression ‘’ has falsetval hence first expression ‘’ is returned.
‘b’ and ‘t’ ‘t’ First expression ‘b’ has truetval hence second expression ‘t’ is returned.

6/19/2020 Pyari Mohan Sahu - 7008146305 17


Special Case

The or only evaluates the second argument if the first one is falsetvalue.

The and only evaluates the second argument if the first one is truetvalue.

Operation Result
x or y If x is falsetvalue , then returns y as result, otherwise x
x and y If x is falsetvalue, then returns x as result, otherwise y
not x If x is falsetvalue, then return True as result, otherwise false.

Note
All operators have left – to – right associativity.
2+5*6
Except exponentiation (**), which has right – to – left associativity.

Example:
2 ** 3 ** 4 will be evaluated as 2 ** (3 ** 4), not as (2 ** 3) ** 4

6/19/2020 Pyari Mohan Sahu - 7008146305 18

You might also like