{"id":26115,"date":"2021-12-14T08:46:01","date_gmt":"2021-12-14T03:16:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=26115"},"modified":"2021-12-14T08:46:01","modified_gmt":"2021-12-14T03:16:01","slug":"python-program-for-membership-and-identity-operators","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-membership-and-identity-operators\/","title":{"rendered":"Python Program for Membership and Identity Operators"},"content":{"rendered":"
Python provides a number of operators for performing manipulation and operations on data values and variables on a larger scale.<\/p>\n
Let us see the two important types of Python operators:<\/p>\n
Membership operators are used to validate a value’s membership. It checks for membership in a sequence, such as strings, lists, or tuples.<\/p>\n
This means that it checks for the presence of the given data sequence in another sequence or structure and validates it.<\/p>\n
‘in’ – Membership operator:<\/strong><\/p>\n The ‘in’ operator is used to determine whether or not a value exists in a sequence. If it finds a variable in the given sequence, it returns true; otherwise, it returns false.<\/p>\n For Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ‘not in’ – Membership operator:<\/strong><\/p>\n If the ‘not in’ operator does not encounter a given data value in a sequence such as a list, string, etc., it returns True.<\/p>\n For Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The Identity operators in Python allow us to check the equivalence of values in terms of what memory location they are pointing to, have the same data type as expected, and so on.<\/p>\n ‘is’ – Identity operator:<\/strong><\/p>\n If the variables on either side of the operator point to the same object, the result is true; otherwise, the result is false.<\/p>\n For Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ‘is not’ – Identity operator:<\/strong><\/p>\n If the variables on either side of the operator point to the same object, the result is false; otherwise, the result is true.<\/p>\n For Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Python provides a number of operators for performing manipulation and operations on data values and variables on a larger scale. Let us see the two important types of Python operators: Membership Operators Identity Operators Membership Operators (‘in’, ‘not in’) Membership operators are used to validate a value’s membership. It checks for membership in a sequence, …<\/p>\n\n
# Give the first list as static input and store it in a variable.\r\ngvn_fstlst = [4, 7, 1, 15, 2]\r\n# Give the second list as static input and store it in another variable.\r\ngvn_scndlst = [7, 6, 2, 5]\r\n# Iterate in the given first list using the for loop and 'in' Operator\r\nfor numb in gvn_fstlst:\r\n # Check if the element present in the first list is also present in the\r\n # second list using the if conditional statement and 'in' Operator.\r\n if numb in gvn_scndlst:\r\n # If it is true, then print the respective element of the first list.\r\n print(numb, \"is common in both the lists\")\r\n<\/pre>\n
7 is common in both the lists\r\n2 is common in both the lists<\/pre>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [4, 7, 1, 15, 2]\r\n# Give the number as static input and store it in another variable.\r\ngvn_numb = 15\r\n# Check if the given number is not present in the given list using the\r\n# if conditional statement and 'not in' Operator.\r\nif gvn_numb not in gvn_lst:\r\n # If it is true, then print \"The given number is NOT present in the given list\"\r\n print(\"The given number\", gvn_numb, \"is NOT present in the given list\")\r\nelse:\r\n # Else print \"The given number is present in given the list\"\r\n print(\"The given number\", gvn_numb, \"is present in the given list\")\r\n<\/pre>\n
The given number 15 is present in the given list<\/pre>\n
Identity Operators (‘is’, ‘is not’)<\/h4>\n
\n
# Give the number as static input and store it in another variable.\r\ngvn_numb = 15.25\r\n# Check if the type of the given number is a float value using the\r\n# if conditional statement and 'is' identity\u00a0Operator.\r\nif type(gvn_numb) is float:\r\n # If it is true, then print \"The given number is a floating-point number\"\r\n print(\"The given number\", gvn_numb, \"is a floating-point number\")\r\nelse:\r\n # Else print \"The given number is NOT a floating-point number\"\r\n print(\"The given number\", gvn_numb, \"is NOT a floating-point number\")\r\n<\/pre>\n
The given number 15.25 is a floating-point number<\/pre>\n
\n
# Give the number as static input and store it in another variable.\r\ngvn_numb = 10\r\n# Check if the type of the given number is not a float value using the\r\n# if conditional statement and 'is not' identity Operator.\r\nif type(gvn_numb) is not float:\r\n # If it is true, then print \"The given number is NOT a floating-point number\"\r\n print(\"The given number\", gvn_numb, \"is NOT a floating-point number\")\r\nelse:\r\n # Else print \"The given number is a floating-point number\"\r\n print(\"The given number\", gvn_numb, \"is a floating-point number\")\r\n<\/pre>\n
The given number 10 is NOT a floating-point number<\/pre>\n