Operator - Operators are used to perform operations on two or more operands.
Example:
2 + 3 = 5 Here 2 and 3 are operands and + is the operator. So + operator perform addition operation on 2 and 3 operands
Arithmetic Operator: “Perform math operation”
+,-,/(Division Float),*,%,//(Division Floor),**(Power)
a = 43
b = 5
addition = a + b
subtraction = a - b
multiplication = a * b
division_float = a / b
remainder = a % b
division_floor = a // b
power = a**b
print("addition =",addition)
print("substraction =",substraction)
print("multiplication =",multiplication)
print("division_float =",division_float)
print("remainder =",remainder)
print("division_floor =",division_floor)
print("power =",power)
Relational Operators: “compares the values. It either returns True or False according to the condition.”
<,>,==,!=,<=,>=
a = True
b = False
print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)
Logical Operator- “perform logical operation”
and,or,not (Draw Truth table for each)
c = True
d = False
print(c and d)
print(c or d)
print(not c)
Few Condition which are false:
0
False
not ‘ ’ #not with space in string return false otherwise return true. not ‘ ’ or not “ ”
None #just like null in other programming languages
Membership Operator- “Used to check the given value is present in sequence or not.”
in and not in
c = [1,3,6,8,9]
print("1 in c =",1 in c)
print("1 not in c =",1 not in c)
#print("a in b = ",a in b)
d = {1,7,"virat"}
print("'virat' in d =",'virat' in d)
print("'Virat' not in d =",'Virat' not in d)
d = "Virat Kholi"
print("'rat' in d =",'rat' in d)
print("'rat' not in d =",'rat' not in d)
Identity operator - “used to check if both values are located to same memory location or not”
is , is not
c = 1
d = 1
print("c is d =",c is d)
print("1 is 1 =",1 is 1)
print("1 is not 1 =",1 is not 1)
a = ["aaa",11]
b = ["aaa",11]
c = a
print("a is b =",a is b)
print("a is c =",a is c)
Bitwise operation - “Work on binary of decimal values”
& (AND),|(OR),^(EX-OR),~(Negation),<<(LEFT Shift),>>(RIGHT SHIFT)
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
128 64 32 16 8 4 2 1/0
print(10 & 7) #ans is 2 Check with AND truth table
print(5 & 3) #ans is 1 IN1 IN2 ANDOut OROut XOR Not(sgl I/P)
0 0 0 0 0 1
0 1 0 1 1 0
1 0 0 1 1 0
1 1 1 1 0 1
8421
10 = 1010 5 0101
7 = 0111 3 0011
---------------- ------------
2 = 0010 1 0001
print(5 | 3) #ans is 7
8421
10 = 1010 5 0101
7 = 0111 3 0011
---------------- ------------
15 = 1111 7 0111
print(5 ^ 3) #ans is 6
8421
10 = 1010 5 0101
7 = 0111 3 0011
---------------- ------------
13 = 1101 6 0110
~ Negation (It will perform two's component(means i)) required only one input
print(~1) #ans is -2
8421 8421
1 = 0001 5 0101 15 1111 23
1 1 1
---------------- ------------ -------------
-2 = 0010 -6 0110 10000
print(30>>3)
print(30<<3)
print(25>>5)
print(25<<5)
'''
6 2
0110 >> 0001 1
0110 << 01 1000 24
30 >> 3
0001 1110 >> 0 0011 3
0001 1110 << 000 1111 0
25 >> 5
0001 1001 >> 0000 0
0001 1001 << 0 0011 0010 0000 800
Assignment Operator: “used to assign values to the variables”
+=,-=,*=,%=,/=,**=,^=
Bitwise & VS Logical and
# X Y
print(30 and 3). #logical check with Few Condition which are false consider x is true then print y if x is false print x
print(False and 3)
print(0 and 3)
print(None and 3)
print(not ' ' and 3)
print(30 or 3)#logicalcheck with Few Condition which are false consider x is true then print x if x is false print y
print(False or 3)
print(0 or 3)
print(None or 3)
print(not ' ' or 3)
print(30&3)#bitwise
print(30&3)#bitwise
1.What is similarity and difference between Logical(and) and Bitwise(&)
- Similarity- Both are used on the same Truth Table.
- Difference - Logical(and) not used