{"id":24710,"date":"2021-11-02T09:45:49","date_gmt":"2021-11-02T04:15:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=24710"},"modified":"2021-11-05T19:46:04","modified_gmt":"2021-11-05T14:16:04","slug":"python-tuple-count-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-tuple-count-method-with-examples\/","title":{"rendered":"Python Tuple count() method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python List sort() Method with Examples<\/a> Tuples are a data structure in Python that stores an ordered succession of values. They are unchangeable. This signifies that the values of a tuple cannot be changed. They allow you to save an organized list of items. A tuple, for example, can be used to hold a list of employee names.<\/p>\n Tuple count() method in Python:<\/strong><\/p>\n The count() function returns the number of occurrences of the particular element in the given tuple.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n The count() method only accepts one argument.<\/p>\n Return Value:<\/strong><\/p>\n The count() method returns the number of occurrences of the given element in the given tuple.<\/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 Approach:<\/strong><\/p>\n Example-1:<\/strong><\/p>\n String Tuple:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example-2:<\/strong><\/p>\n Integer Tuple:<\/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 Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python List sort() Method with Examples Tuples in Python: Tuples are a data structure in Python that stores an ordered succession of values. They are unchangeable. This signifies that the values of a tuple cannot be changed. They allow you to save an organized list of items. A …<\/p>\n
\nTuples in Python:<\/strong><\/p>\ngiventuple.count(gvnelement)<\/pre>\n
\n
Given Tuple = ('hello', 'this', 'is', 'python', 'is', 'programs')\r\nGiven Element = \"is\"<\/pre>\n
The count of the given element { is } in the given tuple ('hello', 'this', 'is', 'python', 'is', 'programs') is :\r\n2<\/pre>\n
Given tuple = (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28)\r\nGiven Element = 28<\/pre>\n
The count of the given element { 28 } in the given tuple (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28) is :\r\n4<\/pre>\n
Tuple count() method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the string tuple as static input and store it in a variable.\r\ngvntupl = ('hello', 'this', 'is', 'python', 'is', 'programs')\r\n# Give the element whose frequency is to be calculated as static input\r\n# and store it in another variable.\r\ngvnelemeent = \"is\"\r\n# Pass the given element above as the argument to the count function\r\n# for the given tuple and store the result in a variable say resltcnt\r\n# (It has the count of the given element in the given tuple).\r\nresltcnt = gvntupl.count(gvnelemeent)\r\n# Print the value of resltcnt.\r\nprint('The count of the given element {', gvnelemeent,\r\n '} in the given tuple', gvntupl, 'is :')\r\nprint(resltcnt)\r\n<\/pre>\n
The count of the given element { is } in the given tuple ('hello', 'this', 'is', 'python', 'is', 'programs') is :\r\n2<\/pre>\n
\n
# Give the Integer tuple as static input and store it in a variable.\r\ngvntupl = (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28)\r\n# Give the element whose frequency is to be calculated as static input\r\n# and store it in another variable.\r\ngvnelemeent = 28\r\n# Pass the given element above as the argument to the count function\r\n# for the given tuple and store the result in a variable say resltcnt\r\n# (It has the count of the given element in the given tuple).\r\nresltcnt = gvntupl.count(gvnelemeent)\r\n# Print the value of resltcnt.\r\nprint('The count of the given element {', gvnelemeent,\r\n '} in the given tuple', gvntupl, 'is :')\r\nprint(resltcnt)\r\n<\/pre>\n
The count of the given element { 28 } in the given tuple (11, 44, 13, 11, 73, 11, 28, 282, 28, 19, 28, 1, 28) is :\r\n4<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the Integer tuple as user input using tuple (),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvntupl = tuple(map(int, input('Enter some random tuple elements = ').split()))\r\n# Give the element as user input using the int(), input() functions,\r\n# and store it in another variable.\r\ngvnelemeent = int(input('Enter some random element = '))\r\n# Pass the given element above as the argument to the count function\r\n# for the given tuple and store the result in a variable say resltcnt\r\n# (It has the count of the given element in the given tuple).\r\nresltcnt = gvntupl.count(gvnelemeent)\r\n# Print the value of resltcnt.\r\nprint('The count of the given element {', gvnelemeent,\r\n '} in the given tuple', gvntupl, 'is :')\r\nprint(resltcnt)\r\n<\/pre>\n
Enter some random tuple elements = 12 7 1 8 3 2 1 7 1 1\r\nEnter some random element = 1\r\nThe count of the given element { 1 } in the given tuple (12, 7, 1, 8, 3, 2, 1, 7, 1, 1) is :\r\n4<\/pre>\n
\n