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 hashtagFalse > a != b hashtagTrue > a < b hashtagTrue > a > b hashtagFalse > a <= b hashtagTrue > a >= b hashtagFalse 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 hashtagReturns True if both statements are true (x > 3 or x < 4) # True hashtagReturns True if one of the statements is true not(x > 3 and x < 10) hashtagFalse hashtagReverse 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 hashtagTrue > "RAKESH" in Sibling hashtagFalse > "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 hashtagTrue > 2 or 2 < 4 #2 > bool(2) or 2 < 4 hashtagTrue > 5 > 10 or [] # [] > 2 < 4 or [] hashtagTrue 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 hashtagRR hashtagDay3 hashtagPython hashtagOperators hashtagDataScience hashtagHappyLearning hashtagWeLearnEveryday 

Comments

Popular posts from this blog

Day 32 - Python Script to track Available COVID-19 Vaccine Slots for 18+ in India

DAY 1 - Steps to prepare your windows laptop for Python Programming

Day 26 - Call Power BI REST APIs to get POWER BI REPORT Details