|
| 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/04_Python_Functions/tree/main/002_Python_Functions_Built_in)**\n", |
| 9 | + "</i></small></small>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Python `any()`\n", |
| 17 | + "\n", |
| 18 | + "The **`any()`** function returns **`True`** if any element of an iterable is True. If not, **`any()`** returns **`False`**.\n", |
| 19 | + "\n", |
| 20 | + "**Syntax**:\n", |
| 21 | + "\n", |
| 22 | + "```python\n", |
| 23 | + "any(iterable)\n", |
| 24 | + "```" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## Parameters for the `any()` function\n", |
| 32 | + "\n", |
| 33 | + "The **`any()`** function takes an iterable (list, string, dictionary etc.) in Python." |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "## Value Returned by the `any()` function\n", |
| 41 | + "\n", |
| 42 | + "The **`any()`** function returns a boolean value:\n", |
| 43 | + "\n", |
| 44 | + "* **`True`** if at least one element of an iterable is true\n", |
| 45 | + "* **`False`** if all elements are false or if an iterable is empty\n", |
| 46 | + "\n", |
| 47 | + "| Condition | Return Value |\n", |
| 48 | + "|:----| :--- |\n", |
| 49 | + "| **All values are true** | **True** |\n", |
| 50 | + "| **All values are false** | **False** |\n", |
| 51 | + "| **One value is true (others are false)** | **True** |\n", |
| 52 | + "| **One value is false (others are true)** | **True** |\n", |
| 53 | + "| **Empty Iterable** | **False** |" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 1, |
| 59 | + "metadata": { |
| 60 | + "ExecuteTime": { |
| 61 | + "end_time": "2021-06-14T09:48:10.376600Z", |
| 62 | + "start_time": "2021-06-14T09:48:10.363419Z" |
| 63 | + } |
| 64 | + }, |
| 65 | + "outputs": [ |
| 66 | + { |
| 67 | + "name": "stdout", |
| 68 | + "output_type": "stream", |
| 69 | + "text": [ |
| 70 | + "True\n", |
| 71 | + "False\n", |
| 72 | + "True\n", |
| 73 | + "False\n" |
| 74 | + ] |
| 75 | + } |
| 76 | + ], |
| 77 | + "source": [ |
| 78 | + "# Example 1: Using any() on Python Lists\n", |
| 79 | + "\n", |
| 80 | + "# True since 1,3 and 4 (at least one) is true\n", |
| 81 | + "l = [1, 3, 4, 0]\n", |
| 82 | + "print(any(l))\n", |
| 83 | + "\n", |
| 84 | + "# False since both are False\n", |
| 85 | + "l = [0, False]\n", |
| 86 | + "print(any(l))\n", |
| 87 | + "\n", |
| 88 | + "# True since 5 is true\n", |
| 89 | + "l = [0, False, 5]\n", |
| 90 | + "print(any(l))\n", |
| 91 | + "\n", |
| 92 | + "# False since iterable is empty\n", |
| 93 | + "l = []\n", |
| 94 | + "print(any(l))" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "metadata": {}, |
| 100 | + "source": [ |
| 101 | + "The **`any()`** method works in a similar way for **[tuples](https://github.com/milaan9/02_Python_Datatypes/blob/main/004_Python_Tuple.ipynb)** and **[sets](https://github.com/milaan9/02_Python_Datatypes/blob/main/006_Python_Sets.ipynb)** like lists." |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": 2, |
| 107 | + "metadata": { |
| 108 | + "ExecuteTime": { |
| 109 | + "end_time": "2021-06-14T09:48:13.464474Z", |
| 110 | + "start_time": "2021-06-14T09:48:13.457639Z" |
| 111 | + } |
| 112 | + }, |
| 113 | + "outputs": [ |
| 114 | + { |
| 115 | + "name": "stdout", |
| 116 | + "output_type": "stream", |
| 117 | + "text": [ |
| 118 | + "True\n", |
| 119 | + "True\n", |
| 120 | + "False\n" |
| 121 | + ] |
| 122 | + } |
| 123 | + ], |
| 124 | + "source": [ |
| 125 | + "# Example 2: Using any() on Python Strings\n", |
| 126 | + "\n", |
| 127 | + "# Atleast one (in fact all) elements are True\n", |
| 128 | + "s = \"This is good\"\n", |
| 129 | + "print(any(s))\n", |
| 130 | + "\n", |
| 131 | + "# 0 is False because it is a non-iterable number\n", |
| 132 | + "# '0' is True because now it is not a number, it is a string character\n", |
| 133 | + "s = '000'\n", |
| 134 | + "print(any(s))\n", |
| 135 | + "\n", |
| 136 | + "# False since empty iterable\n", |
| 137 | + "s = ''\n", |
| 138 | + "print(any(s))" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": 3, |
| 144 | + "metadata": { |
| 145 | + "ExecuteTime": { |
| 146 | + "end_time": "2021-06-14T09:48:14.250605Z", |
| 147 | + "start_time": "2021-06-14T09:48:14.240838Z" |
| 148 | + } |
| 149 | + }, |
| 150 | + "outputs": [ |
| 151 | + { |
| 152 | + "name": "stdout", |
| 153 | + "output_type": "stream", |
| 154 | + "text": [ |
| 155 | + "False\n", |
| 156 | + "True\n", |
| 157 | + "False\n", |
| 158 | + "False\n", |
| 159 | + "True\n" |
| 160 | + ] |
| 161 | + } |
| 162 | + ], |
| 163 | + "source": [ |
| 164 | + "# Example 3: Using any() with Python Dictionaries\n", |
| 165 | + "\n", |
| 166 | + "# In the case of dictionaries, if all keys (not values) are false or the dictionary is empty, \n", |
| 167 | + "# `any()` returns `False`. If at least one key is true, `any()` returns `True`.\n", |
| 168 | + "\n", |
| 169 | + "# 0 is False\n", |
| 170 | + "d = {0: 'False'}\n", |
| 171 | + "print(any(d))\n", |
| 172 | + "\n", |
| 173 | + "# 1 is True\n", |
| 174 | + "d = {0: 'False', 1: 'True'}\n", |
| 175 | + "print(any(d))\n", |
| 176 | + "\n", |
| 177 | + "# 0 and False are false\n", |
| 178 | + "d = {0: 'False', False: 0}\n", |
| 179 | + "print(any(d))\n", |
| 180 | + "\n", |
| 181 | + "# iterable is empty\n", |
| 182 | + "d = {}\n", |
| 183 | + "print(any(d))\n", |
| 184 | + "\n", |
| 185 | + "# 0 is False\n", |
| 186 | + "# '0' is True\n", |
| 187 | + "d = {'0': 'False'}\n", |
| 188 | + "print(any(d))" |
| 189 | + ] |
| 190 | + }, |
| 191 | + { |
| 192 | + "cell_type": "code", |
| 193 | + "execution_count": null, |
| 194 | + "metadata": {}, |
| 195 | + "outputs": [], |
| 196 | + "source": [] |
| 197 | + } |
| 198 | + ], |
| 199 | + "metadata": { |
| 200 | + "hide_input": false, |
| 201 | + "kernelspec": { |
| 202 | + "display_name": "Python 3", |
| 203 | + "language": "python", |
| 204 | + "name": "python3" |
| 205 | + }, |
| 206 | + "language_info": { |
| 207 | + "codemirror_mode": { |
| 208 | + "name": "ipython", |
| 209 | + "version": 3 |
| 210 | + }, |
| 211 | + "file_extension": ".py", |
| 212 | + "mimetype": "text/x-python", |
| 213 | + "name": "python", |
| 214 | + "nbconvert_exporter": "python", |
| 215 | + "pygments_lexer": "ipython3", |
| 216 | + "version": "3.8.8" |
| 217 | + }, |
| 218 | + "toc": { |
| 219 | + "base_numbering": 1, |
| 220 | + "nav_menu": {}, |
| 221 | + "number_sections": true, |
| 222 | + "sideBar": true, |
| 223 | + "skip_h1_title": false, |
| 224 | + "title_cell": "Table of Contents", |
| 225 | + "title_sidebar": "Contents", |
| 226 | + "toc_cell": false, |
| 227 | + "toc_position": {}, |
| 228 | + "toc_section_display": true, |
| 229 | + "toc_window_display": false |
| 230 | + }, |
| 231 | + "varInspector": { |
| 232 | + "cols": { |
| 233 | + "lenName": 16, |
| 234 | + "lenType": 16, |
| 235 | + "lenVar": 40 |
| 236 | + }, |
| 237 | + "kernels_config": { |
| 238 | + "python": { |
| 239 | + "delete_cmd_postfix": "", |
| 240 | + "delete_cmd_prefix": "del ", |
| 241 | + "library": "var_list.py", |
| 242 | + "varRefreshCmd": "print(var_dic_list())" |
| 243 | + }, |
| 244 | + "r": { |
| 245 | + "delete_cmd_postfix": ") ", |
| 246 | + "delete_cmd_prefix": "rm(", |
| 247 | + "library": "var_list.r", |
| 248 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 249 | + } |
| 250 | + }, |
| 251 | + "types_to_exclude": [ |
| 252 | + "module", |
| 253 | + "function", |
| 254 | + "builtin_function_or_method", |
| 255 | + "instance", |
| 256 | + "_Feature" |
| 257 | + ], |
| 258 | + "window_display": false |
| 259 | + } |
| 260 | + }, |
| 261 | + "nbformat": 4, |
| 262 | + "nbformat_minor": 2 |
| 263 | +} |
0 commit comments