{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Alternative to if-else" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:47:15.487181Z", "start_time": "2020-07-31T16:47:15.480570Z" } }, "outputs": [], "source": [ "price_list = {\n", "'fish': 8,\n", "'beef': 7,\n", "'broccoli': 3,\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:47:27.087800Z", "start_time": "2020-07-31T16:47:27.084668Z" } }, "outputs": [], "source": [ "# query the dictionary twice -> not efficient\n", "def find_price(item):\n", " if item in price_list:\n", " return 'The price for {} is {}'.format(item, price_list[item])\n", " else:\n", " return 'The price for {} is not available'.format(item)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:48:37.957154Z", "start_time": "2020-07-31T16:48:37.954888Z" } }, "outputs": [], "source": [ "# .get() looks up a key and returns default value with the non-existent key\n", "def find_price(item):\n", " return 'The price for {} is {}'.format(item, price_list.get(\n", " item, 'not available'))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:48:47.750479Z", "start_time": "2020-07-31T16:48:47.740268Z" } }, "outputs": [ { "data": { "text/plain": [ "'The price for fish is 8'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "find_price('fish')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:48:54.154690Z", "start_time": "2020-07-31T16:48:54.151764Z" } }, "outputs": [ { "data": { "text/plain": [ "'The price for cauliflower is not available'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# returns default value with the non-existent key\n", "find_price('cauliflower')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Operations" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:50:38.045841Z", "start_time": "2020-07-31T16:50:38.036354Z" } }, "outputs": [], "source": [ "# okay\n", "def operations(operator, x, y):\n", " if operator == 'add':\n", " return x + y\n", " elif operator == 'sub':\n", " return x - y\n", " elif operator == 'mul':\n", " return x * y\n", " elif operator == 'div':\n", " return x / y" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:50:27.758351Z", "start_time": "2020-07-31T16:50:27.752404Z" } }, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "operations('mul', 2, 8)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:50:52.856394Z", "start_time": "2020-07-31T16:50:52.847022Z" } }, "outputs": [], "source": [ "# better\n", "def operations(operator, x, y):\n", " return {\n", " 'add': lambda: x+y,\n", " 'sub': lambda: x-y,\n", " 'mul': lambda: x*y,\n", " 'div': lambda: x/y,\n", " }.get(operator, lambda: 'Not a valid operation')()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:50:56.854501Z", "start_time": "2020-07-31T16:50:56.851129Z" } }, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "operations('mul', 2, 8)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:51:02.739125Z", "start_time": "2020-07-31T16:51:02.736367Z" } }, "outputs": [ { "data": { "text/plain": [ "'Not a valid operation'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "operations('unknown', 2, 8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }