diff --git a/Project/Python Progs/dict.ipynb b/Project/Python Progs/dict.ipynb new file mode 100644 index 00000000..67c624a5 --- /dev/null +++ b/Project/Python Progs/dict.ipynb @@ -0,0 +1,496 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x= {\n", + " 'brand': \"Mahindra\",\n", + " 'model': 'XUV',\n", + " 'series': '500',\n", + " 'type': \"Diesel\",\n", + " \"fuel capacity\": 30,\n", + " 'mileage': 10,\n", + " 'color': 'white',\n", + " 'is_4wd': True,\n", + " 'colors' : ['Red','White','Black']\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "info = {} # empty dict" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- add items\n", + "- update items\n", + "- remove items\n", + "- traverse items" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "how to add data\n", + "```\n", + "dict['new_key'] = new_value\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'first_name': 'Henry'}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "info['first_name'] = \"Henry\"\n", + "info" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'first_name': 'Henry', 'last_name': 'Sugar', 'age': 45, 'city': 'New York'}\n" + ] + } + ], + "source": [ + "info['last_name'] = \"Sugar\"\n", + "info['age'] = 45\n", + "info['city'] = \"New York\"\n", + "print(info)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "how to update data\n", + "```\n", + "dict['existing_key'] = new_value\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "info['city'] = \"London\"\n", + "print(info)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "how to remove data\n", + "```\n", + "dict.pop('existing_key')\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "info.pop('age')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "info" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "extract keys and values\n", + "```\n", + "dict.keys()\n", + "```\n", + "```\n", + "dict.values()\n", + "```\n", + "```\n", + "dict.items()\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['first_name', 'last_name', 'age', 'city'])" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "info.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values(['Henry', 'Sugar', 45, 'New York'])" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "info.values()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('first_name', 'Henry'), ('last_name', 'Sugar'), ('age', 45), ('city', 'New York')])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "info.items()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "fetch data from dict\n", + "```\n", + "dict['key'] # throws error if key does not exist\n", + "```\n", + "```\n", + "dict.get('key') # returns None if key does not exist\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'x' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\pc\\OneDrive\\Desktop\\PWDS\\data structures\\dict.ipynb Cell 17\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> 1\u001b[0m x[\u001b[39m'\u001b[39m\u001b[39mbrand\u001b[39m\u001b[39m'\u001b[39m]\n", + "\u001b[1;31mNameError\u001b[0m: name 'x' is not defined" + ] + } + ], + "source": [ + "x['brand']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(x['mileage'])\n", + "print(x['colors'])\n", + "# print(x['price']) # KeyError" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(x.get('brand'))\n", + "print(x.get('model'))\n", + "print(x.get('colors'))\n", + "print(x.get('price'))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "traversing a dict" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for i in x:\n", + " print(i) # prints keys" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for i in x:\n", + " print(x[i]) # prints values" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for i in x:\n", + " print(i, x[i]) # prints keys and values" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# best way to iterate over a dict\n", + "for k, v in x.items():\n", + " print(k,'-->', v)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = {\n", + " \"sol_keys\": [],\n", + " \"validity_checks\": {\n", + " \"1219\": {\n", + " \"AT\": {\n", + " \"sol_hours_with_data\": [\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 11,\n", + " 12\n", + " ],\n", + " \"valid\": False\n", + " },\n", + " \"HWS\": {\n", + " \"sol_hours_with_data\": [\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 11,\n", + " 12\n", + " ],\n", + " \"valid\": False\n", + " },\n", + " \"PRE\": {\n", + " \"sol_hours_with_data\": [\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 11,\n", + " 12\n", + " ],\n", + " \"valid\": False\n", + " },\n", + " \"WD\": {\n", + " \"sol_hours_with_data\": [\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 11,\n", + " 12\n", + " ],\n", + " \"valid\": False\n", + " }\n", + " },\n", + " \"sol_hours_required\": 18,\n", + " \"sols_checked\": [\n", + " \"1219\"\n", + " ]\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for k, v in data.items():\n", + " print(k, '-->', v)\n", + " if isinstance(v , dict):\n", + " for k1, v1 in v.items():\n", + " print(k1, '-->', v1)\n", + " if isinstance(v1, dict):\n", + " for k2, v2 in v1.items():\n", + " print(k2, v2)" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "- sol_keys\n", + "\t []\n", + "- validity_checks\n", + "- 1219\n", + "- AT\n", + "- sol_hours_with_data\n", + "\t [6, 7, 8, 9, 10, 11, 12]\n", + "- valid\n", + "\t False\n", + "- HWS\n", + "- sol_hours_with_data\n", + "\t [6, 7, 8, 9, 10, 11, 12]\n", + "- valid\n", + "\t False\n", + "- PRE\n", + "- sol_hours_with_data\n", + "\t [6, 7, 8, 9, 10, 11, 12]\n", + "- valid\n", + "\t False\n", + "- WD\n", + "- sol_hours_with_data\n", + "\t [6, 7, 8, 9, 10, 11, 12]\n", + "- valid\n", + "\t False\n", + "- sol_hours_required\n", + "\t 18\n", + "- sols_checked\n", + "\t ['1219']\n" + ] + } + ], + "source": [ + "# recursive function\n", + "def extract(d):\n", + " for k, v in d.items():\n", + " print(f\"- {k}\")\n", + " if isinstance(v , dict):\n", + " extract(v)\n", + " else:\n", + " print('\\t', v)\n", + "\n", + "extract(data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.10.4" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Project/Python Progs/list.ipynb b/Project/Python Progs/list.ipynb new file mode 100644 index 00000000..2295a627 --- /dev/null +++ b/Project/Python Progs/list.ipynb @@ -0,0 +1,816 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- create\n", + "- indexing and slicing\n", + "- methods\n", + "- basics operations\n", + "- comprehension" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "x = [ ] # empty list\n", + "a = [1,2,3,4] # numerical list\n", + "b = ['alex', 'alexa', 'alexis'] # text list\n", + "c = ['alex', 12, 'alexa', 15, 'alexis', 10] # mixed list (heterogeneous)\n", + "d = [[1,2,3], [1,2], [2,3,4,4]] # nested list\n", + "e = [[1,2,3], [2,3,4], [1,2,2]] # nested list (equal length)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", + "[1, 2, 3, 1, 2, 3, 1, 2, 3]\n" + ] + } + ], + "source": [ + "w = [1] * 10 # list duplication (repetition)\n", + "print(w)\n", + "\n", + "w2 = [1,2,3] * 3 # list duplication (repetition)\n", + "print(w2)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6]\n", + "[[1, 2, 3], [4, 5, 6]]\n" + ] + } + ], + "source": [ + "a = [1,2,3]\n", + "b = [4,5,6]\n", + "c = a + b # list concatenation\n", + "print(c)\n", + "\n", + "d = [a, b] # nested list\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['one', 'for', 'all']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"one for all\".split() # split string into list" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n", + "[7, 8, 9, 10]\n", + "[4, 5, 6, 7]\n", + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n", + "[1, 3, 5, 7, 9]\n", + "[2, 4, 6, 8, 10]\n" + ] + } + ], + "source": [ + "x = [1,2,3,4,5,6,7,8,9,10]\n", + "print(x[:3]) # first 3 items\n", + "print(x[-4:]) # last 4 items\n", + "print(x[3:-3]) # middle items\n", + "\n", + "print(x)\n", + "print(x[::-1]) # reverse list\n", + "\n", + "print(x[::2]) # all elements with even index\n", + "print(x[1::2]) # all elements with odd index" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "10\n" + ] + } + ], + "source": [ + "print(x[0]) # first element\n", + "print(x[-1]) # last element\n", + "# print(x[12]) # error" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3 4 5 6 7 8 9 10 " + ] + } + ], + "source": [ + "# traverse list\n", + "for i in x:\n", + " print(i, end=' ')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "methods\n", + "- add\n", + " - append\n", + " - extend\n", + " - insert\n", + "- remove\n", + " - remove\n", + " - pop\n", + " - clear\n", + "- helper\n", + " - index\n", + " - count\n", + " - sort\n", + " - reverse\n", + " - copy" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['The Final Empire', 'The Well of Ascension', 'The Hero of Ages', 'Warbreaker']\n" + ] + } + ], + "source": [ + "books = []\n", + "books.append(\"The Final Empire\")\n", + "books.append(\"The Well of Ascension\")\n", + "books.append(\"The Hero of Ages\")\n", + "books.append(\"Warbreaker\")\n", + "print(books)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['The Final Empire', 'The Way of kings', 'The Well of Ascension', 'The Hero of Ages', 'Words of Radiance', 'Warbreaker']\n" + ] + } + ], + "source": [ + "books.insert(1, 'The Way of kings')\n", + "books.insert(4, 'Words of Radiance')\n", + "print(books)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Twilight Saga: New Moon', 'Twilight Saga: Eclipse', 'The Wheel of Time', 'The Great Hunt']\n" + ] + } + ], + "source": [ + "crappy_books = [\n", + " 'Twilight Saga: New Moon',\n", + " 'Twilight Saga: Eclipse',\n", + " 'The Wheel of Time',\n", + " 'The Great Hunt',\n", + "]\n", + "print(crappy_books)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['The Final Empire', 'The Way of kings', 'The Well of Ascension', 'The Hero of Ages', 'Words of Radiance', 'Warbreaker', 'Twilight Saga: New Moon', 'Twilight Saga: Eclipse', 'The Wheel of Time', 'The Great Hunt']\n" + ] + } + ], + "source": [ + "books.extend(crappy_books)\n", + "print(books)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7]\n" + ] + } + ], + "source": [ + "nums = [1,2,3]\n", + "more_nums = [4,5,6,7]\n", + "nums.extend(more_nums)\n", + "print(nums)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7]]\n" + ] + } + ], + "source": [ + "num_matrix = [nums, nums, nums]\n", + "print(num_matrix)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 7]\n" + ] + } + ], + "source": [ + "all_num = nums + more_nums\n", + "print(all_num)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['asdsa', 'asdas', 'as', 'sad', 'asd']\n" + ] + } + ], + "source": [ + "a = []\n", + "for i in range(5):\n", + " x = input('enter some data:')\n", + " a.append(x)\n", + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### take 5 inputs as numbers and then display the total from the user and the numbers in a list" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "total : 55\n" + ] + } + ], + "source": [ + "x = []\n", + "for i in range(5):\n", + " a = int(input(\"Koi number bharo:\"))\n", + " x.append(a)\n", + "print(f'total : {sum(x)}')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "books.remove(\"The Wheel of Time\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def safe_remove(val_to_remove):\n", + " if val_to_remove in books:\n", + " books.remove(val_to_remove)\n", + " print(f\"✅ {val_to_remove} removed from list\")\n", + " else:\n", + " print(f'❌{val_to_remove} not found in list')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "✅ Warbreaker removed from list\n" + ] + } + ], + "source": [ + "safe_remove('Warbreaker')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Words of Radiance'" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "books.pop() # remove last element" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Hero of Ages\n", + "['The Final Empire']\n" + ] + } + ], + "source": [ + "output = books.pop(1) # remove element at index 1\n", + "print(output)\n", + "print(books)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n" + ] + } + ], + "source": [ + "books.clear()\n", + "print(books)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n" + ] + } + ], + "source": [ + "x.clear()\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "del a" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "2\n", + "0\n" + ] + } + ], + "source": [ + "x = ['a1', 'b2', 'a1', 'a1', 'b2']\n", + "print(x.count('a1'))\n", + "print(x.count('b2'))\n", + "print(x.count('c2'))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a1', 'a1', 'a1', 'b2', 'b2']\n", + "['b2', 'b2', 'a1', 'a1', 'a1']\n" + ] + } + ], + "source": [ + "x.sort()\n", + "print(x)\n", + "x.sort(reverse=True)\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5, 4, 3, 2, 1]\n" + ] + } + ], + "source": [ + "a= [1,2,3,4,5]\n", + "a.reverse()\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['aeroplane', 'app', 'apple', 'association', 'avocado']\n", + "['avocado', 'association', 'apple', 'app', 'aeroplane']\n", + "['app', 'apple', 'avocado', 'aeroplane', 'association']\n", + "['association', 'aeroplane', 'avocado', 'apple', 'app']\n" + ] + } + ], + "source": [ + "words = ['app','apple','avocado','aeroplane','association']\n", + "words.sort()\n", + "print(words)\n", + "words.sort(reverse=True)\n", + "print(words)\n", + "words.sort(key=len) # sort by length\n", + "print(words)\n", + "words.sort(key=len, reverse=True) # sort by length , descending\n", + "print(words)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "words.index('app') # returns index of first occurence of element" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "word_copy1 = words # copy by reference\n", + "word_copy2 = words.copy() # copy by value" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "word_copy1 is words # True" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "word_copy2 is words # False" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 4]\n", + "[1, 2, 3, 4, 4]\n" + ] + } + ], + "source": [ + "x = [1,2,3]\n", + "y = x # copy by reference\n", + "x.append(4)\n", + "y.append(4)\n", + "print(x)\n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n", + "[1, 2, 3, 4]\n" + ] + } + ], + "source": [ + "x = [1,2,3]\n", + "y = x.copy() # copy by value\n", + "y.append(4)\n", + "print(x)\n", + "print(y)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "generating a list of numbers from existing list" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25]\n" + ] + } + ], + "source": [ + "x = [1,2,3,4,5]\n", + "x2 = []\n", + "for i in x:\n", + " s = i ** 2\n", + " x2.append(s)\n", + "print(x2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "list comprehension\n", + "syntax:\n", + "```\n", + "new_list = [operation for item in list]\n", + "```\n", + "list comprehension with condition\n", + "```\n", + "new_list = [ operation from item in list if expression ] \n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25]\n" + ] + } + ], + "source": [ + "x2 = [i**2 for i in x]\n", + "print(x2)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "[1, 3, 5, 7, 9]\n" + ] + } + ], + "source": [ + "a = [1,2,3,4,5,6,7,8,9,10]\n", + "a_odd = [i for i in a if i % 2 != 0]\n", + "print(a)\n", + "print(a_odd)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.11.5" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Project/Python Progs/readme.md b/Project/Python Progs/readme.md new file mode 100644 index 00000000..40dcd67b --- /dev/null +++ b/Project/Python Progs/readme.md @@ -0,0 +1,23 @@ +# data structures in python + +## list 💫 + - mutable + - indexed + - ordered + - can contain duplicates +## tuple + - immutable + - indexed + - ordered + - can contain duplicates +## set + - mutable + - unindexed + - unordered + - can contain only unique values +## dict 💫 + - mutable + - unindexed + - ordered + - can contain only unique keys + - can contain duplicates values diff --git a/Project/Python Progs/set.ipynb b/Project/Python Progs/set.ipynb new file mode 100644 index 00000000..c296f6aa --- /dev/null +++ b/Project/Python Progs/set.ipynb @@ -0,0 +1,309 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 3}\n", + "{1, 2, 3, 4, 5, 6}\n" + ] + } + ], + "source": [ + "x = {1,2,3}\n", + "print(x)\n", + "y = {1,2,5,5,1,2,4,1,5,6,3,3,1,1,1}\n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 3, 4, 5, 6, 7}\n" + ] + } + ], + "source": [ + "y.add(7)\n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1, 2, 3, 4, 5, 6}\n" + ] + } + ], + "source": [ + "y.discard(7) # discard() == remove()\n", + "print(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4, 5, 6}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x.union(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 5, 6, 8, 9, 10}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{1,2,3}.union({2,5,6,8,9,10})" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 3}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{1,2,3}.difference({2,5,6,8,9,10})" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{4, 5, 6}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y.difference(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3}" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x.intersection(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{2}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{1,2,3}.intersection({2,5,6,8,9,10})" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{4, 5, 6}" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x.symmetric_difference(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 3, 5, 6, 8, 9, 10}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{1,2,3}.symmetric_difference({2,5,6,8,9,10})" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x.issubset(y)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y.issubset({1,2,3,4,5})" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 12]\n" + ] + } + ], + "source": [ + "x = [1,2,3,4,6,1,2,3,3,4,5,1,1,1,12,2,2,5,5]\n", + "x = list(set(x))\n", + "print(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.11.5" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Project/Python Progs/tuples.ipynb b/Project/Python Progs/tuples.ipynb new file mode 100644 index 00000000..56053289 --- /dev/null +++ b/Project/Python Progs/tuples.ipynb @@ -0,0 +1,169 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 2, 3) \n" + ] + } + ], + "source": [ + "x = (1,2,3) # tuple\n", + "x\n", + "print(x, type(x))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 3, 5, 7, 9) \n" + ] + } + ], + "source": [ + "a = 1, 3, 5, 7, 9 # tuple packing\n", + "print(a, type(a))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(1, 2, 3)\n", + "1 2 3\n", + "1 [2, 3]\n", + "[1, 2] 3\n" + ] + } + ], + "source": [ + "t = (1,2,3)\n", + "a = t\n", + "print(a)\n", + "a, b, c = t # tuple unpacking\n", + "print(a, b, c)\n", + "a, *b = t # a gets 1st element, b gets the rest\n", + "print(a, b)\n", + "*a, b = t # a gets the rest, b gets the last element\n", + "print(a, b)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 9 [2, 3, 4, 5, 6, 7, 8]\n" + ] + } + ], + "source": [ + "t2 = (1,2,3,4,5,6,7,8,9)\n", + "x, *_, y = t2 # _ is a throwaway variable\n", + "print(x, y, _)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "tuple methods\n", + "- count\n", + "- index" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "0\n", + "2\n" + ] + } + ], + "source": [ + "t2 = (3,3,31,1,1,2,2,3)\n", + "print(t2.count(3)) # count the number of 3s in t2\n", + "print(t2.index(3)) # index of the first 3 in t2\n", + "print(t2.index(31)) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 [2, 3]\n" + ] + } + ], + "source": [ + "a = (1,2,3)\n", + "x1,x2,x3 = a\n", + "\n", + "x1, *x2 = a\n", + "print(x1, x2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.11.5" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}