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/sayanmondal31/Assignment-2/blob/sayanmondal31/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=[12,13,15,14,16]"
],
"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": 33
},
"outputId": "6e09e49b-ae3a-466f-e34c-934da5fd7bf6"
},
"cell_type": "code",
"source": [
"print(dummy_list)"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"[12, 13, 15, 14, 16]\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": 33
},
"outputId": "dab6fa13-3329-474e-8932-91d5cddbb6b6"
},
"cell_type": "code",
"source": [
"dummy_list.reverse()\n",
"print(dummy_list)"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"[16, 14, 15, 13, 12]\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": 33
},
"outputId": "2e0eb0c5-7cd7-45f0-8bd7-9a75384d5981"
},
"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 + dummy_list_2\n",
"print(dummy_list)"
],
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": [
"[16, 15, 14, 13, 12, 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={\"keys\":\"dummy_list\",\"values\":\"frequency\"}"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "rtPC6WkSos-y",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
""
],
"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": 33
},
"outputId": "09b92109-c2ea-4fbf-8c59-c6e9e0bc14f9"
},
"cell_type": "code",
"source": [
"print(dummy_dict)"
],
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": [
"{'keys': 'dummy_list', 'values': 'frequency'}\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": 50
},
"outputId": "1f7c57de-9f9b-400f-eced-529372acc45a"
},
"cell_type": "code",
"source": [
"dummy_list.sort()\n",
"print(\"ascending order \",dummy_list)\n",
"dummy_list.sort(reverse=True)\n",
"print(\"descending order \",dummy_list)"
],
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": [
"ascending order [0, 1, 2, 4, 9.45, 12, 12.01, 12.02, 13, 14, 15, 16, 16, 45.67, 90, 200]\n",
"descending order [200, 90, 45.67, 16, 16, 15, 14, 13, 12.02, 12.01, 12, 9.45, 4, 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": 33
},
"outputId": "e4b6f5a3-1ad6-4334-9ea2-9f1836506c2b"
},
"cell_type": "code",
"source": [
"x = 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"
],
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"text": [
"[90, 45.67, 16, 16, 15, 14, 13, 12.02, 12.01, 12, 9.45, 4, 2, 1, 0]\n"
],
"name": "stdout"
}
]
},
{
"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": {
"base_uri": "https://localhost:8080/",
"height": 33
},
"outputId": "cb1345ba-00f4-44cc-c879-0043b7dc4b87"
},
"cell_type": "code",
"source": [
"# Let's play: try doing the same with x > len(dummy_list) + 1 and see what you get\n",
"x = x>len(dummy_list)+1\n",
"dummy_list.remove(x)\n",
"print(dummy_list)\n",
"\n"
],
"execution_count": 17,
"outputs": [
{
"output_type": "stream",
"text": [
"[90, 45.67, 16, 16, 15, 14, 13, 12.02, 12.01, 12, 9.45, 4, 2, 0]\n"
],
"name": "stdout"
}
]
},
{
"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": 33
},
"outputId": "f4d3e45e-92e9-4c47-d913-087ea350c42f"
},
"cell_type": "code",
"source": [
"dummy_list.clear()\n",
"print(dummy_list)\n"
],
"execution_count": 18,
"outputs": [
{
"output_type": "stream",
"text": [
"[]\n"
],
"name": "stdout"
}
]
}
]
}
Loading