{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [ "'''\n", "In Python, a list is a built-in data structure used to store multiple items in a single variable.\n", "Lists are:\n", " Ordered (items maintain their position)\n", " Mutable (can change elements)\n", " Allow duplicates\n", " Can hold any data type (integers, strings, other lists, etc.)\n", "'''" ], "metadata": { "id": "XVur89h_ESnL" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Creating a List\n", "\n", "# Empty list\n", "my_list = []\n", "print(my_list)\n", "my_list1 = list() # using constructor\n", "print(my_list1)\n", "\n", "# List of numbers\n", "numbers = [1, 2, 3, 4, 5]\n", "print(numbers)\n", "\n", "# List of strings\n", "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "print(fruits)\n", "\n", "# List with mixed data types\n", "mixed = [1, \"MS Dhoni\", 7.0, True]\n", "print(mixed)\n", "\n", "# Nested list\n", "nested = [1, [2, 3], 4]\n", "print(nested)\n", "\n", "print(type(mixed), type(nested)) # all are lists only!" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5k59AgplESqQ", "outputId": "d8d525ee-5fff-4a92-85dc-ea73184a6d13" }, "execution_count": 1, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[]\n", "[]\n", "[1, 2, 3, 4, 5]\n", "['apple', 'banana', 'cherry']\n", "[1, 'MS Dhoni', 7.0, True]\n", "[1, [2, 3], 4]\n", " \n" ] } ] }, { "cell_type": "code", "source": [ "# Accessing List Elements\n", "\n", "print(numbers[0]) # 1 (first element)\n", "print(numbers[-1]) # 5 (last element)\n", "print(nested[1][1]) # 3" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZbGurXseEStJ", "outputId": "b6ac0cd2-13f1-42fe-da38-8ed8eee1e8dd" }, "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1\n", "5\n", "3\n" ] } ] }, { "cell_type": "code", "source": [ "# Modifying a List\n", "numbers[0] = 10 # Change 1 to 10\n", "print(numbers) # [10, 2, 3, 4, 5]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "o1UwXPFuESyg", "outputId": "3be8396e-dbe4-4442-968e-11451f8dadae" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[10, 2, 3, 4, 5]\n" ] } ] }, { "cell_type": "code", "source": [ "# List Methods\n", "\n", "# append(): Adds an item to the end of the list\n", "players = ['Dhoni', 'Kohli']\n", "players.append('Raina')\n", "print(\"append:\", players)\n", "print()\n", "# clear(): Removes all items from the list\n", "players.clear()\n", "print(\"clear:\", players)\n", "print()\n", "# copy(): Returns a shallow copy of the list\n", "players = ['Dhoni', 'Kohli']\n", "team_copy = players.copy()\n", "print(\"copy:\", team_copy)\n", "print()\n", "# count(): Returns the number of times a value appears\n", "jerseys = [7, 18, 7, 45, 7]\n", "print(\"count:\", jerseys.count(7))\n", "print()\n", "# extend(): Adds all elements from another iterable\n", "team1 = ['Dhoni']\n", "team2 = ['Kohli', 'Raina']\n", "team1.extend(team2)\n", "print(\"extend:\", team1)\n", "print()\n", "# index(): Returns the index of the first matching value\n", "players = ['Dhoni', 'Kohli', 'Raina', 'Kohli']\n", "print(\"index:\", players.index('Kohli'))\n", "print(\"index after 2:\", players.index('Kohli', 2))\n", "print()\n", "# insert(): Inserts an item at a given index\n", "players.insert(1, 'Jadeja')\n", "print(\"insert:\", players)\n", "print()\n", "# pop(): Removes and returns the item at the given index (default last)\n", "last_player = players.pop()\n", "print(\"pop:\", last_player, players)\n", "print()\n", "# remove(): Removes the first matching value\n", "players.remove('Jadeja') # value based removal; check if the element is present before removal\n", "print(\"remove:\", players)\n", "print()\n", "# reverse(): Reverses the list in-place\n", "players.reverse()\n", "print(\"reverse:\", players)\n", "print()\n", "# sort(): Sorts the list in-place (ascending by default)\n", "scores = [100, 60, 45, 7]\n", "scores.sort()\n", "print(\"sort:\", scores)\n", "scores.sort(reverse = True)\n", "print(\"sort (descending):\", scores)\n", "print()\n", "# sorted(): Returns a new sorted list without modifying the original\n", "unsorted_scores = [90, 20, 50]\n", "print(\"sorted:\", sorted(unsorted_scores, reverse = True))\n", "print(\"original:\", unsorted_scores)\n", "print(\"sorted (descending):\", sorted(unsorted_scores))\n", "print(\"original:\", unsorted_scores)\n", "print()\n", "# reversed(): Returns a reversed iterator (not in-place)\n", "players = ['Dhoni', 'Kohli', 'Raina']\n", "print(\"reversed:\", list(reversed(players)))\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "H7Vh9KknES1R", "outputId": "4d274889-dea4-499c-aedf-c55aa8432084" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "append: ['Dhoni', 'Kohli', 'Raina']\n", "\n", "clear: []\n", "\n", "copy: ['Dhoni', 'Kohli']\n", "\n", "count: 3\n", "\n", "extend: ['Dhoni', 'Kohli', 'Raina']\n", "\n", "index: 1\n", "index after 2: 3\n", "\n", "insert: ['Dhoni', 'Jadeja', 'Kohli', 'Raina', 'Kohli']\n", "\n", "pop: Kohli ['Dhoni', 'Jadeja', 'Kohli', 'Raina']\n", "\n", "remove: ['Dhoni', 'Kohli', 'Raina']\n", "\n", "reverse: ['Raina', 'Kohli', 'Dhoni']\n", "\n", "sort: [7, 45, 60, 100]\n", "sort (descending): [100, 60, 45, 7]\n", "\n", "sorted: [90, 50, 20]\n", "original: [90, 20, 50]\n", "sorted (descending): [20, 50, 90]\n", "original: [90, 20, 50]\n", "\n", "reversed: ['Raina', 'Kohli', 'Dhoni']\n" ] } ] }, { "cell_type": "code", "source": [ "# Iterating through list\n", "\n", "items = [7, \"Dhoni\", 7.7, True, None, 3+14j, [\"Ziva\", \"CSK\"], (\"IPL\", \"MI\"), {\"Best\": \"CSK\"}, {\"CSK\", \"MI\", \"RCB\"}]" ], "metadata": { "id": "h5YWTMGEES9o" }, "execution_count": 5, "outputs": [] }, { "cell_type": "code", "source": [ "print(\"Using range() and indexing\")\n", "for i in range(len(items)):\n", " print(f\"Index {i}: {items[i]} --> {type(items[i])}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DyebVLT9ES6x", "outputId": "7ee9f102-56ee-444c-bee3-f6d2c732c642" }, "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Using range() and indexing\n", "Index 0: 7 --> \n", "Index 1: Dhoni --> \n", "Index 2: 7.7 --> \n", "Index 3: True --> \n", "Index 4: None --> \n", "Index 5: (3+14j) --> \n", "Index 6: ['Ziva', 'CSK'] --> \n", "Index 7: ('IPL', 'MI') --> \n", "Index 8: {'Best': 'CSK'} --> \n", "Index 9: {'RCB', 'MI', 'CSK'} --> \n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Sequence-based (direct iteration)\")\n", "for i in items:\n", " print(f\"Item: {i}\") # can't access index directly\n", "# but complicated [] notation is simplified." ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "em6MfEcRETAY", "outputId": "cf57448c-4997-40bd-b034-595109731e3c" }, "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Sequence-based (direct iteration)\n", "Item: 7\n", "Item: Dhoni\n", "Item: 7.7\n", "Item: True\n", "Item: None\n", "Item: (3+14j)\n", "Item: ['Ziva', 'CSK']\n", "Item: ('IPL', 'MI')\n", "Item: {'Best': 'CSK'}\n", "Item: {'RCB', 'MI', 'CSK'}\n" ] } ] }, { "cell_type": "code", "source": [ "idx = 0\n", "for i in items:\n", " print(f\"Index {idx}: {i} --> {type(i)}\")\n", " idx += 1" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "afuRNT6uJr9M", "outputId": "0e40551a-d218-4f4e-eb72-f911dbfaef71" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Index 0: 7 --> \n", "Index 1: Dhoni --> \n", "Index 2: 7.7 --> \n", "Index 3: True --> \n", "Index 4: None --> \n", "Index 5: (3+14j) --> \n", "Index 6: ['Ziva', 'CSK'] --> \n", "Index 7: ('IPL', 'MI') --> \n", "Index 8: {'Best': 'CSK'} --> \n", "Index 9: {'RCB', 'MI', 'CSK'} --> \n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Using enumerate() (index + value)\")\n", "for idx, val in enumerate(items): # hybrid mode of type 1 and 2.\n", " print(f\"Index {idx}: {val} --> {type(i)}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8E235VZ5ETDR", "outputId": "5eaa68b8-dc00-42f0-bfb5-6615bfd0e0d0" }, "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Using enumerate() (index + value)\n", "Index 0: 7 --> \n", "Index 1: Dhoni --> \n", "Index 2: 7.7 --> \n", "Index 3: True --> \n", "Index 4: None --> \n", "Index 5: (3+14j) --> \n", "Index 6: ['Ziva', 'CSK'] --> \n", "Index 7: ('IPL', 'MI') --> \n", "Index 8: {'Best': 'CSK'} --> \n", "Index 9: {'RCB', 'MI', 'CSK'} --> \n" ] } ] }, { "cell_type": "code", "source": [ "print(\"Using zip() for combining two lists\") # advancement of type 2\n", "types = []\n", "for i in items:\n", " types.append(type(i))\n", "\n", "for val, dt in zip(items, types):\n", " print(f\"{val}: {dt}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "boN_Fo7gETGK", "outputId": "c6959cef-0240-4917-ff47-9e545fdd0726" }, "execution_count": 10, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Using zip() for combining two lists\n", "7: \n", "Dhoni: \n", "7.7: \n", "True: \n", "None: \n", "(3+14j): \n", "['Ziva', 'CSK']: \n", "('IPL', 'MI'): \n", "{'Best': 'CSK'}: \n", "{'RCB', 'MI', 'CSK'}: \n" ] } ] }, { "cell_type": "code", "source": [ "for idx, (val, dtype) in enumerate(zip(items, types)): # complex method enumerate + zip for accessing index as well\n", " print(f\"Index {idx}: {val} --> {dtype}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "PSb1ceCmETJC", "outputId": "11a089ac-5f58-49d3-e7cf-a8cf4a8f2a38" }, "execution_count": 11, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Index 0: 7 --> \n", "Index 1: Dhoni --> \n", "Index 2: 7.7 --> \n", "Index 3: True --> \n", "Index 4: None --> \n", "Index 5: (3+14j) --> \n", "Index 6: ['Ziva', 'CSK'] --> \n", "Index 7: ('IPL', 'MI') --> \n", "Index 8: {'Best': 'CSK'} --> \n", "Index 9: {'RCB', 'MI', 'CSK'} --> \n" ] } ] }, { "cell_type": "code", "source": [ "# common errors\n", "l1 = [1,2,3]\n", "l2 = [\"a\", \"b\", \"c\"]\n", "\n", "# using range\n", "for i in range(len(l1)): # in case of same length\n", " print(l1[i], l2[i])\n", "print()\n", "# using zip\n", "for i, j in zip(l1, l2):\n", " print(i, j)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1eN1PFvmETML", "outputId": "fcd8edee-10b1-4b26-f2a8-09d474c9ec78" }, "execution_count": 12, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1 a\n", "2 b\n", "3 c\n", "\n", "1 a\n", "2 b\n", "3 c\n" ] } ] }, { "cell_type": "code", "source": [ "# making the list 1 length to 4\n", "l1.append(7)\n", "for i in range(len(l1)):\n", " print(l1[i], l2[i])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 228 }, "id": "SFPx2t4rETPS", "outputId": "75995e22-78c2-4aab-f5f6-4fd96303b2fc" }, "execution_count": 13, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1 a\n", "2 b\n", "3 c\n" ] }, { "output_type": "error", "ename": "IndexError", "evalue": "list index out of range", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0ml1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ml2\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ] }, { "cell_type": "code", "source": [ "for i, j in zip(l1, l2): # ignores the extra element and no error\n", " print(i, j)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2vK8dkuMETSQ", "outputId": "04491c73-751d-422b-ff52-ab5adccc7b57" }, "execution_count": 14, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1 a\n", "2 b\n", "3 c\n" ] } ] }, { "cell_type": "code", "source": [ "# Incorrect Indexing\n", "l1 = [1,2,3]\n", "for i in l1:\n", " print(l1[i]) # Error if 'i' is not an integer index" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 211 }, "id": "Nf7whaRoETVI", "outputId": "145fae0c-ed83-4a99-ccc1-cea22057eed5" }, "execution_count": 15, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2\n", "3\n" ] }, { "output_type": "error", "ename": "IndexError", "evalue": "list index out of range", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0ml1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0ml1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Error if 'i' is not an integer index\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ] }, { "cell_type": "code", "source": [ "for i in range(len(l1)): # correct method using range\n", " print(l1[i])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xhTmepM-ETbI", "outputId": "8391b7ad-f205-4ff7-ae07-c0e0d02a865e" }, "execution_count": 16, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1\n", "2\n", "3\n" ] } ] }, { "cell_type": "code", "source": [ "# remove elements ERROR\n", "# List with no elements\n", "empty_list = []\n", "\n", "# Attempting to pop from an empty list\n", "item = empty_list.pop() # This will raise an IndexError\n", "print(item)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 193 }, "id": "YXAkYliYRGT0", "outputId": "aa08f88c-ea76-4395-fafb-8c65a6a64643" }, "execution_count": 17, "outputs": [ { "output_type": "error", "ename": "IndexError", "evalue": "pop from empty list", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# Attempting to pop from an empty list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mitem\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mempty_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# This will raise an IndexError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mIndexError\u001b[0m: pop from empty list" ] } ] }, { "cell_type": "code", "source": [ "# List with no elements\n", "empty_list = []\n", "\n", "# Check if the list is empty before popping\n", "if empty_list:\n", " item = empty_list.pop()\n", " print(\"Popped item:\", item)\n", "else:\n", " print(\"The list is empty, cannot pop.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "QsN9U3QlRGXH", "outputId": "8e96cd3f-8180-4cd8-b5aa-79723195fb6c" }, "execution_count": 18, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The list is empty, cannot pop.\n" ] } ] }, { "cell_type": "code", "source": [ "# List of fruits\n", "fruits = [\"apple\", \"banana\", \"orange\"]\n", "\n", "# Attempting to remove an element not in the list\n", "fruits.remove(\"grape\") # This will raise a ValueError because \"grape\" is not in the list\n", "print(fruits)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 193 }, "id": "QlxvNt96RGaz", "outputId": "6baafd69-69fb-41ee-f54b-6859883881d6" }, "execution_count": 19, "outputs": [ { "output_type": "error", "ename": "ValueError", "evalue": "list.remove(x): x not in list", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Attempting to remove an element not in the list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mfruits\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"grape\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# This will raise a ValueError because \"grape\" is not in the list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfruits\u001b[0m\u001b[0;34m)\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" ] } ] }, { "cell_type": "code", "source": [ "# List of fruits\n", "fruits = [\"apple\", \"banana\", \"orange\"]\n", "\n", "# Check if the item exists before removing\n", "rem = \"grape\"\n", "if rem in fruits:\n", " fruits.remove(rem)\n", " print(f\"{rem} removed from the list.\")\n", "else:\n", " print(f\"{rem} not found in the list.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "EyBHmmEeRGdS", "outputId": "00dbfea5-b115-4702-b28c-4331fa78f1e9" }, "execution_count": 20, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "grape not found in the list.\n" ] } ] }, { "cell_type": "code", "source": [ "# List of numbers\n", "numbers = [1, 2, 3, 4, 5]\n", "\n", "# Attempting to delete an invalid index\n", "del numbers[10] # This will raise an IndexError because the index 10 is out of range\n", "print(numbers)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 193 }, "id": "5OO0Oa4eRWwN", "outputId": "84b44df8-b657-4a12-ae35-1ab5ee723c02" }, "execution_count": 21, "outputs": [ { "output_type": "error", "ename": "IndexError", "evalue": "list assignment index out of range", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Attempting to delete an invalid index\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mnumbers\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# This will raise an IndexError because the index 10 is out of range\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumbers\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range" ] } ] }, { "cell_type": "code", "source": [ "numbers = [1, 2, 3, 4, 5]\n", "\n", "# Check if the index is valid before deleting\n", "idx = 10\n", "if 0 <= idx < len(numbers):\n", " del numbers[idx]\n", " print(\"Item deleted:\", numbers)\n", "else:\n", " print(f\"Index {idx} is out of range. Cannot delete.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "GoDkaGn2RW0G", "outputId": "2a0aa5aa-098e-4c36-f264-573daef9bdfd" }, "execution_count": 22, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Index 10 is out of range. Cannot delete.\n" ] } ] }, { "cell_type": "code", "source": [ "numbers = [10, 20, 30, 40, 50]\n", "\n", "# Check if the negative index is within the valid range\n", "idx = -7\n", "if -len(numbers) <= idx < 0:\n", " del numbers[idx]\n", " print(\"Item deleted:\", numbers)\n", "else:\n", " print(f\"Negative index {idx} is out of range. Cannot delete.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CiHr9DwkRhCV", "outputId": "b45f5d31-de27-4492-ba34-f0e595843e12" }, "execution_count": 23, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Negative index -7 is out of range. Cannot delete.\n" ] } ] }, { "cell_type": "code", "source": [ "# adding elements\n", "numbers = [1, 2, 3]\n", "\n", "# Attempting to append a non-iterable object (a string) as a single item\n", "numbers.append(\"4\", \"5\") # This will raise a TypeError because append() accepts only one argument\n", "print(numbers)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 193 }, "id": "yiMvMSumRhGe", "outputId": "b9877347-d904-4dff-9294-b26198c8968a" }, "execution_count": 24, "outputs": [ { "output_type": "error", "ename": "TypeError", "evalue": "list.append() takes exactly one argument (2 given)", "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[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Attempting to append a non-iterable object (a string) as a single item\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mnumbers\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"4\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"5\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# This will raise a TypeError because append() accepts only one argument\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumbers\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: list.append() takes exactly one argument (2 given)" ] } ] }, { "cell_type": "code", "source": [ "numbers = [1, 2, 3]\n", "\n", "# Correctly append a single element\n", "numbers.append(\"4\") # Output: [1, 2, 3, \"4\"]\n", "\n", "# To append multiple elements, use a list or another method like extend()\n", "numbers.append([\"5\", \"6\"]) # This will append the entire list as a sublist\n", "print(numbers) # Output: [1, 2, 3, \"4\", [\"5\", \"6\"]]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dRrDKuxRRyze", "outputId": "cde7519e-40ce-4edd-b1ff-05506d39fcc2" }, "execution_count": 25, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[1, 2, 3, '4', ['5', '6']]\n" ] } ] }, { "cell_type": "code", "source": [ "numbers = [10, 20, 30]\n", "\n", "# Attempting to insert an element at an invalid index (larger than the list length)\n", "numbers.insert(10, 40) # This will add the element at last\n", "print(numbers)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KXg6nUGHRy2d", "outputId": "6c7d58bc-161f-45d3-8105-d9ca7a3cfb46" }, "execution_count": 26, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[10, 20, 30, 40]\n" ] } ] }, { "cell_type": "code", "source": [ "numbers = [1, 2, 3]\n", "\n", "# Attempting to extend the list with a non-iterable object (an integer)\n", "numbers.extend(4) # This will raise a TypeError because extend() expects an iterable (e.g., list, tuple)\n", "print(numbers)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 193 }, "id": "C7IJ_GdXRy44", "outputId": "a0011bf9-57d0-409e-f6fc-5acc7644ffcb" }, "execution_count": 27, "outputs": [ { "output_type": "error", "ename": "TypeError", "evalue": "'int' object is not iterable", "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[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Attempting to extend the list with a non-iterable object (an integer)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mnumbers\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# This will raise a TypeError because extend() expects an iterable (e.g., list, tuple)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumbers\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: 'int' object is not iterable" ] } ] }, { "cell_type": "code", "source": [ "numbers = [1, 2, 3]\n", "\n", "# Correctly extend the list with an iterable (another list)\n", "numbers.extend([4, 5]) # Output: [1, 2, 3, 4, 5]\n", "\n", "# Attempting to extend with a non-iterable object (like a single integer)\n", "# This would raise a TypeError, so we make sure to provide an iterable:\n", "numbers.extend([6]) # Output: [1, 2, 3, 4, 5, 6]\n", "print(numbers)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jmC0l9b9SGZf", "outputId": "7b934184-9db7-49a6-d2ef-b45d8fd29f05" }, "execution_count": 28, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[1, 2, 3, 4, 5, 6]\n" ] } ] }, { "cell_type": "code", "source": [ "# Appending vs Extending\n", "l = [1, 2]\n", "print(\"Original List\",l)\n", "l.append([3, 4]) # Result: [1, 2, [3, 4]]\n", "print(\"After append\",l) # adds as sublist\n", "l.extend([5, 6]) # Result: [1, 2, [3, 4], 5, 6]\n", "print(\"After extend\",l) # unpacks the list and adds as individual elements" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "FcnuhDRWETfZ", "outputId": "1d78d95a-50bd-4d4b-bd36-4d6924a6c227" }, "execution_count": 29, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Original List [1, 2]\n", "After append [1, 2, [3, 4]]\n", "After extend [1, 2, [3, 4], 5, 6]\n" ] } ] }, { "cell_type": "code", "source": [ "# Confusing copy() with assignment\n", "a = [1, 2, 3]\n", "b = a # Same reference\n", "print(id(a), id(b))\n", "c = a.copy() # Independent copy\n", "print(id(a), id(c))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Och1wYRtET7Y", "outputId": "323ff4f6-ed8e-4fcf-d14c-16d7cf6558c6" }, "execution_count": 30, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "137634742193856 137634742193856\n", "137634742193856 137634742193728\n" ] } ] }, { "cell_type": "code", "source": [ "# List Slicing\n", "'''\n", "list[start:stop:step]\n", "\n", "start: The index to start the slice (inclusive).\n", "stop: The index to end the slice (exclusive).\n", "step: The step between indices.\n", "\n", "'''" ], "metadata": { "id": "y4HD7dRCEUh6" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "items = [10, 20, 30, 40, 50, 60, 70]\n", "\n", "# Slicing from index 1 to index 4 (exclusive)\n", "print(items[1:4]) # Output: [20, 30, 40]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1qN_117cEUBZ", "outputId": "8b4b8b52-468d-42c6-d297-6cd4c70c9a4c" }, "execution_count": 31, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[20, 30, 40]\n" ] } ] }, { "cell_type": "code", "source": [ "# Omitting start and stop (Whole list)\n", "# Slicing the entire list\n", "print(items[:]) # Output: [10, 20, 30, 40, 50, 60, 70]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_2Tv-7N9EUEw", "outputId": "fe06571f-082a-4f58-c74c-86dbe5f67bee" }, "execution_count": 32, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[10, 20, 30, 40, 50, 60, 70]\n" ] } ] }, { "cell_type": "code", "source": [ "# Omitting start (From beginning to index 4)\n", "# Start from the beginning and go till index 4 (exclusive)\n", "print(items[:4]) # Output: [10, 20, 30, 40]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YbLIYqPGEUIB", "outputId": "ad3d9eb7-06ab-405c-babc-814e478d8586" }, "execution_count": 33, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[10, 20, 30, 40]\n" ] } ] }, { "cell_type": "code", "source": [ "# Omitting stop (From index 2 to the end)\n", "# Start from index 2 till the end\n", "print(items[2:]) # Output: [30, 40, 50, 60, 70]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VAW3IqN9EUKw", "outputId": "679b0b52-4835-41e1-cff2-9bdeea286ade" }, "execution_count": 34, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[30, 40, 50, 60, 70]\n" ] } ] }, { "cell_type": "code", "source": [ "# Negative Indices (Counting from the end)\n", "# Using negative indices\n", "print(items[-3:]) # Output: [50, 60, 70] (Last 3 elements)\n", "print(items[:-3]) # Output: [10, 20, 30, 40] (All except last 3 elements)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jwCxB9pvEUPK", "outputId": "e88a99ca-67a5-4c44-b82f-4f7646477556" }, "execution_count": 35, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[50, 60, 70]\n", "[10, 20, 30, 40]\n" ] } ] }, { "cell_type": "code", "source": [ "# Using Step (Skipping Elements)\n", "# Step 2, starting from index 0\n", "print(items[::2]) # Output: [10, 30, 50, 70] (Every 2nd element)\n", "\n", "# Step -1 (Reversing the list)\n", "print(items[::-1]) # Output: [70, 60, 50, 40, 30, 20, 10] (Reversed list)\n", "print(items[len(items)-1::-1]) # for this the above line is the shortcut" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "K0bfhy-rEUew", "outputId": "72f5701c-0e69-4895-8fab-bd6fc079d83e" }, "execution_count": 36, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[10, 30, 50, 70]\n", "[70, 60, 50, 40, 30, 20, 10]\n", "[70, 60, 50, 40, 30, 20, 10]\n" ] } ] }, { "cell_type": "code", "source": [ "# Using Step with Start and Stop\n", "# Start from index 1 to 5 (exclusive) with step 2\n", "print(items[1:5:2]) # Output: [20, 40]\n", "\n", "# Start from index 0 to 6 (exclusive) with step 3\n", "print(items[0:6:3]) # Output: [10, 40]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jyQHdZWAEUlY", "outputId": "58b74f01-03e1-4652-f77e-d8379db973a7" }, "execution_count": 37, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[20, 40]\n", "[10, 40]\n" ] } ] }, { "cell_type": "code", "source": [ "# Using Step with Negative Indices\n", "# Start from the end of the list and skip 2 elements\n", "print(items[-1:-6:-2]) # Output: [70, 50, 30] (Start from 70 and step backwards)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2IIwM5rQEUoM", "outputId": "cf69a7fa-588f-492e-c640-679b30169ef7" }, "execution_count": 38, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[70, 50, 30]\n" ] } ] }, { "cell_type": "code", "source": [ "# Extracting Sublist Using Variables\n", "start, stop, step = 1, 5, 2\n", "print(items[start:stop:step]) # Output: [20, 40]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "b5wqFAneEUq3", "outputId": "578d9121-2ba4-43a4-ddbd-13fb7155d1be" }, "execution_count": 39, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[20, 40]\n" ] } ] }, { "cell_type": "code", "source": [ "# Slicing Nested Lists\n", "nested_items = [1, [2, 3, 4], 5, [6, 7, 8], 9]\n", "# Slice the sublist [2, 3, 4]\n", "print(nested_items[1][1:3]) # Output: [3, 4]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1WM53-r7NhEE", "outputId": "186e0051-84b5-4e58-c57b-d370b3d45aad" }, "execution_count": 40, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[3, 4]\n" ] } ] }, { "cell_type": "code", "source": [ "# List Concatenation and Repetition\n", "# Concatenation\n", "list1 = [1, 2]\n", "list2 = [3, 4]\n", "combined = list1 + list2 # Output: [1, 2, 3, 4]\n", "print(combined)\n", "\n", "# Repetition\n", "repeated = list1 * 3 # Output: [1, 2, 1, 2, 1, 2]\n", "print(repeated)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_cH6ryxrNhGm", "outputId": "9485ca95-48e0-4951-accf-2ccfebea8866" }, "execution_count": 41, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[1, 2, 3, 4]\n", "[1, 2, 1, 2, 1, 2]\n" ] } ] }, { "cell_type": "code", "source": [ "# Nested Lists & Multi-level Access\n", "nested = [1, [2, 3], [4, [5, 6]]]\n", "\n", "# Accessing the nested list\n", "print(nested[1][1]) # Output: 3\n", "print(nested[2][1][0]) # Output: 5" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "e2fBHHqINhJD", "outputId": "64cad94c-3379-4a9b-ec1d-b26c52fb9ae8" }, "execution_count": 42, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "3\n", "5\n" ] } ] }, { "cell_type": "code", "source": [ "# Using del for Deleting List Elements\n", "# Deleting a specific index\n", "l2 = [10, 20, 30, 40]\n", "del l2[2] # Removes 30, output: [10, 20, 40]\n", "print(l2)\n", "\n", "# Deleting the entire list\n", "del l2 # The list is now deleted.\n", "print(l2) # Error because it is no longer available" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 193 }, "id": "j1810sTUNhLh", "outputId": "9588088e-5d49-4b27-ecbc-911c7a334504" }, "execution_count": 43, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[10, 20, 40]\n" ] }, { "output_type": "error", "ename": "NameError", "evalue": "name 'l2' is not defined", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;31m# Deleting the entire list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0ml2\u001b[0m \u001b[0;31m# The list is now deleted.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml2\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Error because it is no longer available\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'l2' is not defined" ] } ] }, { "cell_type": "code", "source": [ "# Using in for Membership Test\n", "\n", "l3 = [10, 20, 30]\n", "print(20 in l3) # Output: True\n", "print(50 in l3) # Output: False" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VZ_ifyXxNhN8", "outputId": "e40f9eb0-f96b-4868-8bcb-006386534e68" }, "execution_count": 44, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "True\n", "False\n" ] } ] }, { "cell_type": "code", "source": [ "# Using list() to Convert Iterables to Lists\n", "# Convert string to list\n", "my_str = \"hello\"\n", "my_list = list(my_str) # Output: ['h', 'e', 'l', 'l', 'o']\n", "print(my_list)\n", "\n", "# Convert tuple to list\n", "my_tuple = (1, 2, 3)\n", "my_list = list(my_tuple) # Output: [1, 2, 3]\n", "print(my_list)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ogVtTDjTOnJW", "outputId": "8e513611-9a48-453c-925e-d72e5a3b5b19" }, "execution_count": 45, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['h', 'e', 'l', 'l', 'o']\n", "[1, 2, 3]\n" ] } ] }, { "cell_type": "code", "source": [ "# Sum of All Elements in a List\n", "# List of numbers\n", "numbers = [10, 20, 30, 40, 50]\n", "\n", "# Calculate the sum\n", "total = sum(numbers)\n", "\n", "print(\"Sum of all elements:\", total)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rKz_sz6XO7Xh", "outputId": "e11f7cd4-b939-4e1d-86d8-61cdae938e52" }, "execution_count": 46, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Sum of all elements: 150\n" ] } ] }, { "cell_type": "code", "source": [ "# elaborate method\n", "total = 0\n", "for i in numbers:\n", " total += i\n", "print(\"Sum of all elements:\", total)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "zispHJA8O7bY", "outputId": "7e826767-38dc-4125-993e-b9a295b0d0ad" }, "execution_count": 47, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Sum of all elements: 150\n" ] } ] }, { "cell_type": "code", "source": [ "# Find the Maximum and Minimum Elements in a List\n", "numbers = [10, 20, 30, 40, 50]\n", "\n", "# Find maximum and minimum\n", "max_num = max(numbers)\n", "min_num = min(numbers)\n", "\n", "print(\"Maximum element:\", max_num)\n", "print(\"Minimum element:\", min_num)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CHbC-GihO7fx", "outputId": "04934115-ca3b-4636-9ba4-8c24e4c9c116" }, "execution_count": 48, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Maximum element: 50\n", "Minimum element: 10\n" ] } ] }, { "cell_type": "code", "source": [ "# # elaborate method\n", "max_num = numbers[0]\n", "min_num = numbers[0]\n", "for i in numbers:\n", " if i > max_num:\n", " max_num = i\n", " if i < min_num:\n", " min_num = i\n", "print(\"Maximum element:\", max_num)\n", "print(\"Minimum element:\", min_num)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "FRm8PMhwPKoH", "outputId": "eab7fc9c-e162-4fa5-a028-b9a92dc401ab" }, "execution_count": 49, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Maximum element: 50\n", "Minimum element: 10\n" ] } ] }, { "cell_type": "code", "source": [ "# Segregate even and odd numbers from the list\n", "numbers = list(range(1,11))\n", "print(numbers)\n", "even = []\n", "odd = []\n", "for i in numbers:\n", " if i % 2 == 0:\n", " even.append(i)\n", " else:\n", " odd.append(i)\n", "print(\"Even numbers:\", even)\n", "print(\"Odd numbers:\", odd)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7540REDyPKrI", "outputId": "bf2c6506-ac52-42d3-ac36-6fd9784f73e6" }, "execution_count": 50, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "Even numbers: [2, 4, 6, 8, 10]\n", "Odd numbers: [1, 3, 5, 7, 9]\n" ] } ] } ] }