{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# loops and conditional statement in Python\n",
    "\n",
    "Trainer: \n",
    "    \n",
    "Md. Jalal Uddin, Founder and director of Research Society https://researchsociety20.org/founder-and-director/\n",
    "    \n",
    "email: dmjalal90@gmail.com, 20205103002@nuist.edu.cn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \"for\" loop with range() function\n",
    "\n",
    "starting from 0 (by default) \n",
    "\n",
    "increments by 1 (by default)\n",
    "\n",
    "ends at a specified number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "for i in range(20):              # start = 0, end = n-1\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "for j in range(1,20):\n",
    "    print(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n",
      "11\n",
      "13\n",
      "15\n",
      "17\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "for k in range(1,20,2):\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \"for\" loop with list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jalal\n",
      "Ripa\n",
      "2\n",
      "4\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "list_item  = ['jalal','Ripa', 2, 4, 6]\n",
    "\n",
    "for i in range(len(list_item)):\n",
    "    print(list_item[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jalal\n",
      "Ripa\n",
      "2\n",
      "4\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "list_item  = ['jalal','Ripa', 2, 4, 6]\n",
    "\n",
    "for i in list_item:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# looping through a string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "J\n",
      "a\n",
      "l\n",
      "a\n",
      "l\n"
     ]
    }
   ],
   "source": [
    "for i in \"Jalal\":              \n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \"for\" loop with dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Jalal', 'wife': 'Ripa Jalal', 'kids': 'Jara Jalal', 'year of birth': 1990, 'good man': True, 'favourite colors': ['white', 'green', 'red']}\n"
     ]
    }
   ],
   "source": [
    "mydict = {\n",
    "  \"name\": \"Jalal\",\n",
    "  \"wife\": \"Ripa Jalal\",\n",
    "  \"kids\":\"Jara Jalal\",\n",
    "  \"year of birth\": 1990,\n",
    "  \"good man\":True,\n",
    "  \"favourite colors\": [\"white\", \"green\", \"red\"]  \n",
    "}\n",
    "print(mydict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name : Jalal\n",
      "wife : Ripa Jalal\n",
      "kids : Jara Jalal\n",
      "year of birth : 1990\n",
      "good man : True\n",
      "favourite colors : ['white', 'green', 'red']\n"
     ]
    }
   ],
   "source": [
    "for key, value in mydict.items():\n",
    "    print(key + \" : \" + str(value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# nested loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jalal Happy\n",
      "Jalal Lucky\n",
      "Jalal Honest\n",
      "Ripa Happy\n",
      "Ripa Lucky\n",
      "Ripa Honest\n",
      "Jara Happy\n",
      "Jara Lucky\n",
      "Jara Honest\n"
     ]
    }
   ],
   "source": [
    "list_item1  = ['Jalal','Ripa', 'Jara']\n",
    "list_item2  = ['Happy','Lucky', 'Honest']\n",
    "\n",
    "for i in list_item1:\n",
    "    for j in list_item2:\n",
    "        print(i,j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "5\n",
      "10\n",
      "17\n",
      "26\n",
      "5\n",
      "8\n",
      "13\n",
      "20\n",
      "29\n",
      "10\n",
      "13\n",
      "18\n",
      "25\n",
      "34\n",
      "17\n",
      "20\n",
      "25\n",
      "32\n",
      "41\n",
      "26\n",
      "29\n",
      "34\n",
      "41\n",
      "50\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "b = [1,2,3,4,5]\n",
    "\n",
    "for i in a:\n",
    "    for j in b:\n",
    "        c = i**2 + j**2\n",
    "        print(c)   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# save result using append"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 5, 10, 17, 26, 5, 8, 13, 20, 29, 10, 13, 18, 25, 34, 17, 20, 25, 32, 41, 26, 29, 34, 41, 50]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "b = [1,2,3,4,5]\n",
    "\n",
    "result = []\n",
    "\n",
    "for i in a:\n",
    "    for j in b:\n",
    "        c = i**2 + j**2 \n",
    "        result.append(c)\n",
    "        \n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# while loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "i is no longer less than 10\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "while i < 10:\n",
    "    print(i)\n",
    "    i += 1   # increments by 1\n",
    "    \n",
    "else:\n",
    "    print(\"i is no longer less than 10\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conditional statement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is the positive number\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "\n",
    "if a > 0:\n",
    "    print(\"a is the positive number\")\n",
    "elif a <0:\n",
    "    print(\"a is the negative number\")\n",
    "else:\n",
    "    print(\"a is equal zero\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is the positive number\n",
      "a is the positive number\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "\n",
    "if a > 0:\n",
    "    print(\"a is the positive number\")     \n",
    "    if a>5:                                    # nested if\n",
    "        print(\"a is the positive number\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# break and continue\n",
    "\n",
    "break    = stop the loop before it has looped through all the items\n",
    "\n",
    "continue = stop the current iteration of the loop, and continue with the next"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jalal\n",
      "Ripa\n"
     ]
    }
   ],
   "source": [
    "list_item1  = ['Jalal','Ripa', 'Jara']\n",
    "\n",
    "for i in list_item1:\n",
    "    print(i)   \n",
    "    \n",
    "    if i == 'Ripa':\n",
    "       break   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jalal\n"
     ]
    }
   ],
   "source": [
    "list_item1  = ['Jalal','Ripa', 'Jara']\n",
    "\n",
    "for i in list_item1:   \n",
    "    if i == 'Ripa':\n",
    "       break\n",
    "    print(i)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jalal\n",
      "Jara\n"
     ]
    }
   ],
   "source": [
    "list_item1  = ['Jalal','Ripa', 'Jara']\n",
    "\n",
    "for i in list_item1:   \n",
    "    if i == 'Ripa':\n",
    "       continue\n",
    "    print(i)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "\n",
    "while i < 10:\n",
    "    print(i)\n",
    "    \n",
    "    if i == 6:\n",
    "        break\n",
    "    i += 1         # increments by 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
