{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "

Conditional Statements and Loops in Python

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Day Objectives\n", "- Conditional Statements\n", " - if\n", " - if-else\n", " - if-elif-else\n", "- Loops in Python\n", " - For\n", " - While\n", "- Strings\n", " - Declaring a String\n", " - Accessing the Elements from the String\n", " - String Methods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conditional Statements\n", "\n", "Controlling the flow of execution\n", "\n", "\n", "#### Sytax\n", "- if\n", "```python\n", "if condition:\n", " Block of Code\n", "```\n", "- if-else\n", "```python\n", "if condition:\n", " Block of Code\n", "else:\n", " Block of Code\n", "```\n", "- if-elif-else\n", "```python\n", "if condition:\n", " Block of Code\n", "elif condition:\n", " Block of Code\n", "else:\n", " Block of Code\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Biggest of 2 numbers" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15 is the biggest number\n" ] } ], "source": [ "a = 5\n", "b = 15\n", "\n", "if a > b:\n", " print(a, \"is the biggest number\")\n", "else:\n", " print(b, \"is the biggest number\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 Both numbers are equal\n" ] } ], "source": [ "a = 5\n", "b = 5\n", "\n", "if a > b:\n", " print(a, \"is the biggest number\")\n", "elif b > a:\n", " print(b, \"is the biggest number\")\n", "else:\n", " print(a, \"Both numbers are equal\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "IndentationError", "evalue": "expected an indented block (, line 7)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m7\u001b[0m\n\u001b[1;33m print(b, \"is the biggest number\")\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n" ] } ], "source": [ "a = 5\n", "b = 15\n", "\n", "if a > b:\n", " print(a, \"is the biggest number\")\n", "else:\n", "print(b, \"is the biggest number\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Biggest of 3 numbers" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15 is the biggest number\n" ] } ], "source": [ "a = 5\n", "b = 15\n", "c = 8\n", "\n", "if a > b and a > c:\n", " print(a, \"is the biggest number\")\n", "elif b > c:\n", " print(b, \"is the biggest number\")\n", "else:\n", " print(c, \"is the biggest number\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- check the input number is even or not" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a Number: 698567\n", "698567 is not even number\n" ] } ], "source": [ "n = int(input(\"Enter a Number: \"))\n", "\n", "if n % 2 == 0:\n", " print(n, \"is the even number\")\n", "else:\n", " print(n, \"is not even number\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Verify given year leap year or not\n", "\n", "- It should by 4 and rem as 0\n", "- it shouldn't divisible by 100\n", "- divisible by 400" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a year: 1900\n", "1900 is not a leap year\n" ] } ], "source": [ "year = int(input(\"Enter a year: \"))\n", "\n", "if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):\n", " print(year, \"is a leap year\")\n", "else:\n", " print(year, \"is not a leap year\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculator Application\n", "\n", "- 2 input number\n", "- Arthematic operation" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number: 5\n", "Enter a number: 5\n", "What Arthematic I Need to Perform: -\n", "0\n", "10\n", "0\n", "25\n" ] } ], "source": [ "a = int(input(\"Enter a number: \"))\n", "b = int(input(\"Enter a number: \"))\n", "arthe = input(\"What Arthematic I Need to Perform: \")\n", "\n", "\n", "if arthe == '+':\n", " print(a + b)\n", "elif arthe == '-':\n", " print(a - b)\n", "elif arthe == '*':\n", " print(a * b)\n", "else:\n", " print(\"Given arthematic operation is invalid\")\n", "\n", "print(a+b)\n", "print(a-b)\n", "print(a*b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Loops in Python\n", "\n", "execute the block of code for multiple times\n", "\n", "- Initialization\n", "- Condition\n", "- inc/dec\n", "\n", "#### Syntax\n", "- For Loop -> How many time I need to Execuite the loop\n", "\n", "```python\n", "for IterativeVariable in GroupOfData:\n", " Block of Code\n", "```\n", "- While Loop -> Based on the condition \n", "\n", "```python\n", "init\n", "while condition:\n", " Block of Code\n", " inc/dec\n", "```\n", "\n", "\n", "### GroupOfNumber\n", "```\n", "range(init, cond, inc/dec)\n", "range(start, stop, step)\n", "\n", "cond -> required arg\n", "init -> 0\n", "inc/dec -> +1```" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "print(1)\n", "print(2)\n", "print(3)\n", "print(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display 1 to 100 numbers to the users\n", "\n", "1, >101, 1 \n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\t13\t14\t15\t16\t17\t18\t19\t20\t21\t22\t23\t24\t25\t26\t27\t28\t29\t30\t31\t32\t33\t34\t35\t36\t37\t38\t39\t40\t41\t42\t43\t44\t45\t46\t47\t48\t49\t50\t51\t52\t53\t54\t55\t56\t57\t58\t59\t60\t61\t62\t63\t64\t65\t66\t67\t68\t69\t70\t71\t72\t73\t74\t75\t76\t77\t78\t79\t80\t81\t82\t83\t84\t85\t86\t87\t88\t89\t90\t91\t92\t93\t94\t95\t96\t97\t98\t99\t100\t" ] } ], "source": [ "for i in range(1, 101, 1):\n", " print(i, end = '\\t')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\t13\t14\t15\t16\t17\t18\t19\t20\t21\t22\t23\t24\t25\t26\t27\t28\t29\t30\t31\t32\t33\t34\t35\t36\t37\t38\t39\t40\t41\t42\t43\t44\t45\t46\t47\t48\t49\t50\t51\t52\t53\t54\t55\t56\t57\t58\t59\t60\t61\t62\t63\t64\t65\t66\t67\t68\t69\t70\t71\t72\t73\t74\t75\t76\t77\t78\t79\t80\t81\t82\t83\t84\t85\t86\t87\t88\t89\t90\t91\t92\t93\t94\t95\t96\t97\t98\t99\t100\t" ] } ], "source": [ "for i in range(101):\n", " print(i, end = '\\t')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\t3\t5\t7\t9\t11\t13\t15\t17\t19\t21\t23\t25\t27\t29\t31\t33\t35\t37\t39\t41\t43\t45\t47\t49\t51\t53\t55\t57\t59\t61\t63\t65\t67\t69\t71\t73\t75\t77\t79\t81\t83\t85\t87\t89\t91\t93\t95\t97\t99\t" ] } ], "source": [ "for i in range(1, 101, 2):\n", " print(i, end = '\\t')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Even numbers between 1 to 1000" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\t4\t6\t8\t10\t12\t14\t16\t18\t20\t22\t24\t26\t28\t30\t32\t34\t36\t38\t40\t42\t44\t46\t48\t50\t52\t54\t56\t58\t60\t62\t64\t66\t68\t70\t72\t74\t76\t78\t80\t82\t84\t86\t88\t90\t92\t94\t96\t98\t100\t102\t104\t106\t108\t110\t112\t114\t116\t118\t120\t122\t124\t126\t128\t130\t132\t134\t136\t138\t140\t142\t144\t146\t148\t150\t152\t154\t156\t158\t160\t162\t164\t166\t168\t170\t172\t174\t176\t178\t180\t182\t184\t186\t188\t190\t192\t194\t196\t198\t200\t202\t204\t206\t208\t210\t212\t214\t216\t218\t220\t222\t224\t226\t228\t230\t232\t234\t236\t238\t240\t242\t244\t246\t248\t250\t252\t254\t256\t258\t260\t262\t264\t266\t268\t270\t272\t274\t276\t278\t280\t282\t284\t286\t288\t290\t292\t294\t296\t298\t300\t302\t304\t306\t308\t310\t312\t314\t316\t318\t320\t322\t324\t326\t328\t330\t332\t334\t336\t338\t340\t342\t344\t346\t348\t350\t352\t354\t356\t358\t360\t362\t364\t366\t368\t370\t372\t374\t376\t378\t380\t382\t384\t386\t388\t390\t392\t394\t396\t398\t400\t402\t404\t406\t408\t410\t412\t414\t416\t418\t420\t422\t424\t426\t428\t430\t432\t434\t436\t438\t440\t442\t444\t446\t448\t450\t452\t454\t456\t458\t460\t462\t464\t466\t468\t470\t472\t474\t476\t478\t480\t482\t484\t486\t488\t490\t492\t494\t496\t498\t500\t502\t504\t506\t508\t510\t512\t514\t516\t518\t520\t522\t524\t526\t528\t530\t532\t534\t536\t538\t540\t542\t544\t546\t548\t550\t552\t554\t556\t558\t560\t562\t564\t566\t568\t570\t572\t574\t576\t578\t580\t582\t584\t586\t588\t590\t592\t594\t596\t598\t600\t602\t604\t606\t608\t610\t612\t614\t616\t618\t620\t622\t624\t626\t628\t630\t632\t634\t636\t638\t640\t642\t644\t646\t648\t650\t652\t654\t656\t658\t660\t662\t664\t666\t668\t670\t672\t674\t676\t678\t680\t682\t684\t686\t688\t690\t692\t694\t696\t698\t700\t702\t704\t706\t708\t710\t712\t714\t716\t718\t720\t722\t724\t726\t728\t730\t732\t734\t736\t738\t740\t742\t744\t746\t748\t750\t752\t754\t756\t758\t760\t762\t764\t766\t768\t770\t772\t774\t776\t778\t780\t782\t784\t786\t788\t790\t792\t794\t796\t798\t800\t802\t804\t806\t808\t810\t812\t814\t816\t818\t820\t822\t824\t826\t828\t830\t832\t834\t836\t838\t840\t842\t844\t846\t848\t850\t852\t854\t856\t858\t860\t862\t864\t866\t868\t870\t872\t874\t876\t878\t880\t882\t884\t886\t888\t890\t892\t894\t896\t898\t900\t902\t904\t906\t908\t910\t912\t914\t916\t918\t920\t922\t924\t926\t928\t930\t932\t934\t936\t938\t940\t942\t944\t946\t948\t950\t952\t954\t956\t958\t960\t962\t964\t966\t968\t970\t972\t974\t976\t978\t980\t982\t984\t986\t988\t990\t992\t994\t996\t998\t1000\t" ] } ], "source": [ "for num in range(1, 1001, 1):\n", " if num % 2 == 0:\n", " print(num, end = '\\t')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Get the Leap Yeap year between the given range\n", " - Starting range ending range is given as input\n", "2. Get the prime numbers between the given range\n", "3. Get n number of Fibonacci numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### While Loop" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\t13\t14\t15\t16\t17\t18\t19\t20\t21\t22\t23\t24\t25\t26\t27\t28\t29\t30\t31\t32\t33\t34\t35\t36\t37\t38\t39\t40\t41\t42\t43\t44\t45\t46\t47\t48\t49\t50\t51\t52\t53\t54\t55\t56\t57\t58\t59\t60\t61\t62\t63\t64\t65\t66\t67\t68\t69\t70\t71\t72\t73\t74\t75\t76\t77\t78\t79\t80\t81\t82\t83\t84\t85\t86\t87\t88\t89\t90\t91\t92\t93\t94\t95\t96\t97\t98\t99\t100\t" ] } ], "source": [ "i = 1\n", "while i < 101:\n", " print(i, end = '\\t')\n", " i += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Break: To terminate the execution of the loop\n", "- Continue: Skip the current execuition of the loop" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "print the numbers between 1 to 100 whenever I reached 50 stop travelling" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\t13\t14\t15\t16\t17\t18\t19\t20\t21\t22\t23\t24\t25\t26\t27\t28\t29\t30\t31\t32\t33\t34\t35\t36\t37\t38\t39\t40\t41\t42\t43\t44\t45\t46\t47\t48\t49\t" ] } ], "source": [ "for travel in range(1, 101, 1):\n", " if travel == 50:\n", " break\n", " print(travel, end = '\\t')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\t3\t5\t7\t9\t11\t13\t15\t17\t19\t21\t23\t25\t27\t29\t31\t33\t35\t37\t39\t41\t43\t45\t47\t49\t" ] } ], "source": [ "for travel in range(1, 101, 1):\n", " if travel == 50:\n", " break\n", " elif travel % 2 == 0:\n", " continue\n", " print(travel, end = '\\t')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "646\n", "646\n", "kbjh\n", "kbjh\n", "nb\n", "nb\n", "vj\n", "vj\n", "nv\n", "nv\n", "inp\n", "inp\n", "mbds\n", "mbds\n", "ksbcc\n", "ksbcc\n", "cancel\n", "Loop Terminated\n" ] } ], "source": [ "while True:\n", " inp = input()\n", " if inp == 'cancel':\n", " print(\"Loop Terminated\")\n", " break\n", " print(inp)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings\n", "\n", "it is group of data enclosed between \"\", '', \"\"\"\"\"\", '''''' are called strings in python\n", "\n", "#### Properties of Strings\n", "\n", "- It is immutable data type\n", "- It is iterable" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "s = \"\"\n", "s2 = str()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "s = 'I am a single line String'\n", "s2 = \"I'm also a single line string\"\n", "s3 = '''I am\n", "a\n", "multi\n", "line\n", "string'''\n", "\n", "s3 = \"\"\"I'm\n", "a\n", "multi\n", "line\n", "string\"\"\"" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "print(type(s), type(s3))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I\t'\tm\t\n", "\ta\t\n", "\tm\tu\tl\tt\ti\t\n", "\tl\ti\tn\te\t\n", "\ts\tt\tr\ti\tn\tg\t" ] } ], "source": [ "for char in s3:\n", " print(char, end = '\\t')" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "s = \"\"\"A leap year (also known as an intercalary year or bissextile year) is a calendar year that contains an additional day (or, in the case of a lunisolar calendar, a month) added to keep the calendar year synchronized with the astronomical year or seasonal year.[1] Because astronomical events and seasons do not repeat in a whole number of days, calendars that have a constant number of days in each year will unavoidably drift over time with respect to the event that the year is supposed to track, such as seasons. By inserting (called intercalating in technical terminology) an additional day or month into some years, the drift between a civilization's dating system and the physical properties of the solar system can be corrected. A year that is not a leap year is a common year.\n", "\n", "For example, in the Gregorian calendar, each leap year has 366 days instead of 365, by extending February to 29 days rather than the common 28. These extra days occur in each year which is an integer multiple of 4 (except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400). The leap year of 366 days has 52 weeks and two days, hence the year following a leap year will start later by two days of the week.\"\"\"" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A leap year (also known as an intercalary year or bissextile year) is a calendar year that contains an additional day (or, in the case of a lunisolar calendar, a month) added to keep the calendar year synchronized with the astronomical year or seasonal year.[1] Because astronomical events and seasons do not repeat in a whole number of days, calendars that have a constant number of days in each year will unavoidably drift over time with respect to the event that the year is supposed to track, such as seasons. By inserting (called intercalating in technical terminology) an additional day or month into some years, the drift between a civilization's dating system and the physical properties of the solar system can be corrected. A year that is not a leap year is a common year.\n", "\n", "For example, in the Gregorian calendar, each leap year has 366 days instead of 365, by extending February to 29 days rather than the common 28. These extra days occur in each year which is an integer multiple of 4 (except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400). The leap year of 366 days has 52 weeks and two days, hence the year following a leap year will start later by two days of the week.\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\t \tl\te\ta\tp\t \ty\te\ta\tr\t \t(\ta\tl\ts\to\t \tk\tn\to\tw\tn\t \ta\ts\t \ta\tn\t \ti\tn\tt\te\tr\tc\ta\tl\ta\tr\ty\t \ty\te\ta\tr\t \to\tr\t \tb\ti\ts\ts\te\tx\tt\ti\tl\te\t \ty\te\ta\tr\t)\t \ti\ts\t \ta\t \tc\ta\tl\te\tn\td\ta\tr\t \ty\te\ta\tr\t \tt\th\ta\tt\t \tc\to\tn\tt\ta\ti\tn\ts\t \ta\tn\t \ta\td\td\ti\tt\ti\to\tn\ta\tl\t \td\ta\ty\t \t(\to\tr\t,\t \ti\tn\t \tt\th\te\t \tc\ta\ts\te\t \to\tf\t \ta\t \tl\tu\tn\ti\ts\to\tl\ta\tr\t \tc\ta\tl\te\tn\td\ta\tr\t,\t \ta\t \tm\to\tn\tt\th\t)\t \ta\td\td\te\td\t \tt\to\t \tk\te\te\tp\t \tt\th\te\t \tc\ta\tl\te\tn\td\ta\tr\t \ty\te\ta\tr\t \ts\ty\tn\tc\th\tr\to\tn\ti\tz\te\td\t \tw\ti\tt\th\t \tt\th\te\t \ta\ts\tt\tr\to\tn\to\tm\ti\tc\ta\tl\t \ty\te\ta\tr\t \to\tr\t \ts\te\ta\ts\to\tn\ta\tl\t \ty\te\ta\tr\t.\t[\t1\t]\t \tB\te\tc\ta\tu\ts\te\t \ta\ts\tt\tr\to\tn\to\tm\ti\tc\ta\tl\t \te\tv\te\tn\tt\ts\t \ta\tn\td\t \ts\te\ta\ts\to\tn\ts\t \td\to\t \tn\to\tt\t \tr\te\tp\te\ta\tt\t \ti\tn\t \ta\t \tw\th\to\tl\te\t \tn\tu\tm\tb\te\tr\t \to\tf\t \td\ta\ty\ts\t,\t \tc\ta\tl\te\tn\td\ta\tr\ts\t \tt\th\ta\tt\t \th\ta\tv\te\t \ta\t \tc\to\tn\ts\tt\ta\tn\tt\t \tn\tu\tm\tb\te\tr\t \to\tf\t \td\ta\ty\ts\t \ti\tn\t \te\ta\tc\th\t \ty\te\ta\tr\t \tw\ti\tl\tl\t \tu\tn\ta\tv\to\ti\td\ta\tb\tl\ty\t \td\tr\ti\tf\tt\t \to\tv\te\tr\t \tt\ti\tm\te\t \tw\ti\tt\th\t \tr\te\ts\tp\te\tc\tt\t \tt\to\t \tt\th\te\t \te\tv\te\tn\tt\t \tt\th\ta\tt\t \tt\th\te\t \ty\te\ta\tr\t \ti\ts\t \ts\tu\tp\tp\to\ts\te\td\t \tt\to\t \tt\tr\ta\tc\tk\t,\t \ts\tu\tc\th\t \ta\ts\t \ts\te\ta\ts\to\tn\ts\t.\t \tB\ty\t \ti\tn\ts\te\tr\tt\ti\tn\tg\t \t(\tc\ta\tl\tl\te\td\t \ti\tn\tt\te\tr\tc\ta\tl\ta\tt\ti\tn\tg\t \ti\tn\t \tt\te\tc\th\tn\ti\tc\ta\tl\t \tt\te\tr\tm\ti\tn\to\tl\to\tg\ty\t)\t \ta\tn\t \ta\td\td\ti\tt\ti\to\tn\ta\tl\t \td\ta\ty\t \to\tr\t \tm\to\tn\tt\th\t \ti\tn\tt\to\t \ts\to\tm\te\t \ty\te\ta\tr\ts\t,\t \tt\th\te\t \td\tr\ti\tf\tt\t \tb\te\tt\tw\te\te\tn\t \ta\t \tc\ti\tv\ti\tl\ti\tz\ta\tt\ti\to\tn\t'\ts\t \td\ta\tt\ti\tn\tg\t \ts\ty\ts\tt\te\tm\t \ta\tn\td\t \tt\th\te\t \tp\th\ty\ts\ti\tc\ta\tl\t \tp\tr\to\tp\te\tr\tt\ti\te\ts\t \to\tf\t \tt\th\te\t \ts\to\tl\ta\tr\t \ts\ty\ts\tt\te\tm\t \tc\ta\tn\t \tb\te\t \tc\to\tr\tr\te\tc\tt\te\td\t.\t \tA\t \ty\te\ta\tr\t \tt\th\ta\tt\t \ti\ts\t \tn\to\tt\t \ta\t \tl\te\ta\tp\t \ty\te\ta\tr\t \ti\ts\t \ta\t \tc\to\tm\tm\to\tn\t \ty\te\ta\tr\t.\t\n", "\t\n", "\tF\to\tr\t \te\tx\ta\tm\tp\tl\te\t,\t \ti\tn\t \tt\th\te\t \tG\tr\te\tg\to\tr\ti\ta\tn\t \tc\ta\tl\te\tn\td\ta\tr\t,\t \te\ta\tc\th\t \tl\te\ta\tp\t \ty\te\ta\tr\t \th\ta\ts\t \t3\t6\t6\t \td\ta\ty\ts\t \ti\tn\ts\tt\te\ta\td\t \to\tf\t \t3\t6\t5\t,\t \tb\ty\t \te\tx\tt\te\tn\td\ti\tn\tg\t \tF\te\tb\tr\tu\ta\tr\ty\t \tt\to\t \t2\t9\t \td\ta\ty\ts\t \tr\ta\tt\th\te\tr\t \tt\th\ta\tn\t \tt\th\te\t \tc\to\tm\tm\to\tn\t \t2\t8\t.\t \tT\th\te\ts\te\t \te\tx\tt\tr\ta\t \td\ta\ty\ts\t \to\tc\tc\tu\tr\t \ti\tn\t \te\ta\tc\th\t \ty\te\ta\tr\t \tw\th\ti\tc\th\t \ti\ts\t \ta\tn\t \ti\tn\tt\te\tg\te\tr\t \tm\tu\tl\tt\ti\tp\tl\te\t \to\tf\t \t4\t \t(\te\tx\tc\te\tp\tt\t \tf\to\tr\t \ty\te\ta\tr\ts\t \te\tv\te\tn\tl\ty\t \td\ti\tv\ti\ts\ti\tb\tl\te\t \tb\ty\t \t1\t0\t0\t,\t \tw\th\ti\tc\th\t \ta\tr\te\t \tn\to\tt\t \tl\te\ta\tp\t \ty\te\ta\tr\ts\t \tu\tn\tl\te\ts\ts\t \te\tv\te\tn\tl\ty\t \td\ti\tv\ti\ts\ti\tb\tl\te\t \tb\ty\t \t4\t0\t0\t)\t.\t \tT\th\te\t \tl\te\ta\tp\t \ty\te\ta\tr\t \to\tf\t \t3\t6\t6\t \td\ta\ty\ts\t \th\ta\ts\t \t5\t2\t \tw\te\te\tk\ts\t \ta\tn\td\t \tt\tw\to\t \td\ta\ty\ts\t,\t \th\te\tn\tc\te\t \tt\th\te\t \ty\te\ta\tr\t \tf\to\tl\tl\to\tw\ti\tn\tg\t \ta\t \tl\te\ta\tp\t \ty\te\ta\tr\t \tw\ti\tl\tl\t \ts\tt\ta\tr\tt\t \tl\ta\tt\te\tr\t \tb\ty\t \tt\tw\to\t \td\ta\ty\ts\t \to\tf\t \tt\th\te\t \tw\te\te\tk\t.\t" ] } ], "source": [ "for char in s:\n", " print(char, end = '\\t')" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "e a e a a o o a a i e a a e a o i e i e e a i a a e a e a a o a i a a i i o a a o i e a e o a u i o a a e a a o a e o e e e a e a e a o i e i e a o o i a e a o e a o a e a e a u e a o o i a e e a e a o o o e e a i a o e u e o a a e a a a e a o a u e o a i e a e a i u a o i a i o e i e i e e o e e e a e e a i u o e o a u a e a o i e i a e i e a a i i e i a e i o o a a i i o a a o o i o o e e a e i e e e a i i i a i o a i e a e i a o e i e o e o a e a e o e e e a a i o a e a e a i a o o e a o e a e i e e o i a a e a e a e a e a a a i e a o e e i e u a o a a e a e o o e e e a a o u i e a e a i i a i e e u i e o e e o e a e e i i i e i a e o e a e a u e e e i i i e e e a e a o a a e e a o a e e e e a o o i a e a e a i a a e o a o e e e " ] } ], "source": [ "for char in s:\n", " if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u':\n", " print(char, end = ' ')" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A e a e a a o o a a i e a a e a o i e i e e a i a a e a e a a o a i a a i i o a a o i e a e o a u i o a a e a a o a e o e e e a e a e a o i e i e a o o i a e a o e a o a e a e a u e a o o i a e e a e a o o o e e a i a o e u e o a a e a a a e a o a u e o a i e a e a i u a o i a i o e i e i e e o e e e a e e a i u o e o a u a e a o i e i a e i e a a i i e i a e i o o a a i i o a a o o i o o e e a e i e e e a i i i a i o a i e a e i a o e i e o e o a e a e o e e A e a a i o a e a e a i a o o e a o e a e i e e o i a a e a e a e a e a a a i e a o e e i e u a o a a e a e o o e e e a a o u i e a e a i i a i e e u i e o e e o e a e e i i i e i a e o e a e a u e e e i i i e e e a e a o a a e e a o a e e e e a o o i a e a e a i a a e o a o e e e " ] } ], "source": [ "for char in s:\n", " if char in 'aeiouAEIOU':\n", " print(char, end = ' ')" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 Upper Case Ovels 371 Lower 857 Consants\n" ] } ], "source": [ "lo = 0\n", "uo = 0\n", "c = 0\n", "\n", "for char in s:\n", " if char in 'aeiou':\n", " lo += 1\n", " elif char in 'AEIOU':\n", " uo += 1\n", " else:\n", " c += 1\n", "print(uo, \"Upper Case Ovels\", lo , \"Lower\", c, \"Consants\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing the characters from the string\n", "\n", "\n", "- Indexing\n", " - +ve/Forward\n", " - -ve/Reverse\n", "- Slicing\n", " - +ve/Forward\n", " - -ve/Reverse" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A leap year (also known as an intercalary year or bissextile year) is a calendar year that contains an additional day (or, in the case of a lunisolar calendar, a month) added to keep the calendar year synchronized with the astronomical year or seasonal year.[1] Because astronomical events and seasons do not repeat in a whole number of days, calendars that have a constant number of days in each year will unavoidably drift over time with respect to the event that the year is supposed to track, such as seasons. By inserting (called intercalating in technical terminology) an additional day or month into some years, the drift between a civilization's dating system and the physical properties of the solar system can be corrected. A year that is not a leap year is a common year.\n", "\n", "For example, in the Gregorian calendar, each leap year has 366 days instead of 365, by extending February to 29 days rather than the common 28. These extra days occur in each year which is an integer multiple of 4 (except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400). The leap year of 366 days has 52 weeks and two days, hence the year following a leap year will start later by two days of the week.\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\n" ] } ], "source": [ "print(s[0])" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n" ] } ], "source": [ "print(s[4])" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ".\n" ] } ], "source": [ "print(s[-1])" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "w\n" ] } ], "source": [ "print(s[-5])" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }