|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<small><small><i>\n", |
| 8 | + "All the IPython Notebooks in this lecture series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/02_Python_Datatypes/tree/main/004_Python_Tuple_Methods)**\n", |
| 9 | + "</i></small></small>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Python Tuple `count()`\n", |
| 17 | + "\n", |
| 18 | + "The **`count()`** method returns the number of times the specified element appears in the tuple.\n", |
| 19 | + "\n", |
| 20 | + "**Syntax**:\n", |
| 21 | + "\n", |
| 22 | + "```python\n", |
| 23 | + "tuple.count(element)\n", |
| 24 | + "```" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## `count()` Parameters\n", |
| 32 | + "\n", |
| 33 | + "The tuple **`count()`** method takes a single argument:\n", |
| 34 | + "\n", |
| 35 | + "* **element** - the element to be counted" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "## Return Value from `count()`\n", |
| 43 | + "\n", |
| 44 | + "The **`count()`** method returns the number of times **`element`** appears in the tuple." |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 1, |
| 50 | + "metadata": { |
| 51 | + "ExecuteTime": { |
| 52 | + "end_time": "2021-06-09T14:25:45.608647Z", |
| 53 | + "start_time": "2021-06-09T14:25:45.595986Z" |
| 54 | + } |
| 55 | + }, |
| 56 | + "outputs": [ |
| 57 | + { |
| 58 | + "name": "stdout", |
| 59 | + "output_type": "stream", |
| 60 | + "text": [ |
| 61 | + "The count of i is: 2\n", |
| 62 | + "The count of p is: 0\n" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + "source": [ |
| 67 | + "# Example 1: Use of Tuple count()\n", |
| 68 | + "\n", |
| 69 | + "# vowels tuple\n", |
| 70 | + "vowels = ('a', 'e', 'i', 'o', 'i', 'u')\n", |
| 71 | + "\n", |
| 72 | + "# count element 'i'\n", |
| 73 | + "count = vowels.count('i')\n", |
| 74 | + "\n", |
| 75 | + "# print count\n", |
| 76 | + "print('The count of i is:', count)\n", |
| 77 | + "\n", |
| 78 | + "# count element 'p'\n", |
| 79 | + "count = vowels.count('p')\n", |
| 80 | + "\n", |
| 81 | + "# print count\n", |
| 82 | + "print('The count of p is:', count)" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": 2, |
| 88 | + "metadata": { |
| 89 | + "ExecuteTime": { |
| 90 | + "end_time": "2021-06-09T14:25:46.903830Z", |
| 91 | + "start_time": "2021-06-09T14:25:46.891169Z" |
| 92 | + } |
| 93 | + }, |
| 94 | + "outputs": [ |
| 95 | + { |
| 96 | + "name": "stdout", |
| 97 | + "output_type": "stream", |
| 98 | + "text": [ |
| 99 | + "The count of ('a', 'b') is: 2\n", |
| 100 | + "The count of [3, 4] is: 1\n" |
| 101 | + ] |
| 102 | + } |
| 103 | + ], |
| 104 | + "source": [ |
| 105 | + "# Example 2: Count List and Tuple Elements Inside Tuple\n", |
| 106 | + "\n", |
| 107 | + "# random tuple\n", |
| 108 | + "random = ('a', ('a', 'b'), ('a', 'b'), [3, 4])\n", |
| 109 | + "\n", |
| 110 | + "# count element ('a', 'b')\n", |
| 111 | + "count = random.count(('a', 'b'))\n", |
| 112 | + "\n", |
| 113 | + "# print count\n", |
| 114 | + "print(\"The count of ('a', 'b') is:\", count)\n", |
| 115 | + "\n", |
| 116 | + "# count element [3, 4]\n", |
| 117 | + "count = random.count([3, 4])\n", |
| 118 | + "\n", |
| 119 | + "# print count\n", |
| 120 | + "print(\"The count of [3, 4] is:\", count)" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": null, |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [], |
| 128 | + "source": [] |
| 129 | + } |
| 130 | + ], |
| 131 | + "metadata": { |
| 132 | + "hide_input": false, |
| 133 | + "kernelspec": { |
| 134 | + "display_name": "Python 3", |
| 135 | + "language": "python", |
| 136 | + "name": "python3" |
| 137 | + }, |
| 138 | + "language_info": { |
| 139 | + "codemirror_mode": { |
| 140 | + "name": "ipython", |
| 141 | + "version": 3 |
| 142 | + }, |
| 143 | + "file_extension": ".py", |
| 144 | + "mimetype": "text/x-python", |
| 145 | + "name": "python", |
| 146 | + "nbconvert_exporter": "python", |
| 147 | + "pygments_lexer": "ipython3", |
| 148 | + "version": "3.8.3" |
| 149 | + }, |
| 150 | + "varInspector": { |
| 151 | + "cols": { |
| 152 | + "lenName": 16, |
| 153 | + "lenType": 16, |
| 154 | + "lenVar": 40 |
| 155 | + }, |
| 156 | + "kernels_config": { |
| 157 | + "python": { |
| 158 | + "delete_cmd_postfix": "", |
| 159 | + "delete_cmd_prefix": "del ", |
| 160 | + "library": "var_list.py", |
| 161 | + "varRefreshCmd": "print(var_dic_list())" |
| 162 | + }, |
| 163 | + "r": { |
| 164 | + "delete_cmd_postfix": ") ", |
| 165 | + "delete_cmd_prefix": "rm(", |
| 166 | + "library": "var_list.r", |
| 167 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 168 | + } |
| 169 | + }, |
| 170 | + "types_to_exclude": [ |
| 171 | + "module", |
| 172 | + "function", |
| 173 | + "builtin_function_or_method", |
| 174 | + "instance", |
| 175 | + "_Feature" |
| 176 | + ], |
| 177 | + "window_display": false |
| 178 | + } |
| 179 | + }, |
| 180 | + "nbformat": 4, |
| 181 | + "nbformat_minor": 2 |
| 182 | +} |
0 commit comments