{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Python Programming - Part - I\n", "Instructor: Dr. Sanasam Ranbir Singh <br>\n", "This tutorial is prepared by Mr. Soumyadeep Jana, OSINT Lab, Dept. of CSE, IIT Guwahati" ] }, { "cell_type": "markdown", "metadata": { "id": "xAb7vYClt6Mn" }, "source": [ "### Introduction\n", "1. Python is a widely-used general-purpose, high-level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation<br>\n", "2. Syntax is much simpler to other mid-level languages like C/C++\n", "3. It is an interpreted language\n", "4. Widely used today in fields like Machine Learning and AI due to its simplicity and powerful packages developed by the community " ] }, { "cell_type": "markdown", "metadata": { "id": "qmKXUew1PacT" }, "source": [ "### 1.Installing Python [Version- 3.10]\n", "Windows : [Install python and pip](https://www.tutorialspoint.com/how-to-install-python-in-windows)\n", "\n", "Linux :[Install Python](https://www.makeuseof.com/install-python-ubuntu/)\n", " [Install Pip](https://www.odoo.com/forum/help-1/how-to-install-pip-in-python-3-on-ubuntu-18-04-167715) <br><br>\n", "\n", "a) **pip** is a python package manager that lets you install powerful python packages developed by open-source developers.<br><br>\n", "b) For windows, pip is installed when you install python.<br>\n", "For linux users, you need to install it seperately<br><br>\n", "c) pip install 'package_name'<br><br>\n", "\n", "d) [More on pip ](https://www.w3schools.com/python/python_pip.asp)\n", "\n", "\n", "<br>Install a code-editor, **Sublime Text** or **VS-code** to write your python scripts" ] }, { "cell_type": "markdown", "metadata": { "id": "XWsdpHVpTLwd" }, "source": [ "###2.Print Statement\n", "Used to display output in the terminal" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HKet4NVaOxU3", "outputId": "6b4bd41f-35f9-40dc-cd03-64b31087505c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to Python Programming Lab\n", "India stands 4th at the Commonwealth Games 2022\n", "MTech Data Science\n" ] } ], "source": [ "print('Welcome to Python Programming Lab')\n", "print('India stands 4th at the Commonwealth Games 2022')\n", "\n", "#Multiple items with print\n", "print('MTech','Data','Science')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "tFPJgPPyURGk" }, "outputs": [], "source": [ "#WARNING[Python 2 and earlier]\n", "print 'Python Programming'" ] }, { "cell_type": "markdown", "metadata": { "id": "6mytTLkFccWY" }, "source": [ "### 3.Data Types\n", "" ] }, { "cell_type": "markdown", "metadata": { "id": "RscTxogSzCSS" }, "source": [ "**(3.1)** Numeric Types" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "e11okyeRyaca", "outputId": "8d830144-6f94-4a94-f7f4-93e804265106" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<class 'int'>\n", "<class 'float'>\n" ] } ], "source": [ "#Integer\n", "num1 = 100\n", "print(type(num1))\n", "\n", "#Float\n", "num2 = 100.1\n", "print(type(num2))" ] }, { "cell_type": "markdown", "metadata": { "id": "ky6OpOD6zzua" }, "source": [ "**Note : No Concept of short, long, double or other modifiers as in C/C++/Java**" ] }, { "cell_type": "markdown", "metadata": { "id": "e3ok_fr-zToI" }, "source": [ "**(3.2)** Boolean Types" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NYX6QCAJzRlo", "outputId": "6671f24e-dbcd-456f-f43c-eb17fd0fff07" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<class 'bool'>\n", "<class 'bool'>\n" ] } ], "source": [ "var1 = True\n", "print(type(var1))\n", "\n", "var2 = False\n", "print(type(var2))" ] }, { "cell_type": "markdown", "metadata": { "id": "sRPCHoplzqGk" }, "source": [ "**(3.3)** String type\n", "<br>1. Represents sequence of characters\n", "<br>2. Enclosed within quotes(either single or double)\n", "<br>3. If you want a string literal to contain either a single-quote or an apostrophe as part of the\n", "string, you can enclose the string literal in double-quote marks" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kaZM6bary1m1", "outputId": "516ae649-027a-47d6-df0f-58ff58e07387" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IITG ranks 7th in NIRF Engineering category\n", "IITG ranks 7th in NIRF Engineering category\n", "It's raining cats and dogs\n" ] } ], "source": [ "x = 'IITG ranks 7th in NIRF Engineering category'\n", "print(x)\n", "\n", "y = \"IITG ranks 7th in NIRF Engineering category\"\n", "print(y)\n", "\n", "z = \"It's raining cats and dogs\"\n", "print(z)" ] }, { "cell_type": "markdown", "metadata": { "id": "YvoS6UIMxcuS" }, "source": [ "**NOTE :Unlike C/C++/Java there's no char type here, even a single character is treated as a string** " ] }, { "cell_type": "markdown", "metadata": { "id": "YREA3FOFU0QH" }, "source": [ "### 4.Variables\n", "A variable is a name that represents a value stored in the computer’s\n", "memory.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "siHYx0_-Wj9-" }, "source": [ "**4.1)** You use an assignment statement to create a variable and make it reference a piece of data.\n", "An assignment statement is written in the following general format:\n", "**variable = expression**\n", "<br>Example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vNr-KE4zUooM", "outputId": "be94abcc-dfe0-4760-b353-68e8cfd64dcb" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.75\n", "99.95\n" ] } ], "source": [ "dollars = 2.75\n", "print(dollars)\n", "\n", "#Reassign variable\n", "dollars = 99.95\n", "print(dollars)" ] }, { "cell_type": "markdown", "metadata": { "id": "96oC8dT8dPsa" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "fDqYVO0RXyWk" }, "source": [ "**WARNING!** You cannot use a variable until you have assigned a value to it. An error\n", "will occur if you try to perform an operation on a variable, such as printing it, before\n", "it has been assigned a value." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 185 }, "id": "9z9WiWuJWy37", "outputId": "bd33f6ed-7079-4b5b-e87b-05bc57722879" }, "outputs": [ { "ename": "NameError", "evalue": "ignored", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-7-28ce6b64cc4d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mtemperature\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m40\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtemp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'temp' is not defined" ] } ], "source": [ "temperature = 40\n", "print(temp)" ] }, { "cell_type": "markdown", "metadata": { "id": "PqLKEeEcYqpF" }, "source": [ "**(4.2)** [Variable Naming Conventions](https://www.w3schools.com/python/gloss_python_variable_names.asp)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 132 }, "id": "6aXQfJNkXikj", "outputId": "890e2ea1-fdae-434c-9443-2fac568a7e1f" }, "outputs": [ { "ename": "SyntaxError", "evalue": "ignored", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"<ipython-input-8-844bd70ed252>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 1age = 3\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "1age = 3\n", "print(1age)" ] }, { "cell_type": "markdown", "metadata": { "id": "PsGPUrlfZIb5" }, "source": [ "**(4.3)** Python is a loosely typed language - You don't have to declare variables with data types unlike JAVA or C or C++ etc \n", "<br>***What a relief !!!***" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "EJUgZoPxpHlW" }, "outputs": [], "source": [ "int a = 10\n", "float b 100.1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RjtkP32LY4E1", "outputId": "21c6377e-7af5-48fb-e10c-40d74fad2599" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lakshya Sen 20 gold\n" ] } ], "source": [ "name = \"Lakshya Sen\"\n", "age = 20\n", "medal = \"gold\"\n", "print(name,age,medal)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CfhtAZ7fajmm", "outputId": "7bf05474-c33c-41a9-aa74-f1641ff6ccab" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IITG ranks 7th in NIRF Engineering category\n", "7\n" ] } ], "source": [ "#Benefit of loose type - single variable can hold multiple datatypes\n", "dollars = \"Virat Kohli\"\n", "print(x)\n", "x = 7\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": { "id": "v5HV5AaB24x7" }, "source": [ "###5.Reading Input\n", "1.Most of the programs that you will write will need to read input and then perform an operation on that input\n", "<br>2. **input()** function is used to do this\n", "<br>3. General Format : **var = input(\"Input Message\")**" ] }, { "cell_type": "markdown", "metadata": { "id": "pRGulo6N3ca_" }, "source": [ "**(5.1)** Reading string type data from keyboard" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "x58yTxDpa7Nn", "outputId": "71684c8f-2345-43d8-c2fc-239b52c043dd" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your namesoumyadeep\n", "soumyadeep\n" ] } ], "source": [ "x = input(\"Enter your name\")\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": { "id": "_WkXbryf45vr" }, "source": [ "**Note: input() function returns any input as string**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "yV25uRAP5DWA", "outputId": "1707eedb-c971-430c-a59a-dec625726d9f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your age25\n", "25\n", "<class 'str'>\n" ] } ], "source": [ "x = input(\"Enter your age\")\n", "print(x)\n", "print(type(x))" ] }, { "cell_type": "markdown", "metadata": { "id": "BtKrtfCN4mZh" }, "source": [ "**(5.2)** Reading numeric data types" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WO9Jcmdv4fV8", "outputId": "f72df10b-48d9-4f70-a0b4-215407d8a141" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your age25\n", "25\n", "<class 'int'>\n", "Enter your cgpa8.9\n", "8.9\n", "<class 'float'>\n" ] } ], "source": [ "#Read integer type data\n", "x = int(input(\"Enter your age\"))\n", "print(x)\n", "print(type(x))\n", "\n", "#Read float type data\n", "y = float(input('Enter your cgpa'))\n", "print(y)\n", "print(type(y))" ] }, { "cell_type": "markdown", "metadata": { "id": "FP0BVIP16JYY" }, "source": [ "###6.Output Formatting" ] }, { "cell_type": "markdown", "metadata": { "id": "0BcUT-A_9T-d" }, "source": [ "**(6.1)** Print variables with strings" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "caTb9Cxf5cEU", "outputId": "ee982e4c-da85-4384-c2d3-ca22e9b12557" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter wickets10\n", "Hopefully India doesn't lose by 10 wickets remaining this time against Pak\n", "Hopefully India doesn't lose by 10 wickets remaining this time against Pak\n" ] } ], "source": [ "wickets = int(input(\"Enter wickets\"))\n", "\n", "print(\"Hopefully India doesn't lose by {} wickets remaining this time against Pak\".format(wickets))\n", "\n", "print(f\"Hopefully India doesn't lose by {wickets} wickets remaining this time against Pak\")" ] }, { "cell_type": "markdown", "metadata": { "id": "d9lzuXqV9au6" }, "source": [ "**(6.2)** Suppressing the print Function’s Ending Newline\n", "\n", "<br>The print function normally displays a line of output. For example, the following three\n", "statements will produce three lines of output:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Bjvk8eB98hNp", "outputId": "69f7bf70-8193-4c80-b5c9-6da2ca133834" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "One\n", "Two\n", "Three\n" ] } ], "source": [ "print('One')\n", "print('Two')\n", "print('Three')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LjzDhSxz94fg", "outputId": "4e006612-2bc1-474c-b4f7-0bd9709a90ff" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "One Two Three\n" ] } ], "source": [ "print('One', end=\" \")\n", "print('Two', end=' ')\n", "print('Three')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "65qqm9vY-BX1", "outputId": "738f7803-cdd9-45d5-99e3-01861c4569e7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "One,Two,Three\n" ] } ], "source": [ "print('One', end=',')\n", "print('Two', end=',')\n", "print('Three')" ] }, { "cell_type": "markdown", "metadata": { "id": "w5a_yGsd-5L0" }, "source": [ "**(6.3)** Formatting Numbers\n", "<br>**format(variable,specifier)** function is used to do this" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "BZ6mrOAj-Xm7", "outputId": "7134843d-34d1-47b3-dda5-e72b2d5219f2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "416.6666666666667\n" ] } ], "source": [ "x = 5000/12\n", "print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9weEtN6DD8Hq", "outputId": "65491c2e-a207-4c7d-ccd6-4c595a0a1c40" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "416.67\n", "416.7\n" ] } ], "source": [ "x = 5000/12\n", "print(format(x,'.2f'))\n", "print(format(x,'.1f'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kBCzLA0gGarq", "outputId": "dc7190f6-8cea-49df-a7fe-3eef1b967c39" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "123,456\n" ] } ], "source": [ "print(format(123456, ',d'))" ] }, { "cell_type": "markdown", "metadata": { "id": "uwyMhyObGMv6" }, "source": [ "**Read more about formatting [here](https://www.w3schools.com/python/ref_string_format.asp)**" ] }, { "cell_type": "markdown", "metadata": { "id": "1OKVJ9JpmPi1" }, "source": [ "### 7.Conditional Statements" ] }, { "cell_type": "markdown", "metadata": { "id": "dPMmbE81nO9V" }, "source": [ "**(7.1)** If-Else Statement\n", "" ] }, { "cell_type": "markdown", "metadata": { "id": "ZC_Av_6joTDP" }, "source": [ "\n", "\n", "```\n", "if condition:\n", " statement\n", " statement\n", " etc.\n", "else:\n", " statement\n", " statement\n", " etc.\n", "```\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "B8zIh5bdEGZZ", "outputId": "125fdc3c-38ad-485d-e905-7b6941ae3b43" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A little cold,isn't it?\n", "Turn up the heat!!\n" ] } ], "source": [ "temperature = 36\n", "\n", "if temperature < 40:\n", " print(\"A little cold,isn't it?\")\n", " print(\"Turn up the heat!!\")\n", "else:\n", " print(\"Nice weather we're having\")\n", " print(\"Pass the sunscreen\")" ] }, { "cell_type": "markdown", "metadata": { "id": "blV91YQoo068" }, "source": [ "**Note :** Indentation is extremely important! Use **TAB** instead of **Space**" ] }, { "cell_type": "markdown", "metadata": { "id": "Aq5JGO8UtP-D" }, "source": [ "**INDENTATION HELL !!!**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "4gjJze5mroiM" }, "outputs": [], "source": [ "number = 5\n", "\n", "if number == 1:\n", " print('One')\n", "else:\n", " if number == 2:\n", " print('Two')\n", " else:\n", " if number == 3:\n", " print('Three')\n", " else:\n", " print('Unknown')" ] }, { "cell_type": "markdown", "metadata": { "id": "kIHh5clspNvF" }, "source": [ "**(7.2)** If-Elif-Else Statement\n", "<br>When multiple conditions need to be tested\n", "\n", "```\n", "if condition_1:\n", " statement\n", " statement\n", " etc.\n", "elif condition_2:\n", " statement\n", " statement\n", " etc.\n", "else:\n", " statement\n", " statement\n", " etc.\n", "```\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "bVNeCi07r8ac" }, "source": [ "**Need For If-Elif-Else**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "L-PxYZEdshaL" }, "outputs": [], "source": [ "number = 5\n", "if number == 1:\n", " print(\"one\")\n", "elif number == 2:\n", " print(\"two\")\n", "elif number == 3:\n", " print(\"three\")\n", "else:\n", " print(\"unknown\")" ] }, { "cell_type": "markdown", "metadata": { "id": "EvrQxyaZvLlo" }, "source": [ "**(7.3)** Relational Operators and Boolean Expressions" ] }, { "cell_type": "markdown", "metadata": { "id": "ce6uZZovv2dy" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "UoP8WdJryVTU" }, "source": [ "**Read about how strings are compared [here](https://www.geeksforgeeks.org/string-comparison-in-python/)**" ] }, { "cell_type": "markdown", "metadata": { "id": "VHOWFebK10Lk" }, "source": [ "**(7.4)** Logical Operators\n", "<br>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "mqkxf9o8s0jo", "outputId": "0d0d4578-4d17-4b47-f858-cd29ee23468a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "something\n" ] } ], "source": [ "a=34\n", "b=26\n", "if a>1 or b>1:\n", " print(\"something\")" ] }, { "cell_type": "markdown", "metadata": { "id": "hOp2LAcP7a7y" }, "source": [ "**Short-Circuit Evaluation of Logical Operators**<br>\n", "<br>1) In case of **and** operator, if the left boolean expression evaluates to **False**, then the entire compound boolean expression evaluated to **FALSE** without the right boolean expression being checked\n", "<br>2) In case of **or** operator, if the left boolean expression evaluates to **True**, then the entire compound boolean expression evaluated to **True** without the right boolean expression being checked" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "o7Bt2gE_8OyY", "outputId": "c9bd064b-bab9-4e51-c637-153a5244d07c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Not OK\n" ] } ], "source": [ "#Short-circuiting of and operator\n", "\n", "num = 10\n", "if num>10 and num%2 == 0:\n", " print(\"Ok\")\n", "else:\n", " print(\"Not OK\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3qtOK1D68nFW", "outputId": "e87de4a1-ecf9-4a9a-e304-8762366b080e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ok\n" ] } ], "source": [ "#Short-circuiting of or operator\n", "\n", "num = 10\n", "if num==10 or num%2 == 0:\n", " print(\"Ok\")\n", "else:\n", " print(\"Not OK\")" ] }, { "cell_type": "markdown", "metadata": { "id": "8h7rL9953tIJ" }, "source": [ "### 8.Let's recapitulate what we have learnt so far with this question\n", "***Determine whether a customer qualifies for a loan based on the following criteria***\n", "<br>***(a)Annual salary is greater than 300000***\n", "<br>***(b)Minimum work Experience is greater than 3 years***\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "bixZU_co64ZV" }, "source": [ "### 9.Some More Operators with their precedence and associativity\n", "Read [here](https://www.programiz.com/python-programming/precedence-associativity)" ] }, { "cell_type": "markdown", "metadata": { "id": "XJTH8HVo82hN" }, "source": [ "### 10.Repetition Structures - Loops" ] }, { "cell_type": "markdown", "metadata": { "id": "dCczl4GbItZ8" }, "source": [ "**(10.1)Motivation**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "HI0FiFyK5ntZ" }, "outputs": [], "source": [ "a = 10\n", "print(a)\n", "print(a+1)\n", "print(a+2)\n", "print(a+3)\n", "print(a+4)\n", "#.\n", "#.\n", "#.\n", "print(a+100)" ] }, { "cell_type": "markdown", "metadata": { "id": "SePEdbTKJ-Nw" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "Xy2xSx9GKxLO" }, "source": [ "**We can do better!!**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZDDhgISbKoTs", "outputId": "9e111d2f-4783-4ae2-cd7d-2a2d8ff6a247" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "11\n", "12\n", "13\n", "14\n", "15\n", "16\n", "17\n", "18\n", "19\n", "20\n", "21\n", "22\n", "23\n", "24\n", "25\n", "26\n", "27\n", "28\n", "29\n", "30\n", "31\n", "32\n", "33\n", "34\n", "35\n", "36\n", "37\n", "38\n", "39\n", "40\n", "41\n", "42\n", "43\n", "44\n", "45\n", "46\n", "47\n", "48\n", "49\n", "50\n", "51\n", "52\n", "53\n", "54\n", "55\n", "56\n", "57\n", "58\n", "59\n", "60\n", "61\n", "62\n", "63\n", "64\n", "65\n", "66\n", "67\n", "68\n", "69\n", "70\n", "71\n", "72\n", "73\n", "74\n", "75\n", "76\n", "77\n", "78\n", "79\n", "80\n", "81\n", "82\n", "83\n", "84\n", "85\n", "86\n", "87\n", "88\n", "89\n", "90\n", "91\n", "92\n", "93\n", "94\n", "95\n", "96\n", "97\n", "98\n", "99\n", "100\n", "101\n", "102\n", "103\n", "104\n", "105\n", "106\n", "107\n", "108\n", "109\n" ] } ], "source": [ "a = 10\n", "for i in range(100):\n", " print(a+i)" ] }, { "cell_type": "markdown", "metadata": { "id": "d5cXUN-iL9Gx" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "qinAB-tDPMel" }, "source": [ "**Notion of Repetition Structures**\n", "<br>\n" ] }, { "cell_type": "markdown", "metadata": { "id": "OUBBDj9-MCSo" }, "source": [ "**(10.2) While Loop**\n", "<br>1.Condition Controlled Loop\n", "<br>2.Executes the statements inside until the given condition is TRUE\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aHYZ1S2NK65p", "outputId": "f6ff61b7-cee4-4046-837b-47cc17370a3f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lives Remaining 10\n", "Lives Remaining 9\n", "Lives Remaining 8\n", "Lives Remaining 7\n", "Lives Remaining 6\n", "Lives Remaining 5\n", "Lives Remaining 4\n", "Lives Remaining 3\n", "Lives Remaining 2\n", "Lives Remaining 1\n" ] } ], "source": [ "#Initialize a starting condition\n", "life = 10\n", "\n", "#Declare the loop\n", "while life > 0:\n", " print(f'Lives Remaining {life}')\n", " life = life - 1" ] }, { "cell_type": "markdown", "metadata": { "id": "PtNzPBxZNy4D" }, "source": [ "**BEWARE OF INFINITE LOOPS**\n", "<br>\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "XLxRxiWYNUOC" }, "outputs": [], "source": [ "#Initialize a starting condition\n", "life = 10\n", "\n", "#Declare the loop\n", "while life > 0:\n", " print(f'Lives Remaining {life}')\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "UcHL0328OWFb" }, "outputs": [], "source": [ "#Another way to represent infinite loop\n", "\n", "while True:\n", " print('DEAD!!')" ] }, { "cell_type": "markdown", "metadata": { "id": "vOcfmQ2IPco_" }, "source": [ "**(10.3) For Loop**\n", "<br>1.Count Controlled Loop\n", "<br>2.Used when the no of times the loop needs to execute is known beforehand\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "fDKVK-eJPglX", "outputId": "ed795891-8e93-492e-a9c3-8b188d4fc735" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n" ] } ], "source": [ "#Execute loop for 5 times\n", "for num in [1,2,3,4,5]:\n", " print(num)" ] }, { "cell_type": "markdown", "metadata": { "id": "CtiYjqBpQIde" }, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wylEWk-CP37O", "outputId": "1df9712b-699c-472e-8e5d-605e633c581e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "9\n", "8\n", "7\n", "6\n", "5\n", "4\n", "3\n", "2\n", "1\n" ] } ], "source": [ "#Use Range function instead of manually typing out the numbers\n", "for num in range(10,0,-1):\n", " print(num)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "jZpHfo0PQx4R" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "collapsed_sections": [], "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 1 }