Created
September 6, 2024 19:36
-
-
Save tensorvijay/56bf0ec00198e8fbd477cb3c1ff2ce2f to your computer and use it in GitHub Desktop.
Internshala_explanation.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyMI7KSZYZk6n6AvgXGbVwX1", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/tensorvijay/56bf0ec00198e8fbd477cb3c1ff2ce2f/internshala_explanation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Student Query 1:" | |
| ], | |
| "metadata": { | |
| "id": "SBDaNGXmF-x0" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "azYxbHBuCSCg" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "L1=[1,2,3,4]\n", | |
| "L1.append(5)\n", | |
| "L2=L1\n", | |
| "L2.append(6)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print(f'L1={L1} and L2={L2} are the same because Lists are mutable')" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "3zv_2Re4CY0V", | |
| "outputId": "04ddbd30-723f-49da-b2f6-16e85c5ff2d8" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "[1, 2, 3, 4, 5, 6] and [1, 2, 3, 4, 5, 6] are the same\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [], | |
| "metadata": { | |
| "id": "-l2cAXuTGHW5" | |
| } | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [], | |
| "metadata": { | |
| "id": "z0E40wvFGIX-" | |
| } | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "This has something to do with the underlying object oriented architecture of python. L2=L1 creates a new object but references the original list object. Since Lists are mutable - meaning original elements can be modified after declaration - the original List is also modified. Below this can be further verified by creating a copy of L2 in L3." | |
| ], | |
| "metadata": { | |
| "id": "-4lPhN-eEnuK" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "L3=L2\n", | |
| "L2.append(7)\n", | |
| "print(f'L1={L1} and L2={L2} and L2={L3} are the same because Lists are mutable and all copies access amd modify the elements of the original list object.')" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "vgUTyJeyDoS3", | |
| "outputId": "18a49187-a819-4db8-b837-d42cbc803a5d" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "L1=[1, 2, 3, 4, 5, 6, 7] and L2=[1, 2, 3, 4, 5, 6, 7] and L2=[1, 2, 3, 4, 5, 6, 7] are the same because Lists are mutable and all copies access the elements of the original list object.\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "L1=(1,2,'C','D')\n", | |
| "L2=L1\n", | |
| "L2+=(5,)\n", | |
| "print(f'L1={L1} and L2={L2} are not the same because tuples are immutable')" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "U9SBtf7qF3Qc", | |
| "outputId": "bf3cda0c-7119-4eb7-d116-b7aaffc576a5" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "L1=(1, 2, 'C', 'D') and L2=(1, 2, 'C', 'D', 5) are not the same because tuples are immutable\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Immutable programming are necessary when safety is of utmost importance. You don't want critical information to be modified. This is also an important feature in concurrent programing (threads)." | |
| ], | |
| "metadata": { | |
| "id": "xE_DGwFPOFZa" | |
| } | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Why are return staments necessary within user defined functions. ?" | |
| ], | |
| "metadata": { | |
| "id": "ohyGC2FpOVkI" | |
| } | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Let us see one way of defining functions, one can consider the problem in the previous statement, Let us define the function for printing the function within a user defined function." | |
| ], | |
| "metadata": { | |
| "id": "LmovJHU0ObgA" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def prlt(L1,L2):\n", | |
| " print(f'L1={L1} and L2={L2} are the same because Lists are mutable.')" | |
| ], | |
| "metadata": { | |
| "id": "MPnVCxRaGbri" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "L1 = [10,11,12,13,15]\n", | |
| "L2=L1\n", | |
| "L2.append([110,111,112,113,115])\n", | |
| "\n" | |
| ], | |
| "metadata": { | |
| "id": "wxcvhqsxSTmW" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "We are passing the Lists that we created as a function we can resue it. This is the typical idea of a function." | |
| ], | |
| "metadata": { | |
| "id": "UDno1nA8SrED" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "prlt(L1,L2)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "rDWK1kF7Sn28", | |
| "outputId": "a563d342-e445-411b-846a-3d3a7f3ac3f6" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "L1=[10, 11, 12, 13, 15, [110, 111, 112, 113, 115]] and L2=[10, 11, 12, 13, 15, [110, 111, 112, 113, 115]] are the same because Lists are mutable.\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "L3=L2\n", | |
| "prlt(L1,L3)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "-xi1KSCmS6AE", | |
| "outputId": "fd7d3f27-d723-426b-e673-3c5fe17fbbf4" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "L1=[10, 11, 12, 13, 15, [110, 111, 112, 113, 115]] and L2=[10, 11, 12, 13, 15, [110, 111, 112, 113, 115]] are the same because Lists are mutable.\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "But what if we want to store this information in another variable and use it. The above operation is comfortable in Jupyter notebook for demonstration purposes. But think about real world operations. It is not difficult to see that in real world applications you need to store variables for further operations." | |
| ], | |
| "metadata": { | |
| "id": "umGyefmmTUMh" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "k=prlt(L1,L3)\n" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "40nMVO4jS9Lf", | |
| "outputId": "66d61c5c-ce1d-4f66-d1d8-713f95b37388" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "L1=[10, 11, 12, 13, 15, [110, 111, 112, 113, 115]] and L2=[10, 11, 12, 13, 15, [110, 111, 112, 113, 115]] are the same because Lists are mutable.\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print(k)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "eo5hDp7RVPLz", | |
| "outputId": "d5d2b07e-835d-4732-db3e-3eef8a2620af" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "None\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "In the abense of the return value, Python returns a none object. Let us redefine the function with a return value." | |
| ], | |
| "metadata": { | |
| "id": "VtEYzIwSVROq" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def prlt(L1,L2):\n", | |
| " return f'L1={L1} and L2={L2} are the same because Lists are mutable.'" | |
| ], | |
| "metadata": { | |
| "id": "z6xp_HglVP9V" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "k=prlt(L1,L3)" | |
| ], | |
| "metadata": { | |
| "id": "PfOA21DjVe6f" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print(k)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "l974kV-mVofz", | |
| "outputId": "8837f2dd-779e-44a5-b3d6-619190ae3cad" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "L1=[10, 11, 12, 13, 15, [110, 111, 112, 113, 115]] and L2=[10, 11, 12, 13, 15, [110, 111, 112, 113, 115]] are the same because Lists are mutable.\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print(k+'and not safe')" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "7Jz5j_w4VvG_", | |
| "outputId": "b105d7f9-934c-4b5b-d139-63a5d8489c76" | |
| }, | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "L1=[10, 11, 12, 13, 15, [110, 111, 112, 113, 115]] and L2=[10, 11, 12, 13, 15, [110, 111, 112, 113, 115]] are the same because Lists are mutable.and not safe\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Above operations are not possible without a return statement." | |
| ], | |
| "metadata": { | |
| "id": "eqLwo0tAV7tT" | |
| } | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Magic functions:" | |
| ], | |
| "metadata": { | |
| "id": "4QmxHrEWV_K2" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [], | |
| "metadata": { | |
| "id": "2rrJeGwkwiFE" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "In the Below code we are writing the Printing list as a part of a very simple class. We are using a constructor to initiate the process. Note that we will see without the magic function of a __str__ function in python." | |
| ], | |
| "metadata": { | |
| "id": "MgkOqtNSx1j_" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "class Prtln:\n", | |
| " def __init__(self,L1,L2):\n", | |
| " self.L1=L1\n", | |
| " self.L2=L2\n", | |
| " #def __str__(self):\n", | |
| " #return f'L1={self.L1} and L2={self.L2} are the same because Lists are mutable.'\n", | |
| "\n", | |
| "\n", | |
| "\n" | |
| ], | |
| "metadata": { | |
| "id": "5VtuLrIlV7Lo" | |
| }, | |
| "execution_count": 11, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "L1=[1,2,3,4]\n", | |
| "L3=L1\n", | |
| "L3.append(5)" | |
| ], | |
| "metadata": { | |
| "id": "8-_sjNmBxpj1" | |
| }, | |
| "execution_count": 12, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "llst=Prtln(L1,L3)" | |
| ], | |
| "metadata": { | |
| "id": "c_apY2MywpOy" | |
| }, | |
| "execution_count": 13, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print(llst)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "I_ruRN7bxv9z", | |
| "outputId": "28826957-9423-4979-9790-efe998d72f86" | |
| }, | |
| "execution_count": 14, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "<__main__.Prtln object at 0x7e4fedc75750>\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "We are receivng the memory location of the object of the class. Now let us try to define a function that would return the information about the lists." | |
| ], | |
| "metadata": { | |
| "id": "RBHkszSgWctR" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "class Prtln:\n", | |
| " def __init__(self,L1,L2):\n", | |
| " self.L1=L1\n", | |
| " self.L2=L2\n", | |
| " def printer(self):\n", | |
| " return f'L1={self.L1} and L2={self.L2} are the same because Lists are mutable.'" | |
| ], | |
| "metadata": { | |
| "id": "-B0Z4EnzyupE" | |
| }, | |
| "execution_count": 17, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "llst=Prtln(L1,L3)" | |
| ], | |
| "metadata": { | |
| "id": "uJp-ODHhy3v5" | |
| }, | |
| "execution_count": 18, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print(llst)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "HL0LYWS1zDDu", | |
| "outputId": "d1bf4047-b3a8-4f25-e765-2464dda77089" | |
| }, | |
| "execution_count": 19, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "<__main__.Prtln object at 0x7e4fedc76770>\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Even when we are trying to define a user defined function within the class we are only getting the address location of the object" | |
| ], | |
| "metadata": { | |
| "id": "JYJkvLSXzGgU" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "class Prtln:\n", | |
| " def __init__(self,L1,L2):\n", | |
| " self.L1=L1\n", | |
| " self.L2=L2\n", | |
| " def __str__(self):\n", | |
| " return f'L1={self.L1} and L2={self.L2} are the same because Lists are mutable.'" | |
| ], | |
| "metadata": { | |
| "id": "wMTImCGIzEpI" | |
| }, | |
| "execution_count": 21, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "llst=Prtln(L1,L3)\n", | |
| "print(llst)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "5GCPlcKwzv-M", | |
| "outputId": "e40244eb-78f6-4c11-c146-635e9c3a8120" | |
| }, | |
| "execution_count": 22, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "L1=[1, 2, 3, 4, 5] and L2=[1, 2, 3, 4, 5] are the same because Lists are mutable.\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "The magic method allows the objects to be returned as strings. The __str__ represetns class objects as a string." | |
| ], | |
| "metadata": { | |
| "id": "80oC-4Wfz1iI" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [], | |
| "metadata": { | |
| "id": "Lejm_7oH1YUt" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment