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": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"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": [
"import random\n",
"dummy_list = [0]\n",
"for x in range(10):\n",
" x = random.randint(1,999)\n",
" dummy_list.append(x)"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "cDjddNGfngnp",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"2) print dummy_list"
]
},
{
"metadata": {
"id": "RVL5178inz9M",
"colab_type": "code",
"outputId": "498ef785-47a4-4a5b-ef9b-8f536a7e9928",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"cell_type": "code",
"source": [
"print(dummy_list)"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"[0, 354, 225, 129, 442, 101, 71, 584, 580, 553, 216]\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",
"outputId": "f4d178ac-5227-4ab0-a9a5-0ddfedbe94a5",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"cell_type": "code",
"source": [
"dummy_list.reverse()\n",
"print(dummy_list)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"[216, 553, 580, 584, 71, 101, 442, 129, 225, 354, 0]\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",
"outputId": "2507ad53-6f9e-4bed-e74f-e174452b7ca5",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"cell_type": "code",
"source": [
"dummy_list_2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n",
"x = len(dummy_list_2)\n",
"for i in range(0,x): #to eliminate the '[]' from the list\n",
" dummy_list.append(dummy_list_2[i])\n",
"print(dummy_list)"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"[216, 553, 580, 584, 71, 101, 442, 129, 225, 354, 0, 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 = {}\n",
"total = len(dummy_list)\n",
"for i in range(total):\n",
" temp = dummy_list[i]\n",
" dummy_dict[temp] = dummy_list.count(temp)"
],
"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",
"outputId": "d3c5edcf-e7f5-4790-9a27-7654e7412a96",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"cell_type": "code",
"source": [
"print(dummy_dict)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"{216: 1, 553: 1, 580: 1, 584: 1, 71: 1, 101: 1, 442: 1, 129: 1, 225: 1, 354: 1, 0: 2, 2: 1, 200: 1, 16: 1, 4: 1, 1: 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",
"outputId": "b93e549e-60d2-4b22-ab22-22d723f43b8b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
}
},
"cell_type": "code",
"source": [
"dummy_list.sort()\n",
"print(dummy_list)\n",
"dummy_list.sort(reverse=True)\n",
"print(dummy_list)"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"[0, 0, 1, 2, 4, 9.45, 12.01, 12.02, 16, 45.67, 71, 90, 101, 129, 200, 216, 225, 354, 442, 553, 580, 584]\n",
"[584, 580, 553, 442, 354, 225, 216, 200, 129, 101, 90, 71, 45.67, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0, 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",
"outputId": "eaa954d6-91c4-4556-878b-457bd0f59a97",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"cell_type": "code",
"source": [
"x = 200\n",
"dummy_list.remove(x)\n",
"print(dummy_list)\n",
"\n",
"#dummy_list.remove(13) \n",
"#ValueError Traceback (most recent call last)\n",
"#<ipython-input-11-0c23785c7afd> in <module>()\n",
"# 1 x = 200\n",
"#---> 2 dummy_list.remove(x)\n",
"# 3 print(dummy_list)\n",
"# 4 dummy_list.remove(13)\n",
"# 5 \n",
"#\n",
"#ValueError: list.remove(x): x not in list\n",
"# Let's play: try the same with something which is not in the list to get the ValueError"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"[584, 580, 553, 442, 354, 225, 216, 129, 101, 90, 71, 45.67, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0, 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": "NakdzAYoRLwv",
"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\n",
"import random\n",
"x = random.randint(0,len(dummy_list))\n",
"dummy_list.remove(x)\n",
"\n",
"#to get the value errors\n",
"#x = len(dummy_list) + 1\n",
"#dummy_list.remove(x)"
],
"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": "aMyo1gmRrVHo",
"colab_type": "code",
"outputId": "e932f27d-579e-482f-e913-77df3c2ec5e9",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"cell_type": "code",
"source": [
"dummy_list.clear()\n",
"print(dummy_list)"
],
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": [
"[]\n"
],
"name": "stdout"
}
]
}
]
}
Loading