{"id":23998,"date":"2021-10-14T09:16:31","date_gmt":"2021-10-14T03:46:31","guid":{"rendered":"https:\/\/python-programs.com\/?p=23998"},"modified":"2021-11-05T16:48:25","modified_gmt":"2021-11-05T11:18:25","slug":"python-program-for-set-len-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-set-len-method\/","title":{"rendered":"Python Program for Set len() Method"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Set sorted() Method<\/a> Python set is a list of items that are not ordered. \u2013 Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.<\/p>\n The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n set len() Method:<\/strong><\/p>\n The set len() method is one of the set methods for determining the length of a set i.e, the total number of items present in the set.<\/p>\n Syntax:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Python Set Methods Examples: Understanding Python Set Methods without any practice is a bit difficult but going through this Python Set Methods Examples<\/a> tutorial will definitely aid programmers to learn easily & efficiently.<\/p>\n In the previous article, we have discussed Python Program for Set sorted() Method Python set: Python set is a list of items that are not ordered. \u2013 Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after …<\/p>\n
\nPython set:<\/strong><\/p>\nGiven set = {'hello', 'this', 'is', 'btechgeeks'}<\/pre>\n
The given set is :\r\n{'is', 'btechgeeks', 'hello', 'this'}\r\nThe given set's length = 4<\/pre>\n
Given set = {34, 2, 56, 12, 1, 4, 2, 5}<\/pre>\n
The given set is :\r\n{1, 34, 2, 4, 5, 12, 56}\r\nThe given set's length = 7<\/pre>\n
Here the total items present in the set is 8, but set doesn't allow duplicate elements. \r\nHere 2 is the duplicate element. so, it gives the length of set as 7.<\/pre>\n
Program for Set len() Method in Python<\/h2>\n
\n
len(set)<\/pre>\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the set as static input and initialize it with some random values.\r\n# Store it in a variable.\r\ngven_set = {'hello', 'this', 'is', 'btechgeeks'}\r\n# Print the above-given set.\r\nprint(\"The given set is :\")\r\nprint(gven_set)\r\n# Apply len() method to the given set to get the total number of items present\r\n# (length) in the given set.\r\n# Store it in another variable.\r\ngvn_setlengt = len(gven_set)\r\n# Print the length of the given set.\r\nprint(\"The given set's length = \", gvn_setlengt)\r\n<\/pre>\n
The given set is :\r\n{'is', 'btechgeeks', 'hello', 'this'}\r\nThe given set's length = 4<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the set as user input using the set(),map(), input(), and split() functions.\r\n# Store it in a variable.\r\ngven_set = set(map\r\n(int,input(\"Enter some random values separated by spaces = \").split()))\r\n# Print the above-given set.\r\nprint(\"The given set is :\")\r\nprint(gven_set)\r\n# Apply len() method to the given set to get the total number of items present\r\n# (length) in the given set.\r\n# Store it in another variable.\r\ngvn_setlengt = len(gven_set)\r\n# Print the length of the given set.\r\nprint(\"The given set's length = \", gvn_setlengt)\r\n\r\n<\/pre>\n
Enter some random values separated by spaces = 34 2 56 12 1 4 2 5\r\nThe given set is :\r\n{1, 34, 2, 4, 5, 12, 56}\r\nThe given set's length = 7<\/pre>\n
\n