diff --git a/class2(1).ipynb b/class2(1).ipynb new file mode 100644 index 0000000..3caa706 --- /dev/null +++ b/class2(1).ipynb @@ -0,0 +1,1505 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5rc1" + }, + "colab": { + "name": "class2.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "i6g8mJsXXT-w", + "colab_type": "text" + }, + "source": [ + "In **Python**, string refers to a collection of characters.\n", + "\n", + "If you are familiar with **C or C++**, then you would know that to declare a string we need to first create an array of '***char***'.\n", + "\n", + "But in Python, anything enclosed inside, '' (single-quotes) or \"\"(double-quotes) is considerd as a string. For example:--" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kaxodY4uW9_E", + "colab_type": "code", + "outputId": "0d5e499b-d80f-497f-8973-b306268fe1b6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "\"It's a string\"" + ], + "execution_count": 16, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "\"It's a string\"" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 16 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fjC1bWn3ZIKC", + "colab_type": "text" + }, + "source": [ + "Even we can nest them, just like in this example--" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ktfyL581W9_r", + "colab_type": "code", + "outputId": "a6ad46f0-98d3-4036-e005-5f3f561593a0", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "'It \"wow\" a string'" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'It \"wow\" a string'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 17 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SxxKzF1qZXQE", + "colab_type": "text" + }, + "source": [ + "We can concatenate two strings in several ways. \n", + "\n", + "One common way is using ** + ** operator" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "WCycdxq_ZiyK", + "colab_type": "code", + "outputId": "137c5052-2933-446a-eaad-f8b678f97c1b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "s = 'hello this is long string ' + 'python wow this is string'\n", + "print(s)" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "hello this is long string python wow this is string\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5rPtNutKZsYB", + "colab_type": "text" + }, + "source": [ + "Also it can be done in the following way....\n", + "\n", + "(But I do not encuorage this one)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "eyKaUqvxW-AI", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "05f75e06-7623-4c1f-da2a-b143b9b646b0" + }, + "source": [ + "t = 'hello this is long string ''python wow this is string'\n", + "print(t)" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "text": [ + "hello this is long string python wow this is string\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "un3ncLqCaMFk", + "colab_type": "text" + }, + "source": [ + "We can think of a string as a **List** (We will discuss it later), which seems quite like arrays in C, C++, but actually not.\n", + "\n", + "Internally they are very different in definition. But let's not get into all those technicalities for now.\n", + "\n", + "We can access the elementary characters in the string using index..\n", + "\n", + "For example------" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "RyZSQK9ZW-BI", + "colab_type": "code", + "outputId": "88b46596-8f17-428b-990a-74c7fa6eea24", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "'hello'[0]" + ], + "execution_count": 20, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'h'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 20 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b_A5SGmfa07f", + "colab_type": "text" + }, + "source": [ + "We can do slicing of strings based on the indices..\n", + "\n", + "The way it is written is as ---\n", + "\n", + "list_[starting_position : ending_position + 1]\n", + "\n", + "That is, the last index mentioned is not included..as we can see from the example...\n", + "\n", + "One more point... indexing in Python starts from 0 just like in C, C++, Java etc." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Jby5Bk3wW-Bp", + "colab_type": "code", + "outputId": "c23adeab-5c2d-4bed-ae4c-ae2ac3e4a7b0", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "'hello'[0:3]" + ], + "execution_count": 21, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'hel'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 21 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "G_dCa5SEcNDL", + "colab_type": "text" + }, + "source": [ + "len() is a very important function, which returns the number of items present in the object. \n", + "\n", + "In case of string, the function returns the length of the string." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KcCdT-hXW-CI", + "colab_type": "code", + "outputId": "2451a1ad-6fc5-443c-c711-5714b98ede36", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "'hello'[0:2] + 'hello'[2:len('hello')]" + ], + "execution_count": 22, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'hello'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 22 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oZ5yOWG-W-Cc", + "colab_type": "code", + "outputId": "c469b6b8-c673-447d-983f-c86998cc6f0e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "len('hello')" + ], + "execution_count": 23, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "5" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 23 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WtrYMbdqrK1Y", + "colab_type": "text" + }, + "source": [ + "**Strings** are **immutable** in nature, i.e. in simple terms, we cannot change the contents of a string.\n", + "\n", + "This is a difference from **Lists** which are mutable in nature." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Q3hLl5qQW-Cx", + "colab_type": "code", + "outputId": "573a643b-ef88-4883-b3a0-c404acc99b6b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 172 + } + }, + "source": [ + "'jello'[0] = 'h'" + ], + "execution_count": 24, + "outputs": [ + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "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[0;32m----> 1\u001b[0;31m \u001b[0;34m'jello'\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'h'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ER-MJEvhuB0B", + "colab_type": "text" + }, + "source": [ + "Python **id()** function returns the “identity” of the object. \n", + "\n", + "The identity of an object is an integer, which is guaranteed to be unique and constant for this object during its lifetime.\n", + "\n", + "For example---" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "x_FT8DB4W-DE", + "colab_type": "code", + "colab": {} + }, + "source": [ + "x = 'hello'" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "EdKybBFUW-Da", + "colab_type": "code", + "outputId": "48076976-603f-4010-ca86-d304f5601490", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "print(x , id(x))" + ], + "execution_count": 26, + "outputs": [ + { + "output_type": "stream", + "text": [ + "hello 140074449895128\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "AzGO1ndVW-Dv", + "colab_type": "code", + "colab": {} + }, + "source": [ + "x = 'hello' + 'python'" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "XNK22FFXW-EH", + "colab_type": "code", + "outputId": "d1813c37-acc2-4379-efca-b7f5448a3857", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "print(x, id(x))" + ], + "execution_count": 28, + "outputs": [ + { + "output_type": "stream", + "text": [ + "hellopython 140074449906672\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "igRlQuJYW-EZ", + "colab_type": "code", + "outputId": "4f3e81db-90ec-404e-ae9b-82ab803633d4", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "x = 5\n", + "print(id(5))" + ], + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10968928\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "fBNnrfCQW-Er", + "colab_type": "code", + "colab": {} + }, + "source": [ + "x = x + 2" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "YV8csH5UW-E5", + "colab_type": "code", + "outputId": "116f21bf-db88-4e83-8328-6ec6f6ffca53", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "print(x, id(x))" + ], + "execution_count": 31, + "outputs": [ + { + "output_type": "stream", + "text": [ + "7 10968992\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "m6K-xUThugJw", + "colab_type": "text" + }, + "source": [ + "Two objects with non-overlapping lifetimes may have the same id() value.\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "IwLLPeijW-FI", + "colab_type": "code", + "colab": {} + }, + "source": [ + "x = 5" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "W73YvsjUW-FY", + "colab_type": "code", + "colab": {} + }, + "source": [ + "y = 5" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "WqmDxZhQW-Fn", + "colab_type": "code", + "colab": {} + }, + "source": [ + "z = 5" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "oQjbbAoWW-F8", + "colab_type": "code", + "outputId": "a17fdbc5-6f6d-4092-d9a9-ce9b54a1dfcc", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "print(id(x) , id(y) , id(z))" + ], + "execution_count": 35, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10968928 10968928 10968928\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wgpGacC8u3aj", + "colab_type": "text" + }, + "source": [ + "From the output, it’s clear that Python cache the integer objects and use them to save memory space. \n", + "\n", + "But it is only true for integer values >=5 and <=256.\n", + "\n", + "This is because for most inerpreters, the value <=256 are pre-loaded in memory for optimizations.\n", + "\n", + "Also some small strings are also preloaded in memory.\n", + "\n", + "But in general, immutable objects are always given distinct ids, i.e. memory location when initialized." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "UWvMLbO4W-GJ", + "colab_type": "code", + "outputId": "39225277-9c11-473b-a215-102d141d6dc1", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "x == y" + ], + "execution_count": 36, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 36 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "s5XPgQPvvFKv", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "1e5e6685-b0f6-4eef-fd6c-d709db8b3d28" + }, + "source": [ + "x is y" + ], + "execution_count": 37, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 37 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cReXpqhUW-Ga", + "colab_type": "code", + "colab": {} + }, + "source": [ + "x = 123456789" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "fyYafxlBW-Gp", + "colab_type": "code", + "colab": {} + }, + "source": [ + "y = 123456789" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "s-o9vQ05W-Gz", + "colab_type": "code", + "outputId": "447ba78c-d3fd-482b-b51f-d31fc3601069", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "print(id(x) , id(y))" + ], + "execution_count": 40, + "outputs": [ + { + "output_type": "stream", + "text": [ + "140074450166544 140074450167152\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KvlFcN0-W-HE", + "colab_type": "code", + "outputId": "13895411-f831-42b7-8b2d-e2b46f6a931f", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "x == y" + ], + "execution_count": 41, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 41 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SJ17EgZ8W-HP", + "colab_type": "code", + "outputId": "52032509-e11f-46f8-c45f-53b8ae8a3311", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "x is y" + ], + "execution_count": 42, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 42 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "q9xhBmh1W-He", + "colab_type": "code", + "colab": {} + }, + "source": [ + "x = 5" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "kIAXjvhxW-Ho", + "colab_type": "code", + "colab": {} + }, + "source": [ + "y = x" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "XdBDU3NFW-Hy", + "colab_type": "code", + "outputId": "a808ba0f-5420-477a-9688-849fee8369d3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "y == x" + ], + "execution_count": 45, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 45 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yeWmoGieW-IC", + "colab_type": "code", + "outputId": "a48d5943-a536-42c9-f860-67f0e3ed10ff", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "y is x" + ], + "execution_count": 46, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 46 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eqn6O4hOxKMN", + "colab_type": "text" + }, + "source": [ + "Goto [this](https://stackoverflow.com/questions/38189660/two-variables-in-python-have-same-id-but-not-lists-or-tuples) link for a detailed overview and understanding of this." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HkZB-UcHxihJ", + "colab_type": "text" + }, + "source": [ + "#If-else conditionals\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5AzIaGglW-IN", + "colab_type": "code", + "outputId": "adffeb55-2094-4afb-b2aa-cb832d6f9b9f", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "if 10>4:\n", + " print('yes, it is.')" + ], + "execution_count": 47, + "outputs": [ + { + "output_type": "stream", + "text": [ + "yes, it is.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "fVOJyXyZW-Iq", + "colab_type": "code", + "outputId": "128e9e36-5f9e-46b7-a843-9844e76a149a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "if 0.0:\n", + " print('if block')\n", + " if 5<10:\n", + " print('second if block')\n", + "elif '':\n", + " print('child if of 1St if')\n", + "elif False:\n", + " print('second elif')\n", + "elif 'wow':\n", + " print('third elif')\n", + "else:\n", + " print('else block')" + ], + "execution_count": 48, + "outputs": [ + { + "output_type": "stream", + "text": [ + "third elif\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uPH9PmQKx1h3", + "colab_type": "text" + }, + "source": [ + "If we have smaller number of cases, we can inline the if-else into 1 line, as we see in the next example..." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Lus_ONSQW-I0", + "colab_type": "code", + "colab": {} + }, + "source": [ + "x = 5 if 20<10 else 'hello'" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "vS135NFsW-I-", + "colab_type": "code", + "outputId": "dc35483e-7e7e-4505-f3c0-74c5e6bb8531", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "print(x)" + ], + "execution_count": 50, + "outputs": [ + { + "output_type": "stream", + "text": [ + "hello\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PeOq1LtvyPAV", + "colab_type": "text" + }, + "source": [ + "% operator when applied to two integers, yields the remainder.\n", + "\n", + "One example will make it clear---" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pL5H2By8W-JJ", + "colab_type": "code", + "outputId": "237a2c6d-7aaa-4683-bac1-358417bce7a8", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "print(10%2)\n", + "\n", + "print(5%3)" + ], + "execution_count": 51, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0\n", + "2\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2gHoMwQ5yrFG", + "colab_type": "text" + }, + "source": [ + "#Brief intro to looping" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9DfQ-bNrW-JT", + "colab_type": "code", + "colab": {} + }, + "source": [ + "y = 10" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6v2Kxxycy3pv", + "colab_type": "text" + }, + "source": [ + "Looping is eseential when we have a several piece of code that needs to be executed repeatedly.\n", + "\n", + "\n", + "The syntax of 'while'loop is--\n", + "\n", + "while condition:\n", + " Body\n", + " Update\n", + "\n", + "The code inside the body part is executed till the condition is evaluated to True." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9Kw6MzMUW-Jc", + "colab_type": "code", + "outputId": "e0ba4c52-638e-48bf-97b3-874ead854053", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 219 + } + }, + "source": [ + "while y < 20:\n", + " print(y)\n", + " y = y+1\n", + "print('outside of loop')" + ], + "execution_count": 53, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n", + "11\n", + "12\n", + "13\n", + "14\n", + "15\n", + "16\n", + "17\n", + "18\n", + "19\n", + "outside of loop\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6HQxH-Dt5NIl", + "colab_type": "text" + }, + "source": [ + "We can use else statement as well. with while loop.\n", + "\n", + "This helps us o execute a piece of code if our loop fails." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xnVGnsgmW-Jm", + "colab_type": "code", + "outputId": "0560cf25-b7af-48b9-86d6-dc4336ac15cd", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + } + }, + "source": [ + "while False:\n", + " print('while is true')\n", + " break\n", + "else:\n", + " print('else of while')\n", + "print('this is outside loop')" + ], + "execution_count": 54, + "outputs": [ + { + "output_type": "stream", + "text": [ + "else of while\n", + "this is outside loop\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zVhOLEVU5cIT", + "colab_type": "text" + }, + "source": [ + "#for-loop\n", + "\n", + "This type of loop ierates over a piece of object like a string or list or iterate over a range of values.\n", + "\n", + "The first example shows how we can iterate over a string." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "8fp4YlpuW-Jx", + "colab_type": "code", + "outputId": "e5826b4c-f965-451b-9109-6ad28b499b09", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + } + }, + "source": [ + "for i in 'hello':\n", + " print(i)" + ], + "execution_count": 55, + "outputs": [ + { + "output_type": "stream", + "text": [ + "h\n", + "e\n", + "l\n", + "l\n", + "o\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-MUrqybc9E8j", + "colab_type": "text" + }, + "source": [ + "This small example helps us to iterate over a range of values..\n", + "\n", + "As the only value mentioned is 5, so the loop will iterate from 0 to 4i.e. from 0 to 5-1." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1jCmdHa8W-J9", + "colab_type": "code", + "outputId": "a165831d-b050-482b-fde3-f76ec10efad0", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + } + }, + "source": [ + "for i in range(5):\n", + " print(i)" + ], + "execution_count": 56, + "outputs": [ + { + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BuXehKqh9Vvf", + "colab_type": "text" + }, + "source": [ + "We can also specify the starting and ending point like in this case.\n", + "\n", + "But remember, the loop terminates on second value - 1." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A2kA8dprW-KZ", + "colab_type": "code", + "outputId": "7e628c64-e7d1-4895-a82a-e4d74b45dbd2", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + } + }, + "source": [ + "for i in range(5 , 10):\n", + " print(i)" + ], + "execution_count": 57, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n", + "6\n", + "7\n", + "8\n", + "9\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jELd171t8LKo", + "colab_type": "text" + }, + "source": [ + "the third parameter denotes the step, that is how much the counter or the iterator should update." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "QNE5-YfYW-Km", + "colab_type": "code", + "outputId": "ed3f6af9-5158-42ec-e2d8-301ca88a3d59", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 90 + } + }, + "source": [ + "for i in range(2, 10, 2):\n", + " print(i)" + ], + "execution_count": 58, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n", + "4\n", + "6\n", + "8\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zRh_5_Vc-Em4", + "colab_type": "text" + }, + "source": [ + "We can take input in runtime using the input() method of Python.\n", + "\n", + "And also, we can print a prompt to the user using the input() method only, as shown below.." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "U_C_aQwfW-Ky", + "colab_type": "code", + "outputId": "229d3c63-596c-4e71-a9e3-7685bb3de287", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "x = input('Enter a number')" + ], + "execution_count": 59, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a number5\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Hc8b8fu3W-K-", + "colab_type": "code", + "outputId": "7a9c7d4c-db20-4eb5-ecb7-44e8f6120ff6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "print(x)" + ], + "execution_count": 60, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yIcNx5doW-LQ", + "colab_type": "code", + "outputId": "65c5ff79-2f73-4270-88d8-d815ecea99d2", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "source": [ + "print(type(x))" + ], + "execution_count": 61, + "outputs": [ + { + "output_type": "stream", + "text": [ + "\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vxvhXXU0-8lN", + "colab_type": "text" + }, + "source": [ + "type() function of Python returns the data type of the variable. \n", + "As we can see, the return type of input() is **string** .\n", + "\n", + "So, we need to typecast the value taken input into our desired data-type explicitly as shown below." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bzn_RJpm_2tT", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 90 + }, + "outputId": "64e2bc52-76cc-4cb9-b70f-d23983bd1a0d" + }, + "source": [ + "x = int(input('Enter a integer'))\n", + "print(type(x))\n", + "\n", + "\n", + "y = float(input(\"Enter a floating pint number\"))\n", + "print(type(y))" + ], + "execution_count": 63, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a integer9\n", + "\n", + "Enter a floating pint number5.2\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "vU9mZ_L9W-Ln", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file