Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
393 changes: 393 additions & 0 deletions Lists.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,393 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Lists.ipynb",
"version": "0.3.2",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"[View in Colaboratory](https://colab.research.google.com/github/sumitra1/Assignment-2/blob/sumitra1/Lists.ipynb)"
]
},
{
"metadata": {
"id": "e0R1W0Vzm4UU",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# Exercise - List"
]
},
{
"metadata": {
"id": "TrO7XNQnnQZ7",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"1) Create any random list and assign it to a variable dummy_list"
]
},
{
"metadata": {
"id": "bjl-2QkznWid",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"dummy_list = [84,65.6,65,8.3,80.83,3.2,74.73,74,99.99,9.5]"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "cDjddNGfngnp",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"2) print dummy_list"
]
},
{
"metadata": {
"id": "RVL5178inz9M",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "22774ca7-934e-4958-ad45-b8cbc7410142"
},
"cell_type": "code",
"source": [
"print(dummy_list)"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"[84, 65.6, 65, 8.3, 80.83, 3.2, 74.73, 74, 99.99, 9.5]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "15jKDXxkn16M",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"3) Reverse dummy_list and print"
]
},
{
"metadata": {
"id": "bYa9gFOOn-4o",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "8afe6cd6-6699-413b-f733-e68a820f160a"
},
"cell_type": "code",
"source": [
"dummy_list.reverse()\n",
"print(dummy_list)"
],
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": [
"[9.5, 99.99, 74, 74.73, 3.2, 80.83, 8.3, 65, 65.6, 84]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "EShv0nfXpUys",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"4) Add the list dummy_list_2 to the previous dummy_list and now print dummy_list"
]
},
{
"metadata": {
"id": "Ngkc7hnYphg6",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "19d85b35-c291-42f7-a92c-5536b9f9bef7"
},
"cell_type": "code",
"source": [
"dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n",
"dummy_list += dummy_list_2\n",
"print(dummy_list)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"[9.5, 99.99, 74, 74.73, 3.2, 80.83, 8.3, 65, 65.6, 84, 2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "Le1aRTuYoDzS",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"5) Create a dictionary named dummy_dict which contains all the elements of dummy_list as keys and frequency as values. "
]
},
{
"metadata": {
"id": "VHfSR_Csthnk",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"dummy_dict = {}"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "rtPC6WkSos-y",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"\n",
"for item in range(len(dummy_list)):\n",
" dummy_dict[dummy_list[item]] = dummy_list.count(dummy_list[item])\n"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "RgCYpFXGou6q",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"6) print dummy_dict"
]
},
{
"metadata": {
"id": "qe5E5IgxpTWU",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
},
"outputId": "564a1b7b-37af-42d3-9146-653a2e903df1"
},
"cell_type": "code",
"source": [
"print(dummy_dict)\n"
],
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": [
"{9.5: 1, 99.99: 1, 74: 1, 74.73: 1, 3.2: 1, 80.83: 1, 8.3: 1, 65: 1, 65.6: 1, 84: 1, 2: 1, 200: 1, 16: 1, 4: 1, 1: 1, 0: 1, 9.45: 1, 45.67: 1, 90: 1, 12.01: 1, 12.02: 1}\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "8n_nsBDup4--",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"7) Sort dummy_list in ascending order as well as descending order and print the changed lists "
]
},
{
"metadata": {
"id": "Z_m7vr26qKnK",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "a65d6fa7-e8c2-4591-9e0c-f0a20e58280f"
},
"cell_type": "code",
"source": [
"dummy_list.sort()\n",
"print(dummy_list)\n",
"dummy_list.reverse()\n",
"print(dummy_list)"
],
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": [
"[0, 1, 2, 3.2, 4, 8.3, 9.45, 9.5, 12.01, 12.02, 16, 45.67, 65, 65.6, 74, 74.73, 80.83, 84, 90, 99.99, 200]\n",
"[200, 99.99, 90, 84, 80.83, 74.73, 74, 65.6, 65, 45.67, 16, 12.02, 12.01, 9.5, 9.45, 8.3, 4, 3.2, 2, 1, 0]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "Znm5Qo4LqPKA",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"8) Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item."
]
},
{
"metadata": {
"id": "1-8mlngDqYvS",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 231
},
"outputId": "1c84ca38-f79d-4431-b30b-e64bfc29e837"
},
"cell_type": "code",
"source": [
"x = 200\n",
"#dummy_list.insert(0,200)\n",
"dummy_list.remove(x)\n",
"print(dummy_list)\n",
"\n",
"# Let's play: try the same with something which is not in the list to get the ValueError\n",
"dummy_list.remove(200)\n",
"# 200 is not in the dummy_list , so we get error\n"
],
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": [
"[99.99, 90, 84, 80.83, 74.73, 74, 65.6, 65, 45.67, 16, 12.02, 12.01, 9.5, 9.45, 8.3, 4, 3.2, 2, 1, 0]\n"
],
"name": "stdout"
},
{
"output_type": "error",
"ename": "ValueError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-12-7bcb43d41f6b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;31m# Let's play: try the same with something which is not in the list to get the ValueError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mdummy_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;31m# 200 is not in the dummy_list , so we get error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list"
]
}
]
},
{
"metadata": {
"id": "QPB6iGbeqviN",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"9) Remove the item at position x. x is any random integer"
]
},
{
"metadata": {
"id": "aMyo1gmRrVHo",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "bqQnnsr8rm6G",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"10) Let's clean everything clear the list and then print"
]
},
{
"metadata": {
"id": "qBC8lKpLrtJW",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "8d327a40-1298-4a35-9b88-3e8dce54024f"
},
"cell_type": "code",
"source": [
"dummy_list.clear()\n",
"print(dummy_list)"
],
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": [
"[]\n"
],
"name": "stdout"
}
]
}
]
}
Loading