|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<small><small><i>\n", |
| 8 | + "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples)**\n", |
| 9 | + "</i></small></small>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Python Program to Swap Two Variables\n", |
| 17 | + "\n", |
| 18 | + "In this example, you will learn to swap two variables by using a temporary variable and, without using temporary variable.\n", |
| 19 | + "\n", |
| 20 | + "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", |
| 21 | + "\n", |
| 22 | + "* **[Python Data Types](https://github.com/milaan9/01_Python_Introduction/blob/main/009_Python_Data_Types.ipynb)**\n", |
| 23 | + "* **[Python Input, Output and Import](https://github.com/milaan9/01_Python_Introduction/blob/main/011_Python_Input_Output_Import.ipynb)**\n", |
| 24 | + "* **[Python Operators](https://github.com/milaan9/01_Python_Introduction/blob/main/012_Python_Operators.ipynb)**" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 1, |
| 30 | + "metadata": { |
| 31 | + "ExecuteTime": { |
| 32 | + "end_time": "2021-07-23T17:52:01.216853Z", |
| 33 | + "start_time": "2021-07-23T17:52:01.179745Z" |
| 34 | + } |
| 35 | + }, |
| 36 | + "outputs": [ |
| 37 | + { |
| 38 | + "name": "stdout", |
| 39 | + "output_type": "stream", |
| 40 | + "text": [ |
| 41 | + "The value of x after swapping: 9\n", |
| 42 | + "The value of y after swapping: 6\n" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "data": { |
| 47 | + "text/plain": [ |
| 48 | + "'\\n>>Output/Runtime Test Cases:\\n \\nThe value of x after swapping: 9\\nThe value of y after swapping: 6\\n'" |
| 49 | + ] |
| 50 | + }, |
| 51 | + "execution_count": 1, |
| 52 | + "metadata": {}, |
| 53 | + "output_type": "execute_result" |
| 54 | + } |
| 55 | + ], |
| 56 | + "source": [ |
| 57 | + "#Example: Swap two variables\n", |
| 58 | + "\n", |
| 59 | + "x = 6\n", |
| 60 | + "y = 9\n", |
| 61 | + "\n", |
| 62 | + "# To take inputs from the user\n", |
| 63 | + "#x = input('Enter value of x: ')\n", |
| 64 | + "#y = input('Enter value of y: ')\n", |
| 65 | + "\n", |
| 66 | + "# create a temporary variable and swap the values\n", |
| 67 | + "temp = x\n", |
| 68 | + "x = y\n", |
| 69 | + "y = temp\n", |
| 70 | + "\n", |
| 71 | + "print('The value of x after swapping: {}'.format(x))\n", |
| 72 | + "print('The value of y after swapping: {}'.format(y))\n", |
| 73 | + "\n", |
| 74 | + "'''\n", |
| 75 | + ">>Output/Runtime Test Cases:\n", |
| 76 | + " \n", |
| 77 | + "The value of x after swapping: 9\n", |
| 78 | + "The value of y after swapping: 6\n", |
| 79 | + "'''" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "**Explanation:**\n", |
| 87 | + " \n", |
| 88 | + "In this program, we use the **`temp`** variable to hold the value of **`x`** temporarily. We then put the value of **`y`** in **`x`** and later **`temp`** in **`y`**. In this way, the values get exchanged." |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "## Source Code: Without Using Temporary Variable\n", |
| 96 | + "\n", |
| 97 | + "In Python, there is a simple construct to swap variables. The following code does the same as above but without the use of any temporary variable." |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": 2, |
| 103 | + "metadata": { |
| 104 | + "ExecuteTime": { |
| 105 | + "end_time": "2021-07-23T17:52:01.366265Z", |
| 106 | + "start_time": "2021-07-23T17:52:01.219783Z" |
| 107 | + } |
| 108 | + }, |
| 109 | + "outputs": [ |
| 110 | + { |
| 111 | + "name": "stdout", |
| 112 | + "output_type": "stream", |
| 113 | + "text": [ |
| 114 | + "x = 9\n", |
| 115 | + "y = 6\n" |
| 116 | + ] |
| 117 | + } |
| 118 | + ], |
| 119 | + "source": [ |
| 120 | + "x = 6\n", |
| 121 | + "y = 9\n", |
| 122 | + "\n", |
| 123 | + "x, y = y, x\n", |
| 124 | + "print(\"x =\", x)\n", |
| 125 | + "print(\"y =\", y)" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "markdown", |
| 130 | + "metadata": {}, |
| 131 | + "source": [ |
| 132 | + "If the variables are both numbers, we can use arithmetic operations to do the same. It might not look intuitive at first sight. But if you think about it, it is pretty easy to figure it out. Here are a few examples:" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "### Addition and Subtraction" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": 3, |
| 145 | + "metadata": { |
| 146 | + "ExecuteTime": { |
| 147 | + "end_time": "2021-07-23T17:52:01.514707Z", |
| 148 | + "start_time": "2021-07-23T17:52:01.371149Z" |
| 149 | + } |
| 150 | + }, |
| 151 | + "outputs": [ |
| 152 | + { |
| 153 | + "name": "stdout", |
| 154 | + "output_type": "stream", |
| 155 | + "text": [ |
| 156 | + "x = 9\n", |
| 157 | + "y = 6\n" |
| 158 | + ] |
| 159 | + } |
| 160 | + ], |
| 161 | + "source": [ |
| 162 | + "x = 6\n", |
| 163 | + "y = 9\n", |
| 164 | + "\n", |
| 165 | + "# Addition and Subtraction\n", |
| 166 | + "x = x + y\n", |
| 167 | + "y = x - y\n", |
| 168 | + "x = x - y\n", |
| 169 | + "print(\"x =\", x)\n", |
| 170 | + "print(\"y =\", y)" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "markdown", |
| 175 | + "metadata": {}, |
| 176 | + "source": [ |
| 177 | + "### Multiplication and Division" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "code", |
| 182 | + "execution_count": 4, |
| 183 | + "metadata": { |
| 184 | + "ExecuteTime": { |
| 185 | + "end_time": "2021-07-23T17:52:01.665096Z", |
| 186 | + "start_time": "2021-07-23T17:52:01.519588Z" |
| 187 | + }, |
| 188 | + "scrolled": true |
| 189 | + }, |
| 190 | + "outputs": [ |
| 191 | + { |
| 192 | + "name": "stdout", |
| 193 | + "output_type": "stream", |
| 194 | + "text": [ |
| 195 | + "x = 9.0\n", |
| 196 | + "y = 6.0\n" |
| 197 | + ] |
| 198 | + } |
| 199 | + ], |
| 200 | + "source": [ |
| 201 | + "x = 6\n", |
| 202 | + "y = 9\n", |
| 203 | + "\n", |
| 204 | + "# Multiplication and Division\n", |
| 205 | + "x = x * y\n", |
| 206 | + "y = x / y\n", |
| 207 | + "x = x / y\n", |
| 208 | + "print(\"x =\", x)\n", |
| 209 | + "print(\"y =\", y)" |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + "cell_type": "markdown", |
| 214 | + "metadata": {}, |
| 215 | + "source": [ |
| 216 | + "### XOR swap\n", |
| 217 | + "\n", |
| 218 | + "This algorithm works for integers only" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "code", |
| 223 | + "execution_count": 5, |
| 224 | + "metadata": { |
| 225 | + "ExecuteTime": { |
| 226 | + "end_time": "2021-07-23T17:52:01.801812Z", |
| 227 | + "start_time": "2021-07-23T17:52:01.668999Z" |
| 228 | + } |
| 229 | + }, |
| 230 | + "outputs": [ |
| 231 | + { |
| 232 | + "name": "stdout", |
| 233 | + "output_type": "stream", |
| 234 | + "text": [ |
| 235 | + "x = 9\n", |
| 236 | + "y = 6\n" |
| 237 | + ] |
| 238 | + } |
| 239 | + ], |
| 240 | + "source": [ |
| 241 | + "x = 6\n", |
| 242 | + "y = 9\n", |
| 243 | + "\n", |
| 244 | + "# XOR swap: This algorithm works for integers only\n", |
| 245 | + "x = x ^ y\n", |
| 246 | + "y = x ^ y\n", |
| 247 | + "x = x ^ y\n", |
| 248 | + "print(\"x =\", x)\n", |
| 249 | + "print(\"y =\", y)" |
| 250 | + ] |
| 251 | + }, |
| 252 | + { |
| 253 | + "cell_type": "code", |
| 254 | + "execution_count": null, |
| 255 | + "metadata": {}, |
| 256 | + "outputs": [], |
| 257 | + "source": [] |
| 258 | + } |
| 259 | + ], |
| 260 | + "metadata": { |
| 261 | + "hide_input": false, |
| 262 | + "kernelspec": { |
| 263 | + "display_name": "Python 3", |
| 264 | + "language": "python", |
| 265 | + "name": "python3" |
| 266 | + }, |
| 267 | + "language_info": { |
| 268 | + "codemirror_mode": { |
| 269 | + "name": "ipython", |
| 270 | + "version": 3 |
| 271 | + }, |
| 272 | + "file_extension": ".py", |
| 273 | + "mimetype": "text/x-python", |
| 274 | + "name": "python", |
| 275 | + "nbconvert_exporter": "python", |
| 276 | + "pygments_lexer": "ipython3", |
| 277 | + "version": "3.8.8" |
| 278 | + }, |
| 279 | + "toc": { |
| 280 | + "base_numbering": 1, |
| 281 | + "nav_menu": {}, |
| 282 | + "number_sections": true, |
| 283 | + "sideBar": true, |
| 284 | + "skip_h1_title": false, |
| 285 | + "title_cell": "Table of Contents", |
| 286 | + "title_sidebar": "Contents", |
| 287 | + "toc_cell": false, |
| 288 | + "toc_position": {}, |
| 289 | + "toc_section_display": true, |
| 290 | + "toc_window_display": false |
| 291 | + }, |
| 292 | + "varInspector": { |
| 293 | + "cols": { |
| 294 | + "lenName": 16, |
| 295 | + "lenType": 16, |
| 296 | + "lenVar": 40 |
| 297 | + }, |
| 298 | + "kernels_config": { |
| 299 | + "python": { |
| 300 | + "delete_cmd_postfix": "", |
| 301 | + "delete_cmd_prefix": "del ", |
| 302 | + "library": "var_list.py", |
| 303 | + "varRefreshCmd": "print(var_dic_list())" |
| 304 | + }, |
| 305 | + "r": { |
| 306 | + "delete_cmd_postfix": ") ", |
| 307 | + "delete_cmd_prefix": "rm(", |
| 308 | + "library": "var_list.r", |
| 309 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 310 | + } |
| 311 | + }, |
| 312 | + "types_to_exclude": [ |
| 313 | + "module", |
| 314 | + "function", |
| 315 | + "builtin_function_or_method", |
| 316 | + "instance", |
| 317 | + "_Feature" |
| 318 | + ], |
| 319 | + "window_display": false |
| 320 | + } |
| 321 | + }, |
| 322 | + "nbformat": 4, |
| 323 | + "nbformat_minor": 4 |
| 324 | +} |
0 commit comments