From 3652d8018ca88b09ebf2009a93e6021d3dfcec5d Mon Sep 17 00:00:00 2001 From: Rea1103 <61258371+Rea1103@users.noreply.github.com> Date: Thu, 15 Oct 2020 23:20:42 +0800 Subject: [PATCH 1/3] Update Python06_Array_&_Dictionary.ipynb Created a summary table for lists and fixed some code cells. --- .../Python06_Array_&_Dictionary.ipynb | 1924 +++++++++-------- 1 file changed, 988 insertions(+), 936 deletions(-) diff --git a/computing_sh/Python06_Array_&_Dictionary.ipynb b/computing_sh/Python06_Array_&_Dictionary.ipynb index d2132f1..3d0ae13 100644 --- a/computing_sh/Python06_Array_&_Dictionary.ipynb +++ b/computing_sh/Python06_Array_&_Dictionary.ipynb @@ -1,1002 +1,1054 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "Python06 - Array & Dictionary.ipynb", - "provenance": [], - "collapsed_sections": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "_sIeTutwNGJK" + }, + "source": [ + "# **Data Structures**\n", + "\n", + "\n", + "* List / Array\n", + "* Dictionary\n", + "\n", + "Create / Insert / Add \n", + "Read / Select / Display \n", + "Update / Edit \n", + "Delete / Remove \n" + ] }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "_sIeTutwNGJK" - }, - "source": [ - "# **Data Structures**\n", - "\n", - "\n", - "* List / Array\n", - "* Dictionary\n", - "\n", - "Create / Insert / Add \n", - "Read / Select / Display \n", - "Update / Edit \n", - "Delete / Remove \n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "TgbEMstMxCHY" - }, - "source": [ - "## **List / Array**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "OgPVcGHKvcTO", - "outputId": "b58eef75-6484-4332-e72a-0344a2441428", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 - } - }, - "source": [ - "str1 = 'Tom' # immutable\n", - "list1 = ['T', 'o', 'm'] # mutable\n", - "print(len(str1))\n", - "print(len(list1))" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "3\n", - "3\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "UaACnox-xfVh", - "outputId": "e8733ac9-6f7d-464a-d57d-849fb0cd1db2", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "list1[0] = 'M'\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['M', 'o', 'm']" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 52 - } - ] - }, + { + "cell_type": "markdown", + "metadata": { + "id": "TgbEMstMxCHY" + }, + "source": [ + "## **List / Array**" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "4oEs6a8fxrNj", - "outputId": "a445ce15-ecb2-47f1-e1be-3522942c13f3", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 85 - } - }, - "source": [ - "print(str1[0])\n", - "print(list1[0])\n", - "print(str1[-1])\n", - "print(list1[-1])" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "T\n", - "M\n", - "m\n", - "m\n" - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "[]" ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Creating empty lists\n", + "list1 = [] # method 1\n", + "list1 = list() # method 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 }, + "id": "OgPVcGHKvcTO", + "outputId": "b58eef75-6484-4332-e72a-0344a2441428" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "pyu0Wm08x7rW", - "outputId": "31b73ad0-711d-437c-e5e6-e3e5d77e1c37", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - } - }, - "source": [ - "for char in list1:\n", - " print(char)" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "M\n", - "o\n", - "m\n" - ], - "name": "stdout" - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "3\n" + ] + } + ], + "source": [ + "str1 = 'Tom' # immutable\n", + "list1 = ['T', 'o', 'm'] # mutable\n", + "print(len(str1))\n", + "print(len(list1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "UaACnox-xfVh", + "outputId": "e8733ac9-6f7d-464a-d57d-849fb0cd1db2" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "cFPui8iHyDYJ", - "outputId": "cd5b3641-a97f-4049-b1a7-556539e90a58", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "list1 = [] # create empty array / list\n", - "list1\n", - "len(list1) # find length\n", - "list1.append('B') # add to the back of array / list\n", - "list1\n", - "list1.append('a')\n", - "list1\n", - "list1.append(3.14)\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['B', 'a', 3.14]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 5 - } + "data": { + "text/plain": [ + "['M', 'o', 'm']" ] + }, + "execution_count": 52, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "list1[0] = 'M'\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 }, + "id": "4oEs6a8fxrNj", + "outputId": "a445ce15-ecb2-47f1-e1be-3522942c13f3" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "B59CDddPyKu6", - "outputId": "0bb0b53a-5091-4f7a-d0c1-78edbf80e8cc", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "list1.insert(1, 'Yo') # add to any position in an array\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['B', 'Yo', 'a', 3.14]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 6 - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "T\n", + "M\n", + "m\n", + "m\n" + ] + } + ], + "source": [ + "print(str1[0])\n", + "print(list1[0])\n", + "print(str1[-1])\n", + "print(list1[-1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 }, + "id": "pyu0Wm08x7rW", + "outputId": "31b73ad0-711d-437c-e5e6-e3e5d77e1c37" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "bVMwgFDMytfX", - "outputId": "3375e227-ad95-4df3-b91e-31b16991af47", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Update\n", - "list1[2] = 35\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['B', 'Yo', 35, 3.14]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 7 - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "M\n", + "o\n", + "m\n" + ] + } + ], + "source": [ + "for char in list1:\n", + " print(char)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### A quick overview of list operations: \n", + "\n", + "| Operation | Parameter | Return |\n", + "| :--------- | :-------- | :------ |\n", + "| len(list) | List | The number of items in a list. |\n", + "| list.append() | An object that has the len of one | Returns None but adds a single element to a list. |\n", + "| list.insert() | Specified index and an object that has the len of one | Returns None but adds the single element to a list based on the index specified. |\n", + "| list.remove(arg) | Any value present in the list | Returns None but removes the first matching element (which is passed as an argument) from the list. |\n", + "| del list[] | Index or range of the value wanted to be deleted from the list | Returns None but deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments. |\n", + "\n", + "*Note that this list is non-exhaustive*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "cFPui8iHyDYJ", + "outputId": "cd5b3641-a97f-4049-b1a7-556539e90a58" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "Ksq7eVuoombD", - "outputId": "0f0cafbe-f5f1-4e34-f593-2eff55987181", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Delete using remove\n", - "list1.remove(3.14)\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['B', 'Yo', 35]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 10 - } + "data": { + "text/plain": [ + "['B', 'a', 3.14]" ] + }, + "execution_count": 5, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "list1 = [] # create empty array / list\n", + "list1\n", + "len(list1) # find length\n", + "list1.append('B') # add to the back of array / list\n", + "list1\n", + "list1.append('a')\n", + "list1\n", + "list1.append(3.14)\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "B59CDddPyKu6", + "outputId": "0bb0b53a-5091-4f7a-d0c1-78edbf80e8cc" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "JZ69baoEpYjv", - "outputId": "d28095d5-15b7-49a0-dfde-cfc5a7d0ceda", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Delete using del\n", - "del list1[0]\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['Yo', 35]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 11 - } + "data": { + "text/plain": [ + "['B', 'Yo', 'a', 3.14]" ] + }, + "execution_count": 6, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "list1.insert(1, 'Yo') # add to any position in an array\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "bVMwgFDMytfX", + "outputId": "3375e227-ad95-4df3-b91e-31b16991af47" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "_c-1OlUVptye", - "outputId": "4ca98ffa-b0cf-4c48-9159-5fd4f4c4d415", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Read\n", - "for item in list1:\n", - " print(item, end=' ')" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Yo 35 " - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "['B', 'Yo', 35, 3.14]" ] + }, + "execution_count": 7, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Update\n", + "list1[2] = 35\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "Ksq7eVuoombD", + "outputId": "0f0cafbe-f5f1-4e34-f593-2eff55987181" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "-sTFYfcCp4bt", - "outputId": "ee213726-b4c7-434f-af86-5b021b633e77", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 257 - } - }, - "source": [ - "# Program to CRUD list / array\n", - "\n", - "def menu():\n", - " print(\"(1) Insert item\")\n", - " print(\"(2) Update item\")\n", - " print(\"(3) Delete item\")\n", - " print(\"(4) Show all items\")\n", - " print(\"(0) Quit\")\n", - "\n", - "def insert_item(score):\n", - " print(\"Inserting item...\")\n", - " marks.append(score)\n", - "\n", - "def update_item(old_mark):\n", - " print(\"Updating item...\")\n", - " for i in range(len(marks)):\n", - " if marks[i] == old_mark:\n", - " new_mark = int(input(\"Enter new mark: \"))\n", - " marks[i] = new_mark\n", - "\n", - "def delete_item(score):\n", - " print(\"Deleting item...\")\n", - " if score in marks:\n", - " marks.remove(score)\n", - " else:\n", - " print(\"Cannot delete non-existent item\")\n", - "\n", - "def show_items():\n", - " print(\"All items: \", end='')\n", - " if marks != []: # if len(marks) > 0:\n", - " for mark in marks:\n", - " print(mark, end=' ')\n", - " print()\n", - " else: # empty list\n", - " print(\"No marks!\")\n", - "\n", - "# main\n", - "# initialise variables\n", - "choice = ''\n", - "marks = [] # create empty list (global variable)\n", - "#marks = [88, 100, 75] # for testing\n", - "while choice != '0': # while not quit\n", - " menu()\n", - " choice = input(\"Enter choice: \")\n", - " if choice == '1': # insert item\n", - " mark = int(input(\"Enter mark to insert: \"))\n", - " insert_item(mark)\n", - " elif choice == '2': # update item\n", - " mark = int(input(\"Enter mark to update: \"))\n", - " update_item(mark)\n", - " elif choice == '3': # delete item\n", - " mark = int(input(\"Enter mark to delete: \"))\n", - " delete_item(mark)\n", - " elif choice == '4': # show all items\n", - " show_items()\n", - " elif choice == '0':\n", - " print(\"Bye\")\n", - " else: # invalid choice\n", - " print(\"Yo! Choose a valid option between 0 to 5.\")\n" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "(1) Insert item\n", - "(2) Update item\n", - "(3) Delete item\n", - "(4) Show all items\n", - "(0) Quit\n", - "Enter choice: 4\n", - "All items: No marks!\n", - "(1) Insert item\n", - "(2) Update item\n", - "(3) Delete item\n", - "(4) Show all items\n", - "(0) Quit\n", - "Enter choice: 0\n", - "Bye\n" - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "['B', 'Yo', 35]" ] + }, + "execution_count": 10, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Delete using remove\n", + "list1.remove(3.14)\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "JZ69baoEpYjv", + "outputId": "d28095d5-15b7-49a0-dfde-cfc5a7d0ceda" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "_xbbDMl-l0Hb", - "outputId": "dee39fa3-4246-4d76-cca0-c0dbdf8442cb", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 120 - } - }, - "source": [ - "# IMDA-Samsung Solve for Tomorrow Groupings\n", - "import random\n", - "\n", - "studentf = [\"ROSE EVANGELINE ANNE DAGMAN DESTOR\", \"TAN SHIAN PEI\", \\\n", - " \"LIU HONGSHUO\", \"NG JIA XIANG\", \"YEO WERN YEN EDA\", \\\n", - " \"JESSICA CHAN JI JI\"]\n", - "\n", - "studentm = [\"CHEN QIRUI\", \"CHIA QI EN DON\", \"HON WEN HONG\", \"HUANG QIU DI\", \\\n", - " \"ISAAC CHEN JING DE\", \"KHNG CHEN SHUO\", \"KINGOLD WANG XINFENG\", \\\n", - " \"KOH TAT YAO\", \"LEO QIYI JOEL\", \"LIEW ZHENG YUAN\", \"LIN JUNYU\", \\\n", - " \"QIANG JIAYUAN\", \"SEE YONG QIAN\", \"TEH WU RUI\", \\\n", - " \"TOBIAS TAN JIA REN\", \"TOH ZONG HAN KIERAN\", \\\n", - " \"TREVOR ONG YEE SIANG\", \"WANG YAOHUI\"]\n", - "\n", - "random.shuffle(studentf)\n", - "random.shuffle(studentm)\n", - "#print(studentf)\n", - "#print(studentm)\n", - "\n", - "count = 0\n", - "for i in range(6):\n", - " print(studentf[i], studentm[count], studentm[count+1], studentm[count+2])\n", - " count += 3" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "ROSE EVANGELINE ANNE DAGMAN DESTOR TEH WU RUI KOH TAT YAO WANG YAOHUI\n", - "JESSICA CHAN JI JI CHEN QIRUI CHIA QI EN DON TOH ZONG HAN KIERAN\n", - "LIU HONGSHUO TOBIAS TAN JIA REN LIEW ZHENG YUAN TREVOR ONG YEE SIANG\n", - "YEO WERN YEN EDA LIN JUNYU KHNG CHEN SHUO HUANG QIU DI\n", - "TAN SHIAN PEI HON WEN HONG SEE YONG QIAN LEO QIYI JOEL\n", - "NG JIA XIANG ISAAC CHEN JING DE QIANG JIAYUAN KINGOLD WANG XINFENG\n" - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "['Yo', 35]" ] + }, + "execution_count": 11, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Delete using del\n", + "del list1[0]\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "_c-1OlUVptye", + "outputId": "4ca98ffa-b0cf-4c48-9159-5fd4f4c4d415" + }, + "outputs": [ { - "cell_type": "markdown", - "metadata": { - "id": "IutQgcG_yhuw" - }, - "source": [ - "## **Dictionary**\n", - "\n", - "\n", - "* unordered key-value pair\n", - "* key must be unique\n", - "\n" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "Yo 35 " + ] + } + ], + "source": [ + "# Read\n", + "for item in list1:\n", + " print(item, end=' ')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 257 }, + "id": "-sTFYfcCp4bt", + "outputId": "ee213726-b4c7-434f-af86-5b021b633e77" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "A6dsbm9EqJBX", - "outputId": "530294d7-dab8-4d85-a6c2-c727808e34cb", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Create empty dictionary\n", - "d = {}\n", - "d" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 27 - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "(1) Insert item\n", + "(2) Update item\n", + "(3) Delete item\n", + "(4) Show all items\n", + "(0) Quit\n", + "Enter choice: 4\n", + "All items: No marks!\n", + "(1) Insert item\n", + "(2) Update item\n", + "(3) Delete item\n", + "(4) Show all items\n", + "(0) Quit\n", + "Enter choice: 0\n", + "Bye\n" + ] + } + ], + "source": [ + "# Program to CRUD list / array\n", + "\n", + "def menu():\n", + " print(\"(1) Insert item\")\n", + " print(\"(2) Update item\")\n", + " print(\"(3) Delete item\")\n", + " print(\"(4) Show all items\")\n", + " print(\"(0) Quit\")\n", + "\n", + "def insert_item(score):\n", + " print(\"Inserting item...\")\n", + " marks.append(score)\n", + "\n", + "def update_item(old_mark):\n", + " print(\"Updating item...\")\n", + " for i in range(len(marks)):\n", + " if marks[i] == old_mark:\n", + " new_mark = int(input(\"Enter new mark: \"))\n", + " marks[i] = new_mark\n", + "\n", + "def delete_item(score):\n", + " print(\"Deleting item...\")\n", + " if score in marks:\n", + " marks.remove(score)\n", + " else:\n", + " print(\"Cannot delete non-existent item\")\n", + "\n", + "def show_items():\n", + " print(\"All items: \", end='')\n", + " if marks != []: # if len(marks) > 0:\n", + " for mark in marks:\n", + " print(mark, end=' ')\n", + " print()\n", + " else: # empty list\n", + " print(\"No marks!\")\n", + "\n", + "# main\n", + "# initialise variables\n", + "choice = ''\n", + "marks = [] # create empty list (global variable)\n", + "#marks = [88, 100, 75] # for testing\n", + "while choice != '0': # while not quit\n", + " menu()\n", + " choice = input(\"Enter choice: \")\n", + " if choice == '1': # insert item\n", + " mark = int(input(\"Enter mark to insert: \"))\n", + " insert_item(mark)\n", + " elif choice == '2': # update item\n", + " mark = int(input(\"Enter mark to update: \"))\n", + " update_item(mark)\n", + " elif choice == '3': # delete item\n", + " mark = int(input(\"Enter mark to delete: \"))\n", + " delete_item(mark)\n", + " elif choice == '4': # show all items\n", + " show_items()\n", + " elif choice == '0':\n", + " print(\"Bye\")\n", + " else: # invalid choice\n", + " print(\"Yo! Choose a valid option between 0 to 4.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 120 }, + "id": "_xbbDMl-l0Hb", + "outputId": "dee39fa3-4246-4d76-cca0-c0dbdf8442cb" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "jjGhTcrczPS5", - "outputId": "729bb576-740c-412d-a797-012e49be6e5f", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Insert\n", - "d['Tom'] = 88\n", - "d" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'Tom': 88}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 28 - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "ROSE EVANGELINE ANNE DAGMAN DESTOR TEH WU RUI KOH TAT YAO WANG YAOHUI\n", + "JESSICA CHAN JI JI CHEN QIRUI CHIA QI EN DON TOH ZONG HAN KIERAN\n", + "LIU HONGSHUO TOBIAS TAN JIA REN LIEW ZHENG YUAN TREVOR ONG YEE SIANG\n", + "YEO WERN YEN EDA LIN JUNYU KHNG CHEN SHUO HUANG QIU DI\n", + "TAN SHIAN PEI HON WEN HONG SEE YONG QIAN LEO QIYI JOEL\n", + "NG JIA XIANG ISAAC CHEN JING DE QIANG JIAYUAN KINGOLD WANG XINFENG\n" + ] + } + ], + "source": [ + "# IMDA-Samsung Solve for Tomorrow Groupings\n", + "import random\n", + "\n", + "studentf = [\"ROSE EVANGELINE ANNE DAGMAN DESTOR\", \"TAN SHIAN PEI\", \\\n", + " \"LIU HONGSHUO\", \"NG JIA XIANG\", \"YEO WERN YEN EDA\", \\\n", + " \"JESSICA CHAN JI JI\"]\n", + "\n", + "studentm = [\"CHEN QIRUI\", \"CHIA QI EN DON\", \"HON WEN HONG\", \"HUANG QIU DI\", \\\n", + " \"ISAAC CHEN JING DE\", \"KHNG CHEN SHUO\", \"KINGOLD WANG XINFENG\", \\\n", + " \"KOH TAT YAO\", \"LEO QIYI JOEL\", \"LIEW ZHENG YUAN\", \"LIN JUNYU\", \\\n", + " \"QIANG JIAYUAN\", \"SEE YONG QIAN\", \"TEH WU RUI\", \\\n", + " \"TOBIAS TAN JIA REN\", \"TOH ZONG HAN KIERAN\", \\\n", + " \"TREVOR ONG YEE SIANG\", \"WANG YAOHUI\"]\n", + "\n", + "random.shuffle(studentf)\n", + "random.shuffle(studentm)\n", + "#print(studentf)\n", + "#print(studentm)\n", + "\n", + "count = 0\n", + "for i in range(6):\n", + " print(studentf[i], studentm[count], studentm[count+1], studentm[count+2])\n", + " count += 3" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IutQgcG_yhuw" + }, + "source": [ + "## **Dictionary**\n", + "\n", + "\n", + "* unordered key-value pair\n", + "* key must be unique\n", + "* keys will be a single element\n", + "* values can be a list or list within a list, numbers, etc.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "A6dsbm9EqJBX", + "outputId": "530294d7-dab8-4d85-a6c2-c727808e34cb" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "OpskJv7kzXDM", - "outputId": "5d5c3847-12f6-4880-e84f-fb3ecb0b8804", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "d['Mary'] = 100\n", - "d" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'Mary': 100, 'Tom': 88}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 29 - } + "data": { + "text/plain": [ + "{}" ] + }, + "execution_count": 27, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Create empty dictionary\n", + "d = {}\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "jjGhTcrczPS5", + "outputId": "729bb576-740c-412d-a797-012e49be6e5f" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "L6OnH4v-zhoF", - "outputId": "d656f13f-3dd6-4e6d-8690-d07b08ca2897", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "d['Tom'] = 35 # update if key exists\n", - "d" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'Mary': 100, 'Tom': 35}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 30 - } + "data": { + "text/plain": [ + "{'Tom': 88}" ] + }, + "execution_count": 28, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Insert\n", + "d['Tom'] = 88\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "OpskJv7kzXDM", + "outputId": "5d5c3847-12f6-4880-e84f-fb3ecb0b8804" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "VmLVJLndzs3z", - "outputId": "347055e4-092d-4c42-cc7d-f56ab1bc7e1a", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Delete\n", - "del d['Tom']\n", - "d" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'Mary': 100}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 31 - } + "data": { + "text/plain": [ + "{'Mary': 100, 'Tom': 88}" ] + }, + "execution_count": 29, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "d['Mary'] = 100\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "L6OnH4v-zhoF", + "outputId": "d656f13f-3dd6-4e6d-8690-d07b08ca2897" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "nD26Uu170LOr", - "outputId": "598658fb-122d-40b7-dd1c-e3490f459b76", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 - } - }, - "source": [ - "# Show\n", - "d = {'Mary': 100, 'Tom': 35}\n", - "d.keys() # show keys\n", - "d.values() # show values\n", - "for key, value in d.items():\n", - " print(key, value)" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Mary 100\n", - "Tom 35\n" - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "{'Mary': 100, 'Tom': 35}" ] + }, + "execution_count": 30, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "d['Tom'] = 35 # update if key exists\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "VmLVJLndzs3z", + "outputId": "347055e4-092d-4c42-cc7d-f56ab1bc7e1a" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "9XWjPlHo0QNt", - "outputId": "1d321a08-404f-4973-c2da-6685ddcf46bb", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 1000 - } - }, - "source": [ - "# CRUD program for dictionary\n", - "def menu():\n", - " print(\"(1) Insert\")\n", - " print(\"(2) Delete\")\n", - " print(\"(3) Update\")\n", - " print(\"(4) Show\")\n", - " print(\"(0) Quit\")\n", - "\n", - "def insert(key, value):\n", - " if key in contacts.keys():\n", - " print(\"Contact already exists!\")\n", - " else:\n", - " contacts[key] = value\n", - "\n", - "def delete(key):\n", - " if key in contacts.keys():\n", - " del contacts[key]\n", - " else:\n", - " print(\"Contact not found!\")\n", - "\n", - "def update(key, new_value):\n", - " if key in contacts.keys():\n", - " contacts[key] = new_value\n", - " else:\n", - " print(\"Contact not found!\")\n", - "\n", - "def show():\n", - " if contacts == {}:\n", - " print(\"You have no contacts!\")\n", - " else:\n", - " for key, value in contacts.items():\n", - " print(key, value)\n", - "\n", - "\n", - "# main\n", - "contacts = {}\n", - "option = ''\n", - "while option != '0': # while not quit\n", - " menu()\n", - " option = input(\"Enter option: \")\n", - " if option == '1':\n", - " name = input(\"Enter name: \")\n", - " mobile = input(\"Enter mobile: \")\n", - " insert(name, mobile)\n", - " elif option == '2':\n", - " name = input(\"Enter name: \")\n", - " delete(name)\n", - " elif option == '3':\n", - " name = input(\"Enter name: \")\n", - " new_mobile = input(\"Enter new mobile: \") \n", - " update(name, new_mobile)\n", - " elif option == '4':\n", - " show()\n", - " elif option == '0':\n", - " print(\"Bye\")\n", - " else:\n", - " print(\"Invalid option!\")\n", - "print(\"Done\")" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 1\n", - "Enter name: Tom\n", - "Enter mobile: 12345678\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 4\n", - "Tom 12345678\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 1\n", - "Enter name: Tom\n", - "Enter mobile: 12345678\n", - "Contact already exists!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 3\n", - "Enter name: Mary\n", - "Enter new mobile: 87654321\n", - "Contact not found!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 2\n", - "Enter name: Mary\n", - "Contact not found!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 2\n", - "Enter name: Tom\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 4\n", - "You have no contacts!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 0\n", - "Bye\n", - "Done\n" - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "{'Mary': 100}" ] + }, + "execution_count": 31, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Delete\n", + "del d['Tom']\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 }, + "id": "nD26Uu170LOr", + "outputId": "598658fb-122d-40b7-dd1c-e3490f459b76" + }, + "outputs": [ { - "cell_type": "markdown", - "metadata": { - "id": "2I7O8CWjtjoX" - }, - "source": [ - "## **Tuple**\n", - "\n", - "* Behaves like a list but is immutable\n", - "\n" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "Mary 100\n", + "Tom 35\n" + ] + } + ], + "source": [ + "# Show\n", + "d = {'Mary': 100, 'Tom': 35}\n", + "d.keys() # show keys\n", + "d.values() # show values\n", + "for key, value in d.items():\n", + " print(key, value)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 }, + "id": "9XWjPlHo0QNt", + "outputId": "1d321a08-404f-4973-c2da-6685ddcf46bb" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "t5Xhqe6W5Rpi", - "outputId": "8fba3785-0200-4ec3-cc15-d25f2d57197e", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - } - }, - "source": [ - "list1 = [1, 2, 3]\n", - "tuple1 = (1, 2, 3)\n", - "\n", - "for i in range(3):\n", - " print(list1[i], tuple1[i])" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "1 1\n", - "2 2\n", - "3 3\n" - ], - "name": "stdout" - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 1\n", + "Enter name: Tom\n", + "Enter mobile: 12345678\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 4\n", + "Tom 12345678\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 1\n", + "Enter name: Tom\n", + "Enter mobile: 12345678\n", + "Contact already exists!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 3\n", + "Enter name: Mary\n", + "Enter new mobile: 87654321\n", + "Contact not found!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 2\n", + "Enter name: Mary\n", + "Contact not found!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 2\n", + "Enter name: Tom\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 4\n", + "You have no contacts!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 0\n", + "Bye\n", + "Done\n" + ] + } + ], + "source": [ + "# CRUD program for dictionary\n", + "def menu():\n", + " print(\"(1) Insert\")\n", + " print(\"(2) Delete\")\n", + " print(\"(3) Update\")\n", + " print(\"(4) Show\")\n", + " print(\"(0) Quit\")\n", + "\n", + "def insert(key, value):\n", + " if key in contacts.keys():\n", + " print(\"Contact already exists!\")\n", + " else:\n", + " contacts[key] = value\n", + "\n", + "def delete(key):\n", + " if key in contacts.keys():\n", + " del contacts[key]\n", + " else:\n", + " print(\"Contact not found!\")\n", + "\n", + "def update(key, new_value):\n", + " if key in contacts.keys():\n", + " contacts[key] = new_value\n", + " else:\n", + " print(\"Contact not found!\")\n", + "\n", + "def show():\n", + " if contacts == {}:\n", + " print(\"You have no contacts!\")\n", + " else:\n", + " for key, value in contacts.items():\n", + " print(key, value)\n", + "\n", + "\n", + "# main\n", + "contacts = {}\n", + "option = ''\n", + "while option != '0': # while not quit\n", + " menu()\n", + " option = input(\"Enter option: \")\n", + " if option == '1':\n", + " name = input(\"Enter name: \")\n", + " mobile = input(\"Enter mobile: \")\n", + " insert(name, mobile)\n", + " elif option == '2':\n", + " name = input(\"Enter name: \")\n", + " delete(name)\n", + " elif option == '3':\n", + " name = input(\"Enter name: \")\n", + " new_mobile = input(\"Enter new mobile: \") \n", + " update(name, new_mobile)\n", + " elif option == '4':\n", + " show()\n", + " elif option == '0':\n", + " print(\"Bye\")\n", + " else:\n", + " print(\"Invalid option!\")\n", + "print(\"Done\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2I7O8CWjtjoX" + }, + "source": [ + "## **Tuple**\n", + "\n", + "* Behaves like a list but is immutable\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 }, + "id": "t5Xhqe6W5Rpi", + "outputId": "8fba3785-0200-4ec3-cc15-d25f2d57197e" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "MBexEYpzuBIM", - "outputId": "9d0b327a-53a9-490b-b783-1dffe73de975", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "list1[0] = 35\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "[35, 2, 3]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 5 - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "1 1\n", + "2 2\n", + "3 3\n" + ] + } + ], + "source": [ + "list1 = [1, 2, 3]\n", + "tuple1 = (1, 2, 3)\n", + "\n", + "for i in range(3):\n", + " print(list1[i], tuple1[i])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "MBexEYpzuBIM", + "outputId": "9d0b327a-53a9-490b-b783-1dffe73de975" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "pEN5t07puJCq", - "outputId": "8181d29b-c7b6-403f-ce75-1d25459d928b", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 182 - } - }, - "source": [ - "tuple1[0] = 35 # error because tuple cannot be changed\n", - "tuple1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "error", - "ename": "TypeError", - "evalue": "ignored", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtuple1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m35\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mtuple1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" - ] - } + "data": { + "text/plain": [ + "[35, 2, 3]" ] + }, + "execution_count": 5, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "list1[0] = 35\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 182 }, + "id": "pEN5t07puJCq", + "outputId": "8181d29b-c7b6-403f-ce75-1d25459d928b" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "aO6W_CT4uNoU", - "outputId": "f0151f87-5687-4799-bb6c-8fa3245e6992", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "type(tuple1)" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "tuple" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 7 - } - ] + "ename": "TypeError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtuple1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m35\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mtuple1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "tuple1[0] = 35 # error because tuple cannot be changed\n", + "tuple1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "aO6W_CT4uNoU", + "outputId": "f0151f87-5687-4799-bb6c-8fa3245e6992" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "StFLjmpxuqAa", - "outputId": "a58c046b-3642-411a-9dd2-3c9830e17c98", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "tuple2 = (35,)\n", - "type(tuple2)" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "tuple" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 10 - } + "data": { + "text/plain": [ + "tuple" ] + }, + "execution_count": 7, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type(tuple1)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "StFLjmpxuqAa", + "outputId": "a58c046b-3642-411a-9dd2-3c9830e17c98" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "DmwXn68quuFp" - }, - "source": [ - "" - ], - "execution_count": null, - "outputs": [] + "name": "stdout", + "output_type": "stream", + "text": [ + "tuple2 is \n", + "tuple3 is \n" + ] } - ] -} \ No newline at end of file + ], + "source": [ + "tuple2 = (35,)\n", + "print('tuple2 is', type(tuple2))\n", + "\n", + "tuple3 = 60,\n", + "print('tuple3 is', type(tuple3))\n", + "\n", + "# Hence, be careful when setting variables as a comma can make it into a tuple" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "DmwXn68quuFp" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "collapsed_sections": [], + "name": "Python06 - Array & Dictionary.ipynb", + "provenance": [] + }, + "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.2" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} From a10af25e51cf54642c5e79d9165532828830d68d Mon Sep 17 00:00:00 2001 From: Rea1103 <61258371+Rea1103@users.noreply.github.com> Date: Thu, 15 Oct 2020 23:21:18 +0800 Subject: [PATCH 2/3] Update Python06_Array_&_Dictionary.ipynb --- .../Python06_Array_&_Dictionary.ipynb | 1922 ++++++++--------- 1 file changed, 935 insertions(+), 987 deletions(-) diff --git a/computing_sh/Python06_Array_&_Dictionary.ipynb b/computing_sh/Python06_Array_&_Dictionary.ipynb index 3d0ae13..8cf8d39 100644 --- a/computing_sh/Python06_Array_&_Dictionary.ipynb +++ b/computing_sh/Python06_Array_&_Dictionary.ipynb @@ -1,1054 +1,1002 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "_sIeTutwNGJK" - }, - "source": [ - "# **Data Structures**\n", - "\n", - "\n", - "* List / Array\n", - "* Dictionary\n", - "\n", - "Create / Insert / Add \n", - "Read / Select / Display \n", - "Update / Edit \n", - "Delete / Remove \n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "TgbEMstMxCHY" - }, - "source": [ - "## **List / Array**" - ] + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Python06 - Array & Dictionary.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ + "cells": [ { - "data": { - "text/plain": [ - "[]" + "cell_type": "markdown", + "metadata": { + "id": "_sIeTutwNGJK" + }, + "source": [ + "# **Data Structures**\n", + "\n", + "\n", + "* List / Array\n", + "* Dictionary\n", + "\n", + "Create / Insert / Add \n", + "Read / Select / Display \n", + "Update / Edit \n", + "Delete / Remove \n" ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Creating empty lists\n", - "list1 = [] # method 1\n", - "list1 = list() # method 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 }, - "id": "OgPVcGHKvcTO", - "outputId": "b58eef75-6484-4332-e72a-0344a2441428" - }, - "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "3\n", - "3\n" - ] - } - ], - "source": [ - "str1 = 'Tom' # immutable\n", - "list1 = ['T', 'o', 'm'] # mutable\n", - "print(len(str1))\n", - "print(len(list1))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 + "cell_type": "markdown", + "metadata": { + "id": "TgbEMstMxCHY" + }, + "source": [ + "## **List / Array**" + ] }, - "id": "UaACnox-xfVh", - "outputId": "e8733ac9-6f7d-464a-d57d-849fb0cd1db2" - }, - "outputs": [ { - "data": { - "text/plain": [ - "['M', 'o', 'm']" + "cell_type": "code", + "metadata": { + "id": "OgPVcGHKvcTO", + "outputId": "b58eef75-6484-4332-e72a-0344a2441428", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "str1 = 'Tom' # immutable\n", + "list1 = ['T', 'o', 'm'] # mutable\n", + "print(len(str1))\n", + "print(len(list1))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "3\n", + "3\n" + ], + "name": "stdout" + } ] - }, - "execution_count": 52, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "list1[0] = 'M'\n", - "list1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 85 }, - "id": "4oEs6a8fxrNj", - "outputId": "a445ce15-ecb2-47f1-e1be-3522942c13f3" - }, - "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "T\n", - "M\n", - "m\n", - "m\n" - ] - } - ], - "source": [ - "print(str1[0])\n", - "print(list1[0])\n", - "print(str1[-1])\n", - "print(list1[-1])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 + "cell_type": "code", + "metadata": { + "id": "UaACnox-xfVh", + "outputId": "e8733ac9-6f7d-464a-d57d-849fb0cd1db2", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "list1[0] = 'M'\n", + "list1" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['M', 'o', 'm']" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 52 + } + ] }, - "id": "pyu0Wm08x7rW", - "outputId": "31b73ad0-711d-437c-e5e6-e3e5d77e1c37" - }, - "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "M\n", - "o\n", - "m\n" - ] - } - ], - "source": [ - "for char in list1:\n", - " print(char)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### A quick overview of list operations: \n", - "\n", - "| Operation | Parameter | Return |\n", - "| :--------- | :-------- | :------ |\n", - "| len(list) | List | The number of items in a list. |\n", - "| list.append() | An object that has the len of one | Returns None but adds a single element to a list. |\n", - "| list.insert() | Specified index and an object that has the len of one | Returns None but adds the single element to a list based on the index specified. |\n", - "| list.remove(arg) | Any value present in the list | Returns None but removes the first matching element (which is passed as an argument) from the list. |\n", - "| del list[] | Index or range of the value wanted to be deleted from the list | Returns None but deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments. |\n", - "\n", - "*Note that this list is non-exhaustive*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 + "cell_type": "code", + "metadata": { + "id": "4oEs6a8fxrNj", + "outputId": "a445ce15-ecb2-47f1-e1be-3522942c13f3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + } + }, + "source": [ + "print(str1[0])\n", + "print(list1[0])\n", + "print(str1[-1])\n", + "print(list1[-1])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "T\n", + "M\n", + "m\n", + "m\n" + ], + "name": "stdout" + } + ] }, - "id": "cFPui8iHyDYJ", - "outputId": "cd5b3641-a97f-4049-b1a7-556539e90a58" - }, - "outputs": [ { - "data": { - "text/plain": [ - "['B', 'a', 3.14]" + "cell_type": "code", + "metadata": { + "id": "pyu0Wm08x7rW", + "outputId": "31b73ad0-711d-437c-e5e6-e3e5d77e1c37", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + } + }, + "source": [ + "for char in list1:\n", + " print(char)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "M\n", + "o\n", + "m\n" + ], + "name": "stdout" + } ] - }, - "execution_count": 5, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "list1 = [] # create empty array / list\n", - "list1\n", - "len(list1) # find length\n", - "list1.append('B') # add to the back of array / list\n", - "list1\n", - "list1.append('a')\n", - "list1\n", - "list1.append(3.14)\n", - "list1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 }, - "id": "B59CDddPyKu6", - "outputId": "0bb0b53a-5091-4f7a-d0c1-78edbf80e8cc" - }, - "outputs": [ { - "data": { - "text/plain": [ - "['B', 'Yo', 'a', 3.14]" + "cell_type": "code", + "metadata": { + "id": "cFPui8iHyDYJ", + "outputId": "cd5b3641-a97f-4049-b1a7-556539e90a58", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "list1 = [] # create empty array / list\n", + "list1\n", + "len(list1) # find length\n", + "list1.append('B') # add to the back of array / list\n", + "list1\n", + "list1.append('a')\n", + "list1\n", + "list1.append(3.14)\n", + "list1" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['B', 'a', 3.14]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 5 + } ] - }, - "execution_count": 6, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "list1.insert(1, 'Yo') # add to any position in an array\n", - "list1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 }, - "id": "bVMwgFDMytfX", - "outputId": "3375e227-ad95-4df3-b91e-31b16991af47" - }, - "outputs": [ { - "data": { - "text/plain": [ - "['B', 'Yo', 35, 3.14]" + "cell_type": "code", + "metadata": { + "id": "B59CDddPyKu6", + "outputId": "0bb0b53a-5091-4f7a-d0c1-78edbf80e8cc", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "list1.insert(1, 'Yo') # add to any position in an array\n", + "list1" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['B', 'Yo', 'a', 3.14]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 6 + } ] - }, - "execution_count": 7, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Update\n", - "list1[2] = 35\n", - "list1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 }, - "id": "Ksq7eVuoombD", - "outputId": "0f0cafbe-f5f1-4e34-f593-2eff55987181" - }, - "outputs": [ { - "data": { - "text/plain": [ - "['B', 'Yo', 35]" + "cell_type": "code", + "metadata": { + "id": "bVMwgFDMytfX", + "outputId": "3375e227-ad95-4df3-b91e-31b16991af47", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Update\n", + "list1[2] = 35\n", + "list1" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['B', 'Yo', 35, 3.14]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 7 + } ] - }, - "execution_count": 10, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Delete using remove\n", - "list1.remove(3.14)\n", - "list1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 }, - "id": "JZ69baoEpYjv", - "outputId": "d28095d5-15b7-49a0-dfde-cfc5a7d0ceda" - }, - "outputs": [ { - "data": { - "text/plain": [ - "['Yo', 35]" + "cell_type": "code", + "metadata": { + "id": "Ksq7eVuoombD", + "outputId": "0f0cafbe-f5f1-4e34-f593-2eff55987181", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Delete using remove\n", + "list1.remove(3.14)\n", + "list1" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['B', 'Yo', 35]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 10 + } ] - }, - "execution_count": 11, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Delete using del\n", - "del list1[0]\n", - "list1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 }, - "id": "_c-1OlUVptye", - "outputId": "4ca98ffa-b0cf-4c48-9159-5fd4f4c4d415" - }, - "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Yo 35 " - ] - } - ], - "source": [ - "# Read\n", - "for item in list1:\n", - " print(item, end=' ')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 257 + "cell_type": "code", + "metadata": { + "id": "JZ69baoEpYjv", + "outputId": "d28095d5-15b7-49a0-dfde-cfc5a7d0ceda", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Delete using del\n", + "del list1[0]\n", + "list1" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['Yo', 35]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 11 + } + ] }, - "id": "-sTFYfcCp4bt", - "outputId": "ee213726-b4c7-434f-af86-5b021b633e77" - }, - "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1) Insert item\n", - "(2) Update item\n", - "(3) Delete item\n", - "(4) Show all items\n", - "(0) Quit\n", - "Enter choice: 4\n", - "All items: No marks!\n", - "(1) Insert item\n", - "(2) Update item\n", - "(3) Delete item\n", - "(4) Show all items\n", - "(0) Quit\n", - "Enter choice: 0\n", - "Bye\n" - ] - } - ], - "source": [ - "# Program to CRUD list / array\n", - "\n", - "def menu():\n", - " print(\"(1) Insert item\")\n", - " print(\"(2) Update item\")\n", - " print(\"(3) Delete item\")\n", - " print(\"(4) Show all items\")\n", - " print(\"(0) Quit\")\n", - "\n", - "def insert_item(score):\n", - " print(\"Inserting item...\")\n", - " marks.append(score)\n", - "\n", - "def update_item(old_mark):\n", - " print(\"Updating item...\")\n", - " for i in range(len(marks)):\n", - " if marks[i] == old_mark:\n", - " new_mark = int(input(\"Enter new mark: \"))\n", - " marks[i] = new_mark\n", - "\n", - "def delete_item(score):\n", - " print(\"Deleting item...\")\n", - " if score in marks:\n", - " marks.remove(score)\n", - " else:\n", - " print(\"Cannot delete non-existent item\")\n", - "\n", - "def show_items():\n", - " print(\"All items: \", end='')\n", - " if marks != []: # if len(marks) > 0:\n", - " for mark in marks:\n", - " print(mark, end=' ')\n", - " print()\n", - " else: # empty list\n", - " print(\"No marks!\")\n", - "\n", - "# main\n", - "# initialise variables\n", - "choice = ''\n", - "marks = [] # create empty list (global variable)\n", - "#marks = [88, 100, 75] # for testing\n", - "while choice != '0': # while not quit\n", - " menu()\n", - " choice = input(\"Enter choice: \")\n", - " if choice == '1': # insert item\n", - " mark = int(input(\"Enter mark to insert: \"))\n", - " insert_item(mark)\n", - " elif choice == '2': # update item\n", - " mark = int(input(\"Enter mark to update: \"))\n", - " update_item(mark)\n", - " elif choice == '3': # delete item\n", - " mark = int(input(\"Enter mark to delete: \"))\n", - " delete_item(mark)\n", - " elif choice == '4': # show all items\n", - " show_items()\n", - " elif choice == '0':\n", - " print(\"Bye\")\n", - " else: # invalid choice\n", - " print(\"Yo! Choose a valid option between 0 to 4.\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 120 + "cell_type": "code", + "metadata": { + "id": "_c-1OlUVptye", + "outputId": "4ca98ffa-b0cf-4c48-9159-5fd4f4c4d415", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Read\n", + "for item in list1:\n", + " print(item, end=' ')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Yo 35 " + ], + "name": "stdout" + } + ] }, - "id": "_xbbDMl-l0Hb", - "outputId": "dee39fa3-4246-4d76-cca0-c0dbdf8442cb" - }, - "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "ROSE EVANGELINE ANNE DAGMAN DESTOR TEH WU RUI KOH TAT YAO WANG YAOHUI\n", - "JESSICA CHAN JI JI CHEN QIRUI CHIA QI EN DON TOH ZONG HAN KIERAN\n", - "LIU HONGSHUO TOBIAS TAN JIA REN LIEW ZHENG YUAN TREVOR ONG YEE SIANG\n", - "YEO WERN YEN EDA LIN JUNYU KHNG CHEN SHUO HUANG QIU DI\n", - "TAN SHIAN PEI HON WEN HONG SEE YONG QIAN LEO QIYI JOEL\n", - "NG JIA XIANG ISAAC CHEN JING DE QIANG JIAYUAN KINGOLD WANG XINFENG\n" - ] - } - ], - "source": [ - "# IMDA-Samsung Solve for Tomorrow Groupings\n", - "import random\n", - "\n", - "studentf = [\"ROSE EVANGELINE ANNE DAGMAN DESTOR\", \"TAN SHIAN PEI\", \\\n", - " \"LIU HONGSHUO\", \"NG JIA XIANG\", \"YEO WERN YEN EDA\", \\\n", - " \"JESSICA CHAN JI JI\"]\n", - "\n", - "studentm = [\"CHEN QIRUI\", \"CHIA QI EN DON\", \"HON WEN HONG\", \"HUANG QIU DI\", \\\n", - " \"ISAAC CHEN JING DE\", \"KHNG CHEN SHUO\", \"KINGOLD WANG XINFENG\", \\\n", - " \"KOH TAT YAO\", \"LEO QIYI JOEL\", \"LIEW ZHENG YUAN\", \"LIN JUNYU\", \\\n", - " \"QIANG JIAYUAN\", \"SEE YONG QIAN\", \"TEH WU RUI\", \\\n", - " \"TOBIAS TAN JIA REN\", \"TOH ZONG HAN KIERAN\", \\\n", - " \"TREVOR ONG YEE SIANG\", \"WANG YAOHUI\"]\n", - "\n", - "random.shuffle(studentf)\n", - "random.shuffle(studentm)\n", - "#print(studentf)\n", - "#print(studentm)\n", - "\n", - "count = 0\n", - "for i in range(6):\n", - " print(studentf[i], studentm[count], studentm[count+1], studentm[count+2])\n", - " count += 3" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "IutQgcG_yhuw" - }, - "source": [ - "## **Dictionary**\n", - "\n", - "\n", - "* unordered key-value pair\n", - "* key must be unique\n", - "* keys will be a single element\n", - "* values can be a list or list within a list, numbers, etc.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 + "cell_type": "code", + "metadata": { + "id": "-sTFYfcCp4bt", + "outputId": "ee213726-b4c7-434f-af86-5b021b633e77", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 257 + } + }, + "source": [ + "# Program to CRUD list / array\n", + "\n", + "def menu():\n", + " print(\"(1) Insert item\")\n", + " print(\"(2) Update item\")\n", + " print(\"(3) Delete item\")\n", + " print(\"(4) Show all items\")\n", + " print(\"(0) Quit\")\n", + "\n", + "def insert_item(score):\n", + " print(\"Inserting item...\")\n", + " marks.append(score)\n", + "\n", + "def update_item(old_mark):\n", + " print(\"Updating item...\")\n", + " for i in range(len(marks)):\n", + " if marks[i] == old_mark:\n", + " new_mark = int(input(\"Enter new mark: \"))\n", + " marks[i] = new_mark\n", + "\n", + "def delete_item(score):\n", + " print(\"Deleting item...\")\n", + " if score in marks:\n", + " marks.remove(score)\n", + " else:\n", + " print(\"Cannot delete non-existent item\")\n", + "\n", + "def show_items():\n", + " print(\"All items: \", end='')\n", + " if marks != []: # if len(marks) > 0:\n", + " for mark in marks:\n", + " print(mark, end=' ')\n", + " print()\n", + " else: # empty list\n", + " print(\"No marks!\")\n", + "\n", + "# main\n", + "# initialise variables\n", + "choice = ''\n", + "marks = [] # create empty list (global variable)\n", + "#marks = [88, 100, 75] # for testing\n", + "while choice != '0': # while not quit\n", + " menu()\n", + " choice = input(\"Enter choice: \")\n", + " if choice == '1': # insert item\n", + " mark = int(input(\"Enter mark to insert: \"))\n", + " insert_item(mark)\n", + " elif choice == '2': # update item\n", + " mark = int(input(\"Enter mark to update: \"))\n", + " update_item(mark)\n", + " elif choice == '3': # delete item\n", + " mark = int(input(\"Enter mark to delete: \"))\n", + " delete_item(mark)\n", + " elif choice == '4': # show all items\n", + " show_items()\n", + " elif choice == '0':\n", + " print(\"Bye\")\n", + " else: # invalid choice\n", + " print(\"Yo! Choose a valid option between 0 to 5.\")\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(1) Insert item\n", + "(2) Update item\n", + "(3) Delete item\n", + "(4) Show all items\n", + "(0) Quit\n", + "Enter choice: 4\n", + "All items: No marks!\n", + "(1) Insert item\n", + "(2) Update item\n", + "(3) Delete item\n", + "(4) Show all items\n", + "(0) Quit\n", + "Enter choice: 0\n", + "Bye\n" + ], + "name": "stdout" + } + ] }, - "id": "A6dsbm9EqJBX", - "outputId": "530294d7-dab8-4d85-a6c2-c727808e34cb" - }, - "outputs": [ { - "data": { - "text/plain": [ - "{}" + "cell_type": "code", + "metadata": { + "id": "_xbbDMl-l0Hb", + "outputId": "dee39fa3-4246-4d76-cca0-c0dbdf8442cb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 120 + } + }, + "source": [ + "# IMDA-Samsung Solve for Tomorrow Groupings\n", + "import random\n", + "\n", + "studentf = [\"ROSE EVANGELINE ANNE DAGMAN DESTOR\", \"TAN SHIAN PEI\", \\\n", + " \"LIU HONGSHUO\", \"NG JIA XIANG\", \"YEO WERN YEN EDA\", \\\n", + " \"JESSICA CHAN JI JI\"]\n", + "\n", + "studentm = [\"CHEN QIRUI\", \"CHIA QI EN DON\", \"HON WEN HONG\", \"HUANG QIU DI\", \\\n", + " \"ISAAC CHEN JING DE\", \"KHNG CHEN SHUO\", \"KINGOLD WANG XINFENG\", \\\n", + " \"KOH TAT YAO\", \"LEO QIYI JOEL\", \"LIEW ZHENG YUAN\", \"LIN JUNYU\", \\\n", + " \"QIANG JIAYUAN\", \"SEE YONG QIAN\", \"TEH WU RUI\", \\\n", + " \"TOBIAS TAN JIA REN\", \"TOH ZONG HAN KIERAN\", \\\n", + " \"TREVOR ONG YEE SIANG\", \"WANG YAOHUI\"]\n", + "\n", + "random.shuffle(studentf)\n", + "random.shuffle(studentm)\n", + "#print(studentf)\n", + "#print(studentm)\n", + "\n", + "count = 0\n", + "for i in range(6):\n", + " print(studentf[i], studentm[count], studentm[count+1], studentm[count+2])\n", + " count += 3" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "ROSE EVANGELINE ANNE DAGMAN DESTOR TEH WU RUI KOH TAT YAO WANG YAOHUI\n", + "JESSICA CHAN JI JI CHEN QIRUI CHIA QI EN DON TOH ZONG HAN KIERAN\n", + "LIU HONGSHUO TOBIAS TAN JIA REN LIEW ZHENG YUAN TREVOR ONG YEE SIANG\n", + "YEO WERN YEN EDA LIN JUNYU KHNG CHEN SHUO HUANG QIU DI\n", + "TAN SHIAN PEI HON WEN HONG SEE YONG QIAN LEO QIYI JOEL\n", + "NG JIA XIANG ISAAC CHEN JING DE QIANG JIAYUAN KINGOLD WANG XINFENG\n" + ], + "name": "stdout" + } ] - }, - "execution_count": 27, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Create empty dictionary\n", - "d = {}\n", - "d" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 }, - "id": "jjGhTcrczPS5", - "outputId": "729bb576-740c-412d-a797-012e49be6e5f" - }, - "outputs": [ { - "data": { - "text/plain": [ - "{'Tom': 88}" + "cell_type": "markdown", + "metadata": { + "id": "IutQgcG_yhuw" + }, + "source": [ + "## **Dictionary**\n", + "\n", + "\n", + "* unordered key-value pair\n", + "* key must be unique\n", + "\n" ] - }, - "execution_count": 28, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Insert\n", - "d['Tom'] = 88\n", - "d" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 }, - "id": "OpskJv7kzXDM", - "outputId": "5d5c3847-12f6-4880-e84f-fb3ecb0b8804" - }, - "outputs": [ { - "data": { - "text/plain": [ - "{'Mary': 100, 'Tom': 88}" + "cell_type": "code", + "metadata": { + "id": "A6dsbm9EqJBX", + "outputId": "530294d7-dab8-4d85-a6c2-c727808e34cb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Create empty dictionary\n", + "d = {}\n", + "d" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 27 + } ] - }, - "execution_count": 29, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "d['Mary'] = 100\n", - "d" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 }, - "id": "L6OnH4v-zhoF", - "outputId": "d656f13f-3dd6-4e6d-8690-d07b08ca2897" - }, - "outputs": [ { - "data": { - "text/plain": [ - "{'Mary': 100, 'Tom': 35}" + "cell_type": "code", + "metadata": { + "id": "jjGhTcrczPS5", + "outputId": "729bb576-740c-412d-a797-012e49be6e5f", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Insert\n", + "d['Tom'] = 88\n", + "d" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'Tom': 88}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 28 + } ] - }, - "execution_count": 30, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "d['Tom'] = 35 # update if key exists\n", - "d" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 }, - "id": "VmLVJLndzs3z", - "outputId": "347055e4-092d-4c42-cc7d-f56ab1bc7e1a" - }, - "outputs": [ { - "data": { - "text/plain": [ - "{'Mary': 100}" + "cell_type": "code", + "metadata": { + "id": "OpskJv7kzXDM", + "outputId": "5d5c3847-12f6-4880-e84f-fb3ecb0b8804", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "d['Mary'] = 100\n", + "d" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'Mary': 100, 'Tom': 88}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 29 + } ] - }, - "execution_count": 31, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "# Delete\n", - "del d['Tom']\n", - "d" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 }, - "id": "nD26Uu170LOr", - "outputId": "598658fb-122d-40b7-dd1c-e3490f459b76" - }, - "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Mary 100\n", - "Tom 35\n" - ] - } - ], - "source": [ - "# Show\n", - "d = {'Mary': 100, 'Tom': 35}\n", - "d.keys() # show keys\n", - "d.values() # show values\n", - "for key, value in d.items():\n", - " print(key, value)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 1000 + "cell_type": "code", + "metadata": { + "id": "L6OnH4v-zhoF", + "outputId": "d656f13f-3dd6-4e6d-8690-d07b08ca2897", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "d['Tom'] = 35 # update if key exists\n", + "d" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'Mary': 100, 'Tom': 35}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 30 + } + ] }, - "id": "9XWjPlHo0QNt", - "outputId": "1d321a08-404f-4973-c2da-6685ddcf46bb" - }, - "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 1\n", - "Enter name: Tom\n", - "Enter mobile: 12345678\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 4\n", - "Tom 12345678\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 1\n", - "Enter name: Tom\n", - "Enter mobile: 12345678\n", - "Contact already exists!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 3\n", - "Enter name: Mary\n", - "Enter new mobile: 87654321\n", - "Contact not found!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 2\n", - "Enter name: Mary\n", - "Contact not found!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 2\n", - "Enter name: Tom\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 4\n", - "You have no contacts!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 0\n", - "Bye\n", - "Done\n" - ] - } - ], - "source": [ - "# CRUD program for dictionary\n", - "def menu():\n", - " print(\"(1) Insert\")\n", - " print(\"(2) Delete\")\n", - " print(\"(3) Update\")\n", - " print(\"(4) Show\")\n", - " print(\"(0) Quit\")\n", - "\n", - "def insert(key, value):\n", - " if key in contacts.keys():\n", - " print(\"Contact already exists!\")\n", - " else:\n", - " contacts[key] = value\n", - "\n", - "def delete(key):\n", - " if key in contacts.keys():\n", - " del contacts[key]\n", - " else:\n", - " print(\"Contact not found!\")\n", - "\n", - "def update(key, new_value):\n", - " if key in contacts.keys():\n", - " contacts[key] = new_value\n", - " else:\n", - " print(\"Contact not found!\")\n", - "\n", - "def show():\n", - " if contacts == {}:\n", - " print(\"You have no contacts!\")\n", - " else:\n", - " for key, value in contacts.items():\n", - " print(key, value)\n", - "\n", - "\n", - "# main\n", - "contacts = {}\n", - "option = ''\n", - "while option != '0': # while not quit\n", - " menu()\n", - " option = input(\"Enter option: \")\n", - " if option == '1':\n", - " name = input(\"Enter name: \")\n", - " mobile = input(\"Enter mobile: \")\n", - " insert(name, mobile)\n", - " elif option == '2':\n", - " name = input(\"Enter name: \")\n", - " delete(name)\n", - " elif option == '3':\n", - " name = input(\"Enter name: \")\n", - " new_mobile = input(\"Enter new mobile: \") \n", - " update(name, new_mobile)\n", - " elif option == '4':\n", - " show()\n", - " elif option == '0':\n", - " print(\"Bye\")\n", - " else:\n", - " print(\"Invalid option!\")\n", - "print(\"Done\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "2I7O8CWjtjoX" - }, - "source": [ - "## **Tuple**\n", - "\n", - "* Behaves like a list but is immutable\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 + "cell_type": "code", + "metadata": { + "id": "VmLVJLndzs3z", + "outputId": "347055e4-092d-4c42-cc7d-f56ab1bc7e1a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Delete\n", + "del d['Tom']\n", + "d" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'Mary': 100}" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 31 + } + ] }, - "id": "t5Xhqe6W5Rpi", - "outputId": "8fba3785-0200-4ec3-cc15-d25f2d57197e" - }, - "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 1\n", - "2 2\n", - "3 3\n" - ] - } - ], - "source": [ - "list1 = [1, 2, 3]\n", - "tuple1 = (1, 2, 3)\n", - "\n", - "for i in range(3):\n", - " print(list1[i], tuple1[i])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 + "cell_type": "code", + "metadata": { + "id": "nD26Uu170LOr", + "outputId": "598658fb-122d-40b7-dd1c-e3490f459b76", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Show\n", + "d = {'Mary': 100, 'Tom': 35}\n", + "d.keys() # show keys\n", + "d.values() # show values\n", + "for key, value in d.items():\n", + " print(key, value)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Mary 100\n", + "Tom 35\n" + ], + "name": "stdout" + } + ] }, - "id": "MBexEYpzuBIM", - "outputId": "9d0b327a-53a9-490b-b783-1dffe73de975" - }, - "outputs": [ { - "data": { - "text/plain": [ - "[35, 2, 3]" + "cell_type": "code", + "metadata": { + "id": "9XWjPlHo0QNt", + "outputId": "1d321a08-404f-4973-c2da-6685ddcf46bb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 + } + }, + "source": [ + "# CRUD program for dictionary\n", + "def menu():\n", + " print(\"(1) Insert\")\n", + " print(\"(2) Delete\")\n", + " print(\"(3) Update\")\n", + " print(\"(4) Show\")\n", + " print(\"(0) Quit\")\n", + "\n", + "def insert(key, value):\n", + " if key in contacts.keys():\n", + " print(\"Contact already exists!\")\n", + " else:\n", + " contacts[key] = value\n", + "\n", + "def delete(key):\n", + " if key in contacts.keys():\n", + " del contacts[key]\n", + " else:\n", + " print(\"Contact not found!\")\n", + "\n", + "def update(key, new_value):\n", + " if key in contacts.keys():\n", + " contacts[key] = new_value\n", + " else:\n", + " print(\"Contact not found!\")\n", + "\n", + "def show():\n", + " if contacts == {}:\n", + " print(\"You have no contacts!\")\n", + " else:\n", + " for key, value in contacts.items():\n", + " print(key, value)\n", + "\n", + "\n", + "# main\n", + "contacts = {}\n", + "option = ''\n", + "while option != '0': # while not quit\n", + " menu()\n", + " option = input(\"Enter option: \")\n", + " if option == '1':\n", + " name = input(\"Enter name: \")\n", + " mobile = input(\"Enter mobile: \")\n", + " insert(name, mobile)\n", + " elif option == '2':\n", + " name = input(\"Enter name: \")\n", + " delete(name)\n", + " elif option == '3':\n", + " name = input(\"Enter name: \")\n", + " new_mobile = input(\"Enter new mobile: \") \n", + " update(name, new_mobile)\n", + " elif option == '4':\n", + " show()\n", + " elif option == '0':\n", + " print(\"Bye\")\n", + " else:\n", + " print(\"Invalid option!\")\n", + "print(\"Done\")" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 1\n", + "Enter name: Tom\n", + "Enter mobile: 12345678\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 4\n", + "Tom 12345678\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 1\n", + "Enter name: Tom\n", + "Enter mobile: 12345678\n", + "Contact already exists!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 3\n", + "Enter name: Mary\n", + "Enter new mobile: 87654321\n", + "Contact not found!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 2\n", + "Enter name: Mary\n", + "Contact not found!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 2\n", + "Enter name: Tom\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 4\n", + "You have no contacts!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 0\n", + "Bye\n", + "Done\n" + ], + "name": "stdout" + } ] - }, - "execution_count": 5, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "list1[0] = 35\n", - "list1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 182 }, - "id": "pEN5t07puJCq", - "outputId": "8181d29b-c7b6-403f-ce75-1d25459d928b" - }, - "outputs": [ { - "ename": "TypeError", - "evalue": "ignored", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtuple1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m35\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mtuple1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" - ] - } - ], - "source": [ - "tuple1[0] = 35 # error because tuple cannot be changed\n", - "tuple1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 + "cell_type": "markdown", + "metadata": { + "id": "2I7O8CWjtjoX" + }, + "source": [ + "## **Tuple**\n", + "\n", + "* Behaves like a list but is immutable\n", + "\n" + ] }, - "id": "aO6W_CT4uNoU", - "outputId": "f0151f87-5687-4799-bb6c-8fa3245e6992" - }, - "outputs": [ { - "data": { - "text/plain": [ - "tuple" + "cell_type": "code", + "metadata": { + "id": "t5Xhqe6W5Rpi", + "outputId": "8fba3785-0200-4ec3-cc15-d25f2d57197e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + } + }, + "source": [ + "list1 = [1, 2, 3]\n", + "tuple1 = (1, 2, 3)\n", + "\n", + "for i in range(3):\n", + " print(list1[i], tuple1[i])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1 1\n", + "2 2\n", + "3 3\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MBexEYpzuBIM", + "outputId": "9d0b327a-53a9-490b-b783-1dffe73de975", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "list1[0] = 35\n", + "list1" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[35, 2, 3]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 5 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pEN5t07puJCq", + "outputId": "8181d29b-c7b6-403f-ce75-1d25459d928b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 182 + } + }, + "source": [ + "tuple1[0] = 35 # error because tuple cannot be changed\n", + "tuple1" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtuple1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m35\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mtuple1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "aO6W_CT4uNoU", + "outputId": "f0151f87-5687-4799-bb6c-8fa3245e6992", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "type(tuple1)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tuple" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 7 + } ] - }, - "execution_count": 7, - "metadata": { - "tags": [] - }, - "output_type": "execute_result" - } - ], - "source": [ - "type(tuple1)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 }, - "id": "StFLjmpxuqAa", - "outputId": "a58c046b-3642-411a-9dd2-3c9830e17c98" - }, - "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "tuple2 is \n", - "tuple3 is \n" - ] + "cell_type": "code", + "metadata": { + "id": "StFLjmpxuqAa", + "outputId": "a58c046b-3642-411a-9dd2-3c9830e17c98", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "tuple2 = (35,)\n", + "type(tuple2)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "tuple" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 10 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "DmwXn68quuFp" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] } - ], - "source": [ - "tuple2 = (35,)\n", - "print('tuple2 is', type(tuple2))\n", - "\n", - "tuple3 = 60,\n", - "print('tuple3 is', type(tuple3))\n", - "\n", - "# Hence, be careful when setting variables as a comma can make it into a tuple" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "DmwXn68quuFp" - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "name": "Python06 - Array & Dictionary.ipynb", - "provenance": [] - }, - "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.2" - } - }, - "nbformat": 4, - "nbformat_minor": 1 + ] } From 8cd32515adee620ce91c4bef1cf263110169155a Mon Sep 17 00:00:00 2001 From: Rea1103 <61258371+Rea1103@users.noreply.github.com> Date: Thu, 15 Oct 2020 23:21:54 +0800 Subject: [PATCH 3/3] Update Python06_Array_&_Dictionary.ipynb Created a summary table and fixed some code cells. --- .../Python06_Array_&_Dictionary.ipynb | 1922 +++++++++-------- 1 file changed, 987 insertions(+), 935 deletions(-) diff --git a/computing_sh/Python06_Array_&_Dictionary.ipynb b/computing_sh/Python06_Array_&_Dictionary.ipynb index 8cf8d39..3d0ae13 100644 --- a/computing_sh/Python06_Array_&_Dictionary.ipynb +++ b/computing_sh/Python06_Array_&_Dictionary.ipynb @@ -1,1002 +1,1054 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "Python06 - Array & Dictionary.ipynb", - "provenance": [], - "collapsed_sections": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "_sIeTutwNGJK" + }, + "source": [ + "# **Data Structures**\n", + "\n", + "\n", + "* List / Array\n", + "* Dictionary\n", + "\n", + "Create / Insert / Add \n", + "Read / Select / Display \n", + "Update / Edit \n", + "Delete / Remove \n" + ] }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "_sIeTutwNGJK" - }, - "source": [ - "# **Data Structures**\n", - "\n", - "\n", - "* List / Array\n", - "* Dictionary\n", - "\n", - "Create / Insert / Add \n", - "Read / Select / Display \n", - "Update / Edit \n", - "Delete / Remove \n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "TgbEMstMxCHY" - }, - "source": [ - "## **List / Array**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "OgPVcGHKvcTO", - "outputId": "b58eef75-6484-4332-e72a-0344a2441428", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 - } - }, - "source": [ - "str1 = 'Tom' # immutable\n", - "list1 = ['T', 'o', 'm'] # mutable\n", - "print(len(str1))\n", - "print(len(list1))" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "3\n", - "3\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "UaACnox-xfVh", - "outputId": "e8733ac9-6f7d-464a-d57d-849fb0cd1db2", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "list1[0] = 'M'\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['M', 'o', 'm']" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 52 - } - ] - }, + { + "cell_type": "markdown", + "metadata": { + "id": "TgbEMstMxCHY" + }, + "source": [ + "## **List / Array**" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "4oEs6a8fxrNj", - "outputId": "a445ce15-ecb2-47f1-e1be-3522942c13f3", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 85 - } - }, - "source": [ - "print(str1[0])\n", - "print(list1[0])\n", - "print(str1[-1])\n", - "print(list1[-1])" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "T\n", - "M\n", - "m\n", - "m\n" - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "[]" ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Creating empty lists\n", + "list1 = [] # method 1\n", + "list1 = list() # method 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 }, + "id": "OgPVcGHKvcTO", + "outputId": "b58eef75-6484-4332-e72a-0344a2441428" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "pyu0Wm08x7rW", - "outputId": "31b73ad0-711d-437c-e5e6-e3e5d77e1c37", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - } - }, - "source": [ - "for char in list1:\n", - " print(char)" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "M\n", - "o\n", - "m\n" - ], - "name": "stdout" - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "3\n" + ] + } + ], + "source": [ + "str1 = 'Tom' # immutable\n", + "list1 = ['T', 'o', 'm'] # mutable\n", + "print(len(str1))\n", + "print(len(list1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "UaACnox-xfVh", + "outputId": "e8733ac9-6f7d-464a-d57d-849fb0cd1db2" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "cFPui8iHyDYJ", - "outputId": "cd5b3641-a97f-4049-b1a7-556539e90a58", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "list1 = [] # create empty array / list\n", - "list1\n", - "len(list1) # find length\n", - "list1.append('B') # add to the back of array / list\n", - "list1\n", - "list1.append('a')\n", - "list1\n", - "list1.append(3.14)\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['B', 'a', 3.14]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 5 - } + "data": { + "text/plain": [ + "['M', 'o', 'm']" ] + }, + "execution_count": 52, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "list1[0] = 'M'\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 }, + "id": "4oEs6a8fxrNj", + "outputId": "a445ce15-ecb2-47f1-e1be-3522942c13f3" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "B59CDddPyKu6", - "outputId": "0bb0b53a-5091-4f7a-d0c1-78edbf80e8cc", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "list1.insert(1, 'Yo') # add to any position in an array\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['B', 'Yo', 'a', 3.14]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 6 - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "T\n", + "M\n", + "m\n", + "m\n" + ] + } + ], + "source": [ + "print(str1[0])\n", + "print(list1[0])\n", + "print(str1[-1])\n", + "print(list1[-1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 }, + "id": "pyu0Wm08x7rW", + "outputId": "31b73ad0-711d-437c-e5e6-e3e5d77e1c37" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "bVMwgFDMytfX", - "outputId": "3375e227-ad95-4df3-b91e-31b16991af47", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Update\n", - "list1[2] = 35\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['B', 'Yo', 35, 3.14]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 7 - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "M\n", + "o\n", + "m\n" + ] + } + ], + "source": [ + "for char in list1:\n", + " print(char)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### A quick overview of list operations: \n", + "\n", + "| Operation | Parameter | Return |\n", + "| :--------- | :-------- | :------ |\n", + "| len(list) | List | The number of items in a list. |\n", + "| list.append() | An object that has the len of one | Returns None but adds a single element to a list. |\n", + "| list.insert() | Specified index and an object that has the len of one | Returns None but adds the single element to a list based on the index specified. |\n", + "| list.remove(arg) | Any value present in the list | Returns None but removes the first matching element (which is passed as an argument) from the list. |\n", + "| del list[] | Index or range of the value wanted to be deleted from the list | Returns None but deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments. |\n", + "\n", + "*Note that this list is non-exhaustive*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "cFPui8iHyDYJ", + "outputId": "cd5b3641-a97f-4049-b1a7-556539e90a58" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "Ksq7eVuoombD", - "outputId": "0f0cafbe-f5f1-4e34-f593-2eff55987181", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Delete using remove\n", - "list1.remove(3.14)\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['B', 'Yo', 35]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 10 - } + "data": { + "text/plain": [ + "['B', 'a', 3.14]" ] + }, + "execution_count": 5, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "list1 = [] # create empty array / list\n", + "list1\n", + "len(list1) # find length\n", + "list1.append('B') # add to the back of array / list\n", + "list1\n", + "list1.append('a')\n", + "list1\n", + "list1.append(3.14)\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "B59CDddPyKu6", + "outputId": "0bb0b53a-5091-4f7a-d0c1-78edbf80e8cc" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "JZ69baoEpYjv", - "outputId": "d28095d5-15b7-49a0-dfde-cfc5a7d0ceda", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Delete using del\n", - "del list1[0]\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "['Yo', 35]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 11 - } + "data": { + "text/plain": [ + "['B', 'Yo', 'a', 3.14]" ] + }, + "execution_count": 6, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "list1.insert(1, 'Yo') # add to any position in an array\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "bVMwgFDMytfX", + "outputId": "3375e227-ad95-4df3-b91e-31b16991af47" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "_c-1OlUVptye", - "outputId": "4ca98ffa-b0cf-4c48-9159-5fd4f4c4d415", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Read\n", - "for item in list1:\n", - " print(item, end=' ')" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Yo 35 " - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "['B', 'Yo', 35, 3.14]" ] + }, + "execution_count": 7, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Update\n", + "list1[2] = 35\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "Ksq7eVuoombD", + "outputId": "0f0cafbe-f5f1-4e34-f593-2eff55987181" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "-sTFYfcCp4bt", - "outputId": "ee213726-b4c7-434f-af86-5b021b633e77", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 257 - } - }, - "source": [ - "# Program to CRUD list / array\n", - "\n", - "def menu():\n", - " print(\"(1) Insert item\")\n", - " print(\"(2) Update item\")\n", - " print(\"(3) Delete item\")\n", - " print(\"(4) Show all items\")\n", - " print(\"(0) Quit\")\n", - "\n", - "def insert_item(score):\n", - " print(\"Inserting item...\")\n", - " marks.append(score)\n", - "\n", - "def update_item(old_mark):\n", - " print(\"Updating item...\")\n", - " for i in range(len(marks)):\n", - " if marks[i] == old_mark:\n", - " new_mark = int(input(\"Enter new mark: \"))\n", - " marks[i] = new_mark\n", - "\n", - "def delete_item(score):\n", - " print(\"Deleting item...\")\n", - " if score in marks:\n", - " marks.remove(score)\n", - " else:\n", - " print(\"Cannot delete non-existent item\")\n", - "\n", - "def show_items():\n", - " print(\"All items: \", end='')\n", - " if marks != []: # if len(marks) > 0:\n", - " for mark in marks:\n", - " print(mark, end=' ')\n", - " print()\n", - " else: # empty list\n", - " print(\"No marks!\")\n", - "\n", - "# main\n", - "# initialise variables\n", - "choice = ''\n", - "marks = [] # create empty list (global variable)\n", - "#marks = [88, 100, 75] # for testing\n", - "while choice != '0': # while not quit\n", - " menu()\n", - " choice = input(\"Enter choice: \")\n", - " if choice == '1': # insert item\n", - " mark = int(input(\"Enter mark to insert: \"))\n", - " insert_item(mark)\n", - " elif choice == '2': # update item\n", - " mark = int(input(\"Enter mark to update: \"))\n", - " update_item(mark)\n", - " elif choice == '3': # delete item\n", - " mark = int(input(\"Enter mark to delete: \"))\n", - " delete_item(mark)\n", - " elif choice == '4': # show all items\n", - " show_items()\n", - " elif choice == '0':\n", - " print(\"Bye\")\n", - " else: # invalid choice\n", - " print(\"Yo! Choose a valid option between 0 to 5.\")\n" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "(1) Insert item\n", - "(2) Update item\n", - "(3) Delete item\n", - "(4) Show all items\n", - "(0) Quit\n", - "Enter choice: 4\n", - "All items: No marks!\n", - "(1) Insert item\n", - "(2) Update item\n", - "(3) Delete item\n", - "(4) Show all items\n", - "(0) Quit\n", - "Enter choice: 0\n", - "Bye\n" - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "['B', 'Yo', 35]" ] + }, + "execution_count": 10, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Delete using remove\n", + "list1.remove(3.14)\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "JZ69baoEpYjv", + "outputId": "d28095d5-15b7-49a0-dfde-cfc5a7d0ceda" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "_xbbDMl-l0Hb", - "outputId": "dee39fa3-4246-4d76-cca0-c0dbdf8442cb", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 120 - } - }, - "source": [ - "# IMDA-Samsung Solve for Tomorrow Groupings\n", - "import random\n", - "\n", - "studentf = [\"ROSE EVANGELINE ANNE DAGMAN DESTOR\", \"TAN SHIAN PEI\", \\\n", - " \"LIU HONGSHUO\", \"NG JIA XIANG\", \"YEO WERN YEN EDA\", \\\n", - " \"JESSICA CHAN JI JI\"]\n", - "\n", - "studentm = [\"CHEN QIRUI\", \"CHIA QI EN DON\", \"HON WEN HONG\", \"HUANG QIU DI\", \\\n", - " \"ISAAC CHEN JING DE\", \"KHNG CHEN SHUO\", \"KINGOLD WANG XINFENG\", \\\n", - " \"KOH TAT YAO\", \"LEO QIYI JOEL\", \"LIEW ZHENG YUAN\", \"LIN JUNYU\", \\\n", - " \"QIANG JIAYUAN\", \"SEE YONG QIAN\", \"TEH WU RUI\", \\\n", - " \"TOBIAS TAN JIA REN\", \"TOH ZONG HAN KIERAN\", \\\n", - " \"TREVOR ONG YEE SIANG\", \"WANG YAOHUI\"]\n", - "\n", - "random.shuffle(studentf)\n", - "random.shuffle(studentm)\n", - "#print(studentf)\n", - "#print(studentm)\n", - "\n", - "count = 0\n", - "for i in range(6):\n", - " print(studentf[i], studentm[count], studentm[count+1], studentm[count+2])\n", - " count += 3" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "ROSE EVANGELINE ANNE DAGMAN DESTOR TEH WU RUI KOH TAT YAO WANG YAOHUI\n", - "JESSICA CHAN JI JI CHEN QIRUI CHIA QI EN DON TOH ZONG HAN KIERAN\n", - "LIU HONGSHUO TOBIAS TAN JIA REN LIEW ZHENG YUAN TREVOR ONG YEE SIANG\n", - "YEO WERN YEN EDA LIN JUNYU KHNG CHEN SHUO HUANG QIU DI\n", - "TAN SHIAN PEI HON WEN HONG SEE YONG QIAN LEO QIYI JOEL\n", - "NG JIA XIANG ISAAC CHEN JING DE QIANG JIAYUAN KINGOLD WANG XINFENG\n" - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "['Yo', 35]" ] + }, + "execution_count": 11, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Delete using del\n", + "del list1[0]\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "_c-1OlUVptye", + "outputId": "4ca98ffa-b0cf-4c48-9159-5fd4f4c4d415" + }, + "outputs": [ { - "cell_type": "markdown", - "metadata": { - "id": "IutQgcG_yhuw" - }, - "source": [ - "## **Dictionary**\n", - "\n", - "\n", - "* unordered key-value pair\n", - "* key must be unique\n", - "\n" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "Yo 35 " + ] + } + ], + "source": [ + "# Read\n", + "for item in list1:\n", + " print(item, end=' ')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 257 }, + "id": "-sTFYfcCp4bt", + "outputId": "ee213726-b4c7-434f-af86-5b021b633e77" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "A6dsbm9EqJBX", - "outputId": "530294d7-dab8-4d85-a6c2-c727808e34cb", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Create empty dictionary\n", - "d = {}\n", - "d" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 27 - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "(1) Insert item\n", + "(2) Update item\n", + "(3) Delete item\n", + "(4) Show all items\n", + "(0) Quit\n", + "Enter choice: 4\n", + "All items: No marks!\n", + "(1) Insert item\n", + "(2) Update item\n", + "(3) Delete item\n", + "(4) Show all items\n", + "(0) Quit\n", + "Enter choice: 0\n", + "Bye\n" + ] + } + ], + "source": [ + "# Program to CRUD list / array\n", + "\n", + "def menu():\n", + " print(\"(1) Insert item\")\n", + " print(\"(2) Update item\")\n", + " print(\"(3) Delete item\")\n", + " print(\"(4) Show all items\")\n", + " print(\"(0) Quit\")\n", + "\n", + "def insert_item(score):\n", + " print(\"Inserting item...\")\n", + " marks.append(score)\n", + "\n", + "def update_item(old_mark):\n", + " print(\"Updating item...\")\n", + " for i in range(len(marks)):\n", + " if marks[i] == old_mark:\n", + " new_mark = int(input(\"Enter new mark: \"))\n", + " marks[i] = new_mark\n", + "\n", + "def delete_item(score):\n", + " print(\"Deleting item...\")\n", + " if score in marks:\n", + " marks.remove(score)\n", + " else:\n", + " print(\"Cannot delete non-existent item\")\n", + "\n", + "def show_items():\n", + " print(\"All items: \", end='')\n", + " if marks != []: # if len(marks) > 0:\n", + " for mark in marks:\n", + " print(mark, end=' ')\n", + " print()\n", + " else: # empty list\n", + " print(\"No marks!\")\n", + "\n", + "# main\n", + "# initialise variables\n", + "choice = ''\n", + "marks = [] # create empty list (global variable)\n", + "#marks = [88, 100, 75] # for testing\n", + "while choice != '0': # while not quit\n", + " menu()\n", + " choice = input(\"Enter choice: \")\n", + " if choice == '1': # insert item\n", + " mark = int(input(\"Enter mark to insert: \"))\n", + " insert_item(mark)\n", + " elif choice == '2': # update item\n", + " mark = int(input(\"Enter mark to update: \"))\n", + " update_item(mark)\n", + " elif choice == '3': # delete item\n", + " mark = int(input(\"Enter mark to delete: \"))\n", + " delete_item(mark)\n", + " elif choice == '4': # show all items\n", + " show_items()\n", + " elif choice == '0':\n", + " print(\"Bye\")\n", + " else: # invalid choice\n", + " print(\"Yo! Choose a valid option between 0 to 4.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 120 }, + "id": "_xbbDMl-l0Hb", + "outputId": "dee39fa3-4246-4d76-cca0-c0dbdf8442cb" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "jjGhTcrczPS5", - "outputId": "729bb576-740c-412d-a797-012e49be6e5f", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Insert\n", - "d['Tom'] = 88\n", - "d" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'Tom': 88}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 28 - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "ROSE EVANGELINE ANNE DAGMAN DESTOR TEH WU RUI KOH TAT YAO WANG YAOHUI\n", + "JESSICA CHAN JI JI CHEN QIRUI CHIA QI EN DON TOH ZONG HAN KIERAN\n", + "LIU HONGSHUO TOBIAS TAN JIA REN LIEW ZHENG YUAN TREVOR ONG YEE SIANG\n", + "YEO WERN YEN EDA LIN JUNYU KHNG CHEN SHUO HUANG QIU DI\n", + "TAN SHIAN PEI HON WEN HONG SEE YONG QIAN LEO QIYI JOEL\n", + "NG JIA XIANG ISAAC CHEN JING DE QIANG JIAYUAN KINGOLD WANG XINFENG\n" + ] + } + ], + "source": [ + "# IMDA-Samsung Solve for Tomorrow Groupings\n", + "import random\n", + "\n", + "studentf = [\"ROSE EVANGELINE ANNE DAGMAN DESTOR\", \"TAN SHIAN PEI\", \\\n", + " \"LIU HONGSHUO\", \"NG JIA XIANG\", \"YEO WERN YEN EDA\", \\\n", + " \"JESSICA CHAN JI JI\"]\n", + "\n", + "studentm = [\"CHEN QIRUI\", \"CHIA QI EN DON\", \"HON WEN HONG\", \"HUANG QIU DI\", \\\n", + " \"ISAAC CHEN JING DE\", \"KHNG CHEN SHUO\", \"KINGOLD WANG XINFENG\", \\\n", + " \"KOH TAT YAO\", \"LEO QIYI JOEL\", \"LIEW ZHENG YUAN\", \"LIN JUNYU\", \\\n", + " \"QIANG JIAYUAN\", \"SEE YONG QIAN\", \"TEH WU RUI\", \\\n", + " \"TOBIAS TAN JIA REN\", \"TOH ZONG HAN KIERAN\", \\\n", + " \"TREVOR ONG YEE SIANG\", \"WANG YAOHUI\"]\n", + "\n", + "random.shuffle(studentf)\n", + "random.shuffle(studentm)\n", + "#print(studentf)\n", + "#print(studentm)\n", + "\n", + "count = 0\n", + "for i in range(6):\n", + " print(studentf[i], studentm[count], studentm[count+1], studentm[count+2])\n", + " count += 3" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IutQgcG_yhuw" + }, + "source": [ + "## **Dictionary**\n", + "\n", + "\n", + "* unordered key-value pair\n", + "* key must be unique\n", + "* keys will be a single element\n", + "* values can be a list or list within a list, numbers, etc.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "A6dsbm9EqJBX", + "outputId": "530294d7-dab8-4d85-a6c2-c727808e34cb" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "OpskJv7kzXDM", - "outputId": "5d5c3847-12f6-4880-e84f-fb3ecb0b8804", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "d['Mary'] = 100\n", - "d" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'Mary': 100, 'Tom': 88}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 29 - } + "data": { + "text/plain": [ + "{}" ] + }, + "execution_count": 27, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Create empty dictionary\n", + "d = {}\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "jjGhTcrczPS5", + "outputId": "729bb576-740c-412d-a797-012e49be6e5f" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "L6OnH4v-zhoF", - "outputId": "d656f13f-3dd6-4e6d-8690-d07b08ca2897", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "d['Tom'] = 35 # update if key exists\n", - "d" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'Mary': 100, 'Tom': 35}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 30 - } + "data": { + "text/plain": [ + "{'Tom': 88}" ] + }, + "execution_count": 28, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Insert\n", + "d['Tom'] = 88\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "OpskJv7kzXDM", + "outputId": "5d5c3847-12f6-4880-e84f-fb3ecb0b8804" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "VmLVJLndzs3z", - "outputId": "347055e4-092d-4c42-cc7d-f56ab1bc7e1a", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "# Delete\n", - "del d['Tom']\n", - "d" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'Mary': 100}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 31 - } + "data": { + "text/plain": [ + "{'Mary': 100, 'Tom': 88}" ] + }, + "execution_count": 29, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "d['Mary'] = 100\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "L6OnH4v-zhoF", + "outputId": "d656f13f-3dd6-4e6d-8690-d07b08ca2897" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "nD26Uu170LOr", - "outputId": "598658fb-122d-40b7-dd1c-e3490f459b76", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 - } - }, - "source": [ - "# Show\n", - "d = {'Mary': 100, 'Tom': 35}\n", - "d.keys() # show keys\n", - "d.values() # show values\n", - "for key, value in d.items():\n", - " print(key, value)" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Mary 100\n", - "Tom 35\n" - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "{'Mary': 100, 'Tom': 35}" ] + }, + "execution_count": 30, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "d['Tom'] = 35 # update if key exists\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "VmLVJLndzs3z", + "outputId": "347055e4-092d-4c42-cc7d-f56ab1bc7e1a" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "9XWjPlHo0QNt", - "outputId": "1d321a08-404f-4973-c2da-6685ddcf46bb", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 1000 - } - }, - "source": [ - "# CRUD program for dictionary\n", - "def menu():\n", - " print(\"(1) Insert\")\n", - " print(\"(2) Delete\")\n", - " print(\"(3) Update\")\n", - " print(\"(4) Show\")\n", - " print(\"(0) Quit\")\n", - "\n", - "def insert(key, value):\n", - " if key in contacts.keys():\n", - " print(\"Contact already exists!\")\n", - " else:\n", - " contacts[key] = value\n", - "\n", - "def delete(key):\n", - " if key in contacts.keys():\n", - " del contacts[key]\n", - " else:\n", - " print(\"Contact not found!\")\n", - "\n", - "def update(key, new_value):\n", - " if key in contacts.keys():\n", - " contacts[key] = new_value\n", - " else:\n", - " print(\"Contact not found!\")\n", - "\n", - "def show():\n", - " if contacts == {}:\n", - " print(\"You have no contacts!\")\n", - " else:\n", - " for key, value in contacts.items():\n", - " print(key, value)\n", - "\n", - "\n", - "# main\n", - "contacts = {}\n", - "option = ''\n", - "while option != '0': # while not quit\n", - " menu()\n", - " option = input(\"Enter option: \")\n", - " if option == '1':\n", - " name = input(\"Enter name: \")\n", - " mobile = input(\"Enter mobile: \")\n", - " insert(name, mobile)\n", - " elif option == '2':\n", - " name = input(\"Enter name: \")\n", - " delete(name)\n", - " elif option == '3':\n", - " name = input(\"Enter name: \")\n", - " new_mobile = input(\"Enter new mobile: \") \n", - " update(name, new_mobile)\n", - " elif option == '4':\n", - " show()\n", - " elif option == '0':\n", - " print(\"Bye\")\n", - " else:\n", - " print(\"Invalid option!\")\n", - "print(\"Done\")" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 1\n", - "Enter name: Tom\n", - "Enter mobile: 12345678\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 4\n", - "Tom 12345678\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 1\n", - "Enter name: Tom\n", - "Enter mobile: 12345678\n", - "Contact already exists!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 3\n", - "Enter name: Mary\n", - "Enter new mobile: 87654321\n", - "Contact not found!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 2\n", - "Enter name: Mary\n", - "Contact not found!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 2\n", - "Enter name: Tom\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 4\n", - "You have no contacts!\n", - "(1) Insert\n", - "(2) Delete\n", - "(3) Update\n", - "(4) Show\n", - "(0) Quit\n", - "Enter option: 0\n", - "Bye\n", - "Done\n" - ], - "name": "stdout" - } + "data": { + "text/plain": [ + "{'Mary': 100}" ] + }, + "execution_count": 31, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "# Delete\n", + "del d['Tom']\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 }, + "id": "nD26Uu170LOr", + "outputId": "598658fb-122d-40b7-dd1c-e3490f459b76" + }, + "outputs": [ { - "cell_type": "markdown", - "metadata": { - "id": "2I7O8CWjtjoX" - }, - "source": [ - "## **Tuple**\n", - "\n", - "* Behaves like a list but is immutable\n", - "\n" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "Mary 100\n", + "Tom 35\n" + ] + } + ], + "source": [ + "# Show\n", + "d = {'Mary': 100, 'Tom': 35}\n", + "d.keys() # show keys\n", + "d.values() # show values\n", + "for key, value in d.items():\n", + " print(key, value)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 }, + "id": "9XWjPlHo0QNt", + "outputId": "1d321a08-404f-4973-c2da-6685ddcf46bb" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "t5Xhqe6W5Rpi", - "outputId": "8fba3785-0200-4ec3-cc15-d25f2d57197e", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - } - }, - "source": [ - "list1 = [1, 2, 3]\n", - "tuple1 = (1, 2, 3)\n", - "\n", - "for i in range(3):\n", - " print(list1[i], tuple1[i])" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "1 1\n", - "2 2\n", - "3 3\n" - ], - "name": "stdout" - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 1\n", + "Enter name: Tom\n", + "Enter mobile: 12345678\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 4\n", + "Tom 12345678\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 1\n", + "Enter name: Tom\n", + "Enter mobile: 12345678\n", + "Contact already exists!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 3\n", + "Enter name: Mary\n", + "Enter new mobile: 87654321\n", + "Contact not found!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 2\n", + "Enter name: Mary\n", + "Contact not found!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 2\n", + "Enter name: Tom\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 4\n", + "You have no contacts!\n", + "(1) Insert\n", + "(2) Delete\n", + "(3) Update\n", + "(4) Show\n", + "(0) Quit\n", + "Enter option: 0\n", + "Bye\n", + "Done\n" + ] + } + ], + "source": [ + "# CRUD program for dictionary\n", + "def menu():\n", + " print(\"(1) Insert\")\n", + " print(\"(2) Delete\")\n", + " print(\"(3) Update\")\n", + " print(\"(4) Show\")\n", + " print(\"(0) Quit\")\n", + "\n", + "def insert(key, value):\n", + " if key in contacts.keys():\n", + " print(\"Contact already exists!\")\n", + " else:\n", + " contacts[key] = value\n", + "\n", + "def delete(key):\n", + " if key in contacts.keys():\n", + " del contacts[key]\n", + " else:\n", + " print(\"Contact not found!\")\n", + "\n", + "def update(key, new_value):\n", + " if key in contacts.keys():\n", + " contacts[key] = new_value\n", + " else:\n", + " print(\"Contact not found!\")\n", + "\n", + "def show():\n", + " if contacts == {}:\n", + " print(\"You have no contacts!\")\n", + " else:\n", + " for key, value in contacts.items():\n", + " print(key, value)\n", + "\n", + "\n", + "# main\n", + "contacts = {}\n", + "option = ''\n", + "while option != '0': # while not quit\n", + " menu()\n", + " option = input(\"Enter option: \")\n", + " if option == '1':\n", + " name = input(\"Enter name: \")\n", + " mobile = input(\"Enter mobile: \")\n", + " insert(name, mobile)\n", + " elif option == '2':\n", + " name = input(\"Enter name: \")\n", + " delete(name)\n", + " elif option == '3':\n", + " name = input(\"Enter name: \")\n", + " new_mobile = input(\"Enter new mobile: \") \n", + " update(name, new_mobile)\n", + " elif option == '4':\n", + " show()\n", + " elif option == '0':\n", + " print(\"Bye\")\n", + " else:\n", + " print(\"Invalid option!\")\n", + "print(\"Done\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2I7O8CWjtjoX" + }, + "source": [ + "## **Tuple**\n", + "\n", + "* Behaves like a list but is immutable\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 }, + "id": "t5Xhqe6W5Rpi", + "outputId": "8fba3785-0200-4ec3-cc15-d25f2d57197e" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "MBexEYpzuBIM", - "outputId": "9d0b327a-53a9-490b-b783-1dffe73de975", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "list1[0] = 35\n", - "list1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "[35, 2, 3]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 5 - } - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "1 1\n", + "2 2\n", + "3 3\n" + ] + } + ], + "source": [ + "list1 = [1, 2, 3]\n", + "tuple1 = (1, 2, 3)\n", + "\n", + "for i in range(3):\n", + " print(list1[i], tuple1[i])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "MBexEYpzuBIM", + "outputId": "9d0b327a-53a9-490b-b783-1dffe73de975" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "pEN5t07puJCq", - "outputId": "8181d29b-c7b6-403f-ce75-1d25459d928b", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 182 - } - }, - "source": [ - "tuple1[0] = 35 # error because tuple cannot be changed\n", - "tuple1" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "error", - "ename": "TypeError", - "evalue": "ignored", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtuple1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m35\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mtuple1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" - ] - } + "data": { + "text/plain": [ + "[35, 2, 3]" ] + }, + "execution_count": 5, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "list1[0] = 35\n", + "list1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 182 }, + "id": "pEN5t07puJCq", + "outputId": "8181d29b-c7b6-403f-ce75-1d25459d928b" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "aO6W_CT4uNoU", - "outputId": "f0151f87-5687-4799-bb6c-8fa3245e6992", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "type(tuple1)" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "tuple" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 7 - } - ] + "ename": "TypeError", + "evalue": "ignored", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtuple1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m35\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mtuple1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "tuple1[0] = 35 # error because tuple cannot be changed\n", + "tuple1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "aO6W_CT4uNoU", + "outputId": "f0151f87-5687-4799-bb6c-8fa3245e6992" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "StFLjmpxuqAa", - "outputId": "a58c046b-3642-411a-9dd2-3c9830e17c98", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "tuple2 = (35,)\n", - "type(tuple2)" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "tuple" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 10 - } + "data": { + "text/plain": [ + "tuple" ] + }, + "execution_count": 7, + "metadata": { + "tags": [] + }, + "output_type": "execute_result" + } + ], + "source": [ + "type(tuple1)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 }, + "id": "StFLjmpxuqAa", + "outputId": "a58c046b-3642-411a-9dd2-3c9830e17c98" + }, + "outputs": [ { - "cell_type": "code", - "metadata": { - "id": "DmwXn68quuFp" - }, - "source": [ - "" - ], - "execution_count": null, - "outputs": [] + "name": "stdout", + "output_type": "stream", + "text": [ + "tuple2 is \n", + "tuple3 is \n" + ] } - ] + ], + "source": [ + "tuple2 = (35,)\n", + "print('tuple2 is', type(tuple2))\n", + "\n", + "tuple3 = 60,\n", + "print('tuple3 is', type(tuple3))\n", + "\n", + "# Hence, be careful when setting variables as a comma can make it into a tuple" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "DmwXn68quuFp" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "collapsed_sections": [], + "name": "Python06 - Array & Dictionary.ipynb", + "provenance": [] + }, + "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.2" + } + }, + "nbformat": 4, + "nbformat_minor": 1 }