{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Decisions, loops\n\nDecision making\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "var = 100\nif var == 100:\n print(\"value of expression is 100\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While loop\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "count = 0\nwhile count < 9:\n print(\"The count is: \", count)\n count = count + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While loop with else statement\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "count = 0\nwhile count < 5:\n print(count, \" is less than 5\")\n count = count + 1\nelse:\n print(count, \" is not less than 5\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For loop\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for letter in \"Python\": # First Example\n print(\"Current Letter :\", letter)\n\nfruits = [\"banana\", \"apple\", \"mango\"]\nfor fruit in fruits: # Second Example\n print(\"Current fruit :\", fruit)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iterating by sequence index\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fruits = [\"banana\", \"apple\", \"mango\"]\nfor index in range(len(fruits)):\n print(\"Current fruit :\", fruits[index])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For loop with else\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for num in range(10, 20): # to iterate between 10 to 20\n for idx in range(2, num): # to iterate on the factors of the number\n if num % idx == 0: # to determine the first factor\n jdx = num / idx # to calculate the second factor\n print(\"%d equals %d * %d\" % (num, idx, jdx))\n break # to move to the next number, the #first FOR\n else: # else part of the loop\n print(num, \"is a prime number\")\n break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nested loops\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "val = 2\nwhile val < 100:\n val2 = 2\n while val2 <= (val / val2):\n if not (val % val2):\n break\n val2 = val2 + 1\n if val2 > val / val2:\n print(val, \" is prime\")\n val = val + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Break statement\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for letter in \"Python\": # First Example\n if letter == \"h\":\n break\n print(\"Current Letter :\", letter)\n\nvar = 10 # Second Example\nwhile var > 0:\n print(\"Current variable value :\", var)\n var = var - 1\n if var == 5:\n break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Continue statement\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for letter in \"Python\": # First Example\n if letter == \"h\":\n continue\n print(\"Current Letter :\", letter)\n\nvar = 10 # Second Example\nwhile var > 0:\n var = var - 1\n if var == 5:\n continue\n print(\"Current variable value :\", var)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pass statement\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for letter in \"Python\":\n if letter == \"h\":\n pass\n print(\"This is pass block\")\n print(\"Current Letter :\", letter)" ] } ], "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.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }