DAY 3 - Operators
a = 10 # 1010
b = 20 #0001 0100
c = 0
1. Bit-wise Operator:
c = a & b; # 0 = 0 # Operator copies a bit to the result if it exists in both operands
c = a | b; # 30 = 0001 1110 # It copies a bit if it exists in either operand.
c = a ^ b; # 30 = 0001 1110 # (a XOR b)
c = ~a; # 5 = 0101 # It is unary and has the effect of 'flipping' bits.
c = a << 2; # 40 = 0010 1000 # The left operands value is moved left by the number of bits specified by the right operand.
c = a >> 2; # 2 = 0000 0010 # The left operands value is moved right by the number of bits specified by the right operand.
2. Comparison Operator: It returns Boolean value i.e. either True or False
> a == b False > a != b True > a < b True
> a > b False > a <= b True > a >= b False
3. Logical Operator: Logical operators are used to combine conditional statements and it returns Boolean value in this case.
x= 5
(x > 3 and x < 10) # True Returns True if both statements are true
(x > 3 or x < 4) # True Returns True if one of the statements is true
not(x > 3 and x < 10) False Reverse the result, returns False if the result is true
4. Membership Operator: Membership operators are used to test if a sequence (such as strings, lists, or tuples) is presented in an object. It returns boolean value Sibling = ["Rakesh", "Rupesh","Ritesh","Puja"] > "Rakesh" in Sibling True > "RAKESH" in Sibling False > "Khushi" not in Sibling # True Note: Comparison Operator , Logical Operator & Mebership operator can be used in IF-ELSE statement Some interesting observations: a = 10 b = 5 > a and b #5 > b and a #10 > a or b #10 > b or a #5 > 2 < 4 or 2 True > 2 or 2 < 4 #2 > bool(2) or 2 < 4 True > 5 > 10 or [] # [] > 2 < 4 or [] True if 2 or 2 < 4: print(2) else: print(2<4) o/p: 2 Observation 1 : and operator returns the first false operand it finds, or the last one if both are evaluated to True Observation 2 : or operator returns the first true operand it finds, or the last one if both are evaluated to false RR Day3 Python Operators DataScience HappyLearning WeLearnEveryday
4. Membership Operator: Membership operators are used to test if a sequence (such as strings, lists, or tuples) is presented in an object. It returns boolean value Sibling = ["Rakesh", "Rupesh","Ritesh","Puja"] > "Rakesh" in Sibling True > "RAKESH" in Sibling False > "Khushi" not in Sibling # True Note: Comparison Operator , Logical Operator & Mebership operator can be used in IF-ELSE statement Some interesting observations: a = 10 b = 5 > a and b #5 > b and a #10 > a or b #10 > b or a #5 > 2 < 4 or 2 True > 2 or 2 < 4 #2 > bool(2) or 2 < 4 True > 5 > 10 or [] # [] > 2 < 4 or [] True if 2 or 2 < 4: print(2) else: print(2<4) o/p: 2 Observation 1 : and operator returns the first false operand it finds, or the last one if both are evaluated to True Observation 2 : or operator returns the first true operand it finds, or the last one if both are evaluated to false RR Day3 Python Operators DataScience HappyLearning WeLearnEveryday
Comments
Post a Comment