{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Loops\n", "\n", "[Source](https://github.com/iArunava/Python-TheNoTheoryGuide/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Simple while\n", "# Loop runs till the condition is True\n", "v1 = 5\n", "while v1 <= 10:\n", " print (v1)\n", " v1 += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Infinite Loops\n", "while 1:\n", " print (1)\n", " \n", "while True:\n", " print (1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# One Liner while\n", "v1 = 0\n", "while v1 <= 40: v1 += 1\n", "print (v1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v1 = 2\n", "++v1\n", "++v1\n", "print (v1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Terminate loop on a certain user input\n", "# Note: The loop will break only when the user inputs 100\n", "v1 = 1\n", "while v1 != 100:\n", " v1 = int(input(\"Enter new v1: \"))\n", " print (\"v1 modified to: \" + str(v1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 'break' -> breaks out of loop, doesn't execute any statement after it\n", "while 1:\n", " v1 = int(input())\n", " if v1 == 100: \n", " break;\n", " print (v1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 'continue' -> continues to next iteration, skips all statements after it for that iteration\n", "# Note: When 'v1' < 100 the last print statement is skipped and the control moves to the next iteration\n", "while 1:\n", " print (\"Iteration begins\")\n", " v1 = int(input())\n", " if v1 == 100:\n", " break;\n", " elif v1 < 100:\n", " print (\"v1 less than 100\")\n", " continue;\n", " print (\"Iteration complete\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# while with lists\n", "l1 = [\"Jennifer\", 12, \"Python\", 'A', 56, 'B', 2.12, \"Scarlett\"]\n", "l2 = []\n", "i = 0\n", "while i < len(l1):\n", " if type(l1[i]) == int:\n", " l2.append(l1[i])\n", " i += 1\n", "print (l2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Removing all instances of a specific value in list\n", "l1 = ['A', 'B', 'C', 'D', 'A', 'E', 'Q', 'A', 'Z', 'A', 'Q', 'D', 'A']\n", "while 'A' in l1: l1.remove('A')\n", "print (l1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Filing a dictionary\n", "d1 = {}\n", "while 1:\n", " key = input(\"Enter a key: \")\n", " value = input(\"Enter a value: \")\n", " d1[key] = value;\n", " if input(\"exit? \") == \"yes\": break;\n", "print (d1)" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }