PYTHON OPERATORS =================== 4) RELATIONAL OPERATORS ======================== ==> ALSO CALLED AS "COMPARISION OPERATORS" ==> RETURNS: BOOLEAN VALUES (True/False) ==> THE RELATIONAL OPERATORS ARE: < ==> LESS THAN > ==> GREATER THAN <= ==> LESS THAN OR EQUALS TO >= ==> GREATER THA OR EQUALS TO == ==> EQUALS TO != ==> NOT EQUALS TO ==> ARE BINARY OPERATORS a = int(input("Enter some value:")) b = int(input("Enter some value:")) print(a < b) # a is less than b print(a > b) # a is greater than b print(a <= b) # a is either less or equal to b print(a >= b) # a is either greater or equals to b print(a == b) # a is equals to b print(a != b) # a is not equals to b DIFFERENCE BETWEEN = AND == ============================ == ==> CHECK TWO NUMBERS ARE EQUAL OR NOT EX: A == 10;B= 10 A == B==> TRUE A = B # B VALUE WILL BE ASSIGNED TO A # equality # a == 100 Name Error a = 100 b = 100 print(a == b) # print(a = b) # Type Error ======================================== 5) LOGICAL OPERATORS ===================== ==> ARE ALLOWED TO DEFINE ON LOGICAL VALUES (BOOLEAN VALUES) ==> PYTHON CAN BE ALLOWED TO USE THE LOGICAL OPERATORS WITH ANY OTHER DATA. ==> THREE LOGICAL OPERATORS: 1) LOGICAL AND ==> and 2) LOGICAL OR ==> or 3) LOGICAL NOT ==> not ==> and/or operators ==> binary operators ==> not ==> unary operator ==> all these logical operators, working based on the truth tables. and operator ========== a b a and b ====================== False False False False True False True False False True True True Note: (Otherthan Booleans) ===================== 1) if both inputs are non-zero values output ==> second input value(second operand) 2) if first operand ==> zero and second operand ==> non-zero output ==> zero 3) if first operand ==> non-zero and second operand ==> zero output ==> zero print(False and False) print(False and True) print(True and False) print(True and True) # define with integers print(10 and 20) print(0b11001 and 0o1122) print(0 and 10) print(20 and 0) print(-10 and -20) # with floats print(1.2 and 2.3) print(0.0 and 2.3) print(1.2 and 0.0) # WITH COMPLEX print(1-2j and 1+2j) # with strings print('a' and 'b') print('' and 'b') print(True) print('a' and '') print('Ravi') =================================== or operator ========= a b a or b ===================== False False False False True True True False True True True True Note: (defining with otherthan boolean values) ==================================== 1) if both operands ==> non-zero output ==> First operand 2) if first operand ==> zero second operand ==> non-zero output ==> Second operand 3) if the first operand ==> non-zero second-operand ==> zero output ==> first operand print(False or False) print(False or True) print(True or False) print(True or True) # with integers print(10 or 20) print(-10 or -20) print(-10 or 20) print(0 or 20) print(10 or 0) print(0 or 0) # with floats print(10.2 or 20.3) print(0.0 or 20.3) print(10.2 or 0.0) # with complex print(10-2j or 10+3j) # with string print('a' or 'b') print('' or 'b') print('a' or '') =================================== not operator ========= a not a ============ True False False True Note: ==== can accept any type of input output ==> boolean print(not True) print(not False) # with integers print(not 10) # with floats print(not 0.0001) # with complex print(not 1-2j) # with string print(not 'a') print(not '') ================================== 6) TERNARY OPERATOR ==================== ==> ALSO CALLED AS "CONDITIONAL OPERATOR". Syntax: value3/operand3 = value1/operand1 if test-condition else value2/operand2 # WAP IN PYTHON USING TERNARY OPERATOR TO CHECK WHETHER THE GIVEN INTEGER IS POSITIVE OR NEGATIVE. number = int(input("Enter a value:")) # positive ==>? number > 0 # negative ==> number < 0 # result = "number is positive" if number > 0 else "number is negative" # # print(result) print(number,"is positive number") if number > 0 else print(number,"is negative number") =========================================== # WAP IN PYTHON USING TERNARY OPERATOR TO FIND WHETHER THE GIVEN NUMBER IS EVEN NUMBER OR ODD NUMBER. number = int(input("Enter a value:")) # even ==> number / 2, remainder == 0 # odd ==> number / 2, remainder != 0 print(number,"is an even number.") if number % 2 == 0 else print(number,"is an odd number.") ====================================================== # WAP IN PYTHON TO FIND THE MAXIMUM AMONG THREE NUMBERS USING CONDITIONAL OPERATOR. a = int(input("Enter a value:")) # 7 b = int(input("Enter a value:")) # 1 c = int(input("Enter a value:")) # 9 maximum = a if (a > b and a > c) else (b if b > c else c) print("The Maximum value = ",maximum) =================================================== SPECIAL OPERATORS ================== ==> TWO SPECIAL OPERATORS: 1) MEMBERSHIP OPERATORS ==> in and not in ====================================== on collection data and on strings used to check whether the specified element is belonging to the collection or not. return: Boolean Values a = 'python' b = [1,3,5,7,9] print('P' in a) # False print('P' not in a) # True print(9 in b) # True print(9 not in b) # False 2) IDENTITY OPERATORS ==> is and is not =================================== ==> used to check the address of the data whether the two objects are belonging to same address or not. a = 100 b = 200 c = 100 print(id(a)) print(id(b)) print(id(c)) print(a is c) # True print(a is not b) # True