OPERATORS ========= LOGICAL OPERATORS ================= 3-TYPES: 1) LOGICAL AND ==> and 2) LOGICAL OR ==> or 3) LOGICAL NOT ==> not 2) LOGICAL OR ==> or ====================== ==> binary operator a b a or b ====================== False False False False True True True False True True True True print(True or True) print(True or False) print(False or True) print(False or False) # with integers print(10 or 20) # 10 print(-10 or 0) # -10 print(0 or 10) # 10 print(0 or -20) # -20 # when the first input == non-zero value, output == first-input without decoding the second input # when the first input == zero value, output == second input # with other datatypes print(10.2 or 0.0000) print(0.0000 or 20.234) print(10-23j or 0j) print('a' or '0') print('' or '10') ===================================== 3) LOGICAL NOT ==> not ======================== ==> unary operator a not a ============= True False False True Note: ==== 1) The logical and Logical or: for other inputs (other than Booleans) output ==> corresponding type of input Ex: input ==> decimal ==> output ==> decimal 2) The Logical not: for any input ==> output ==> Boolean print(not True) print(not False) print(not 10) print(not 0) print(not 1.23) print(not 0.0000) print(not 1-2j) print(not -0j) print(not 'a') print(not '') # Any value other than zero (non-zero) ==> True # value with zero ==> False ========================================================= CONDITIONAL OPERATOR ==================== ==> ALSO CALLED AS "TERNARY OPERATOR" Syntax: resultant-variable = expression1 if condition else expression2 here: if and else ==> keywords conditions ==> with relational operators with relational and logical operators ex: a > b, a == b or a < b # WAP IN PYTHON TO FIND WHETHER THE NUMBER IS POSITIVE OR NEGATIVE USING CONDITIONAL OPERATOR. number = -123 result = "number is positive" if number > 0 else "number is negative" print("number is positive") if number > 0 else print("number is negative") print(result) =================================== # WAP TO FIND THE DIFFERENCE IF THE FIRST NUMBER IS GREATER THAN SECOND OTHERWISE FIND THEIR SUM. # first, second # first > second ==> first - second # otherwise: first + second first = 79 second = 97 result = (first - second) if first > second else (first + second) print(result) ================================== ASSIGNMENT: ========== 1) WAP TO CHECK WHETHER THE GIVEN NUMBER IS EVEN NUMBER OR ODD NUMBER USING CONDITIONAL OPERATOR. 2) WAP TO FIND THE BIGGEST NUMBER AMONG TWO NUMBERS USING TERNARY OPERATOR. =============================================================== BITWISE OPERATORS ================= ALL BITWISE OPERATIONS CAN BE PERFOM ON INDIVIDUAL BITS OF THE GIVEN DATA EX: 10 ==> BITS 0 ==> 0000 1 ==> 0001 2 ==> 0010 3 ==> 0011 4 ==> 0100 5 ==> 0101 6 ==> 0110 7 ==> 0111 8 ==> 1000 9 ==> 1001 10 ==> 1010 11 ==> 1011 12 ==> 1100 13 ==> 1101 14 ==> 1110 15 ==> 1111 ==> BITWISE OPERATIONS ARE ALLOWED TO DEFINE WITH: BOOLEAN DATA AND INTEGRE DATA ONLY. ==> BITWISE OPERATIONS ARE: BITWISE AND ==> & BITWISE OR ==> | BITWISE XOR ==> ^ BITWISE COMPLEMENT ==> ~ SHIFT OPERATORS LEFT SHIFT ==> << RIGHT SHIFT => >> BITWISE AND ==> & ========================== ==> BINARY OPERATOR a b a & b ===================== 1 1 1 1 0 0 0 1 0 0 0 0 print(0b1 & 0b1) print(0b1 & 0b0) print(0b0 & 0b1) print(0b0 & 0b0) print(13 & 17) # 1 print(0o12 & 0o21) # 0 print(0x11 & 0x22) # 0 # print(1.2 & 2.3) print(True & False) ====================================== BITWISE OR ==> | ========================= ==> Binary operator a b a | b ===================== 0 0 0 0 1 1 1 0 1 1 1 1 print(0b0 | 0b0) print(0b0 | 0b1) print(0b1 | 0b0) print(0b1 | 0b1) print(17 | 19) # 19 print(0o12 | 0o13) # 11 print(0x21 | 0x22) # 35 print(True | False) # print('a' | 'b') ========================================== BITWISE XOR ==> ^ ====================== ==> binary operator a b a ^ b ===================== 0 0 0 0 1 1 1 0 1 1 1 0 print(0b0 ^ 0b0) print(0b0 ^ 0b1) print(0b1 ^ 0b0) print(0b1 ^ 0b1) print(10 ^ 32) print(0o1122 ^ 0o2344) print(0x11 ^ 0x22) print(True ^ False) =============================================== BITWISE COMPLEMENT ==> ~ ========================= can use to find the 2's complement of number. unary operator 2's complement ==> inverter +eve ==> -eve -eve ==> +eve print(~0) print(~7) print(~-7)