{
"cells": [
{
"cell_type": "markdown",
"id": "5fca9845",
"metadata": {
"id": "5fca9845"
},
"source": [
"# Indexing lists and palindromes"
]
},
{
"cell_type": "markdown",
"id": "e0586cb8",
"metadata": {
"id": "e0586cb8"
},
"source": [
"Reminder: if `L` is a list then `L[-1]` is the last element of the list, and `L[-2]` is the one before last elements, etc.. Same for _strings_."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd9d63e6",
"metadata": {
"id": "fd9d63e6",
"outputId": "298426c7-46ff-4a27-9db7-80052fcfc5fb"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"L[-1]=e\n",
"L[-2]=d\n"
]
}
],
"source": [
"L = [\"a\",\"b\",\"c\",\"d\",\"e\"]\n",
"print(f\"L[-1]={L[-1]}\")\n",
"print(f\"L[-2]={L[-2]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c5cb699",
"metadata": {
"id": "3c5cb699",
"outputId": "cec91967-9641-44d6-82c1-f8785f261c26"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n"
]
}
],
"source": [
"x = \"boaz\"\n",
"print(x[-2])"
]
},
{
"cell_type": "markdown",
"id": "c9f107b7",
"metadata": {
"id": "c9f107b7"
},
"source": [
"__Question from lab:__ A _Palindrome_ is a string that is the same if you read it forward and backwards:\n",
"\n",
"Which one is a Palindrome?\n",
"\n",
"* `kayak`?\n",
"\n",
"* `banana`?\n",
"\n",
"* `racecar`?\n",
"\n",
"* `aaabaaa`?"
]
},
{
"cell_type": "markdown",
"id": "6cef6f11",
"metadata": {
"id": "6cef6f11"
},
"source": [
"Write a function `pal4(s)` that takes as input `s` of length 4, and returns `True` if `s` is a Palindrome and `False` otherwise. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9334b615",
"metadata": {
"id": "9334b615"
},
"outputs": [],
"source": [
"def pal4(s):\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d207156",
"metadata": {
"id": "3d207156",
"outputId": "85054644-fe1a-45d2-86ba-51c1b112be8a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n"
]
}
],
"source": [
"print(pal4(\"abba\"))\n",
"print(pal4(\"boaz\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9f5de75f",
"metadata": {
"id": "9f5de75f"
},
"outputs": [],
"source": [
"def pal4(s):\n",
" if s[0]==s[-1] and s[1]==s[-2]:\n",
" return True\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2bb21c6",
"metadata": {
"id": "c2bb21c6"
},
"outputs": [],
"source": [
"# version with _or_:\n",
"def pal4_or(s):\n",
" if s[0]!=s[-1] or s[1]!=s[-2]:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "be61c5b3",
"metadata": {
"id": "be61c5b3",
"outputId": "fadad6fa-5182-4f22-e8b2-705b91d49c49"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pal4 and pal4_or agree on abba\n",
"pal4 and pal4_or agree on boaz\n",
"pal4 and pal4_or agree on naan\n",
"pal4 and pal4_or agree on help\n",
"pal4 and pal4_or agree on deed\n"
]
}
],
"source": [
"check_words = [\"abba\", \"boaz\", \"naan\", \"help\", \"deed\"]\n",
"for word in check_words:\n",
" if pal4_or(word)==pal4(word):\n",
" print(f\"pal4 and pal4_or agree on {word}\")\n",
" else:\n",
" print(f\"pal4and pal4_or disagree on {word}!!!!\")\n",
" "
]
},
{
"cell_type": "markdown",
"id": "325baaeb",
"metadata": {
"id": "325baaeb"
},
"source": [
"Write a function `pal6(s)` that takes as input `s` of length 6, and returns `True` if `s` is a Palindrome and `False` otherwise. Use `or`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b47717e",
"metadata": {
"id": "8b47717e"
},
"outputs": [],
"source": [
"def pal6(s):\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b12e99e8",
"metadata": {
"id": "b12e99e8"
},
"outputs": [],
"source": [
"def pal6(s):\n",
" if s[0]!=s[-1] or s[1]!=s[-2] or s[2] != s[-3]:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a62317",
"metadata": {
"id": "85a62317",
"outputId": "e9baf232-8db3-43e9-99bf-af90ff3bc8db"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pal6(\"abccba\")"
]
},
{
"cell_type": "markdown",
"id": "ff74fbc9",
"metadata": {
"id": "ff74fbc9"
},
"source": [
"Write function `pal(s)` that works for Palindromes for every even length. As bonus, make it work for every length"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89aab639",
"metadata": {
"id": "89aab639"
},
"outputs": [],
"source": [
"def pal(s):\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09d402b4",
"metadata": {
"id": "09d402b4"
},
"outputs": [],
"source": [
"def pal(s):\n",
" n = len(s)//2\n",
" for i in range(n):\n",
" if s[i] != s[-i-1]:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6cdff61c",
"metadata": {
"id": "6cdff61c",
"outputId": "e7b94b5d-36e2-4409-eb77-dd63fd7bd01c"
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pal(\"hello\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d62fd3a3",
"metadata": {
"id": "d62fd3a3",
"outputId": "b13351f4-b459-43a6-c2c8-0e2ef564d454"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pal(\"aaaaabcbaaaaa\")"
]
},
{
"cell_type": "markdown",
"id": "ddbbe224",
"metadata": {
"id": "ddbbe224"
},
"source": [
"Write a function `reverse(s)` that takes a string `s` and returns its reverse. \n",
"\n",
"`reverse(\"boaz\")` will be `\"zaob\"`\n",
"\n",
"Hint: if `x` is a string and `a` is a letter then `x = x+ a` changes `x` so that now `a` is added to end of the string `x` and `x = a + x` changes `x` to the string where `a` is added to the beginning the previous `x`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e725bb0a",
"metadata": {
"id": "e725bb0a"
},
"outputs": [],
"source": [
"def reverse(s):\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc11b6df",
"metadata": {
"id": "bc11b6df"
},
"outputs": [],
"source": [
"def reverse(s):\n",
" res = \"\"\n",
" for a in s:\n",
" res = a + res\n",
" return res"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7c203a3",
"metadata": {
"id": "b7c203a3",
"outputId": "e22f7d4b-22ff-4a0d-9825-f654e4de33d5"
},
"outputs": [
{
"data": {
"text/plain": [
"'zaob'"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reverse(\"boaz\")"
]
},
{
"cell_type": "markdown",
"id": "7a843716",
"metadata": {
"id": "7a843716"
},
"source": [
"__Exercise:__ Use `reverse` to give a function to compute `pal` (Palindromes). Can you do it in three lines? How about in one?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ae82d679",
"metadata": {
"id": "ae82d679"
},
"outputs": [],
"source": [
"def pal(s):\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "967d429e",
"metadata": {
"id": "967d429e"
},
"outputs": [],
"source": [
"def pal(s):\n",
" if s==reverse(s):\n",
" return True\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfc927e9",
"metadata": {
"id": "cfc927e9"
},
"outputs": [],
"source": [
"def pal(s):\n",
" return s == reverse(s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53b711bb",
"metadata": {
"id": "53b711bb",
"outputId": "ef5709d7-514b-4484-d500-f0c2b7457edf"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pal(\"racecar\")"
]
},
{
"cell_type": "markdown",
"id": "b6016d7b",
"metadata": {
"id": "b6016d7b"
},
"source": [
"## Slicing"
]
},
{
"cell_type": "markdown",
"id": "5e798527",
"metadata": {
"id": "5e798527"
},
"source": [
"If `L` is a list then `L[2:7]` is the elements of `L` in positions 2 until 6. Same for string."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b09ced0",
"metadata": {
"id": "6b09ced0",
"outputId": "01134149-852f-4397-ce4d-9480360d6ac6"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['c', 'd']\n",
"['e', 'f', 'g']\n"
]
}
],
"source": [
"L = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\"]\n",
"# What will be printed\n",
"print(L[2:4])\n",
"print(L[4:7])"
]
},
{
"cell_type": "markdown",
"id": "0de2ae40",
"metadata": {
"id": "0de2ae40"
},
"source": [
"If `L` is a list then `L[2:]` is the elements of `L` in positions 2 until the end, `L[:7]` is the elements of `L` from the beginning until position 6. Same for strings."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b9e85c26",
"metadata": {
"id": "b9e85c26",
"outputId": "ba11ad59-0911-43c9-9b5c-a5623e8de4dc"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Jam\n",
"Coders\n"
]
}
],
"source": [
"s = \"JamCoders\"\n",
"a = s[:3]\n",
"b = s[3:]\n",
"print(a)\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"id": "84f813ea",
"metadata": {
"id": "84f813ea"
},
"source": [
"## Questions?"
]
},
{
"cell_type": "markdown",
"id": "81a3227e",
"metadata": {
"id": "81a3227e"
},
"source": [
"\n",
"## More function examples "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2df58f99",
"metadata": {
"id": "2df58f99"
},
"outputs": [],
"source": [
"def passing_grade(score):\n",
" if score > 50:\n",
" return True\n",
" return False\n",
"print(passing_grade(60))\n",
"print(passing_grade(25))"
]
},
{
"cell_type": "markdown",
"id": "b7e62e9f",
"metadata": {
"id": "b7e62e9f"
},
"source": [
"## Reminder - difference between print and return"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ffa27cf",
"metadata": {
"id": "5ffa27cf"
},
"outputs": [],
"source": [
"def passing_grade(score): # print version\n",
" if score > 50:\n",
" print(\"True\")\n",
" print(\"False\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91dd615f",
"metadata": {
"id": "91dd615f"
},
"outputs": [],
"source": [
"if passing_grade(99):\n",
" print(\"That's a great grade\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f7938fc",
"metadata": {
"id": "7f7938fc"
},
"outputs": [],
"source": [
"def passing_grade(score): # print version\n",
" if score > 50:\n",
" return True\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3042c651",
"metadata": {
"id": "3042c651"
},
"outputs": [],
"source": [
"if passing_grade(99):\n",
" print(\"That's a great grade\")"
]
},
{
"cell_type": "markdown",
"id": "6e4e13b1",
"metadata": {
"id": "6e4e13b1"
},
"source": [
"# Using a function"
]
},
{
"cell_type": "markdown",
"id": "b3be8a25",
"metadata": {
"id": "b3be8a25"
},
"source": [
"Suppose we have the `passing_grade` function above. Write a function `passed` that takes as input two lists `students` and `grades` and prints the names of the students that passed."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c5e522d8",
"metadata": {
"id": "c5e522d8"
},
"outputs": [],
"source": [
"def passed(students,grades):\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80742a55",
"metadata": {
"id": "80742a55"
},
"outputs": [],
"source": [
"def passed(students,grades):\n",
" n = len(students)\n",
" for i in range(n):\n",
" if passing_grade(grades[i]):\n",
" print(students[i])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc2ad5cb",
"metadata": {
"id": "cc2ad5cb"
},
"outputs": [],
"source": [
"students = [ \"Bereket\", \"Annamira\", \"Elijah\", \"Orr\", \"Jabari\", \"Annakai\", \"Tyler\" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4935e7d",
"metadata": {
"id": "f4935e7d"
},
"outputs": [],
"source": [
"# let's make grades random for fairness\n",
"import random\n",
"grades = []\n",
"for i in range(len(students)):\n",
" grades.append(random.randint(1,100))\n",
"\n",
"grades"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ec3c7db",
"metadata": {
"id": "3ec3c7db"
},
"outputs": [],
"source": [
"passed(students,grades)"
]
},
{
"cell_type": "markdown",
"id": "bd090a30",
"metadata": {
"id": "bd090a30"
},
"source": [
"# Secret codes \n",
"\n",
"We will write a function `enc(s)` that takes a string `s` and outputs it to a string where `a` is replaced with `z`, `b` is replaced with `y`, and so on\n",
"\n",
"\n",
"So `enc(car)` is `yaj`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a76209ac",
"metadata": {
"id": "a76209ac",
"outputId": "78ee78c8-286f-4d80-9c04-23afc4ea9fad"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-1\n"
]
}
],
"source": [
"# If a and b are strings, then a.find(b) gives the first position where b is in a. Otherwise it gives -1\n",
"\n",
"x = \"abcdefg\"\n",
"y = \"h\"\n",
"print(x.find(y))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82ae7ef3",
"metadata": {
"id": "82ae7ef3",
"outputId": "c03c1bb4-c0de-468a-8058-46497c91c24f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
]
}
],
"source": [
"from string import ascii_letters\n",
"print(ascii_letters)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afb49dae",
"metadata": {
"id": "afb49dae",
"outputId": "65582512-836b-4fad-cd8b-956dd4415d4d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"abcdefghijklmnopqrstuvwxyz\n"
]
}
],
"source": [
"lowercase = ascii_letters[:26]\n",
"print(lowercase)"
]
},
{
"cell_type": "markdown",
"id": "5cd60e17",
"metadata": {
"id": "5cd60e17"
},
"source": [
"Write a function `enc(s)` that takes a string `s` and outputs it to a string where `a` is replaced with `z`, `b` is replaced with `y`, and so on\n",
"\n",
"Can use:\n",
"* The string `lowercase` that contains all lowercase letters.\n",
"* The `find` operation: `a.find(b)` is the first position of `b` in `a`\n",
"\n",
"We'll use it to decode `ytjmnsdd`\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5acda900",
"metadata": {
"id": "5acda900"
},
"outputs": [],
"source": [
"def enc(s):\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c0246191",
"metadata": {
"id": "c0246191"
},
"outputs": [],
"source": [
"def enc(s):\n",
" res = \"\"\n",
" for a in s:\n",
" i = lowercase.find(a)\n",
" res += lowercase[-i]\n",
" return res"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1bf11487",
"metadata": {
"id": "1bf11487",
"outputId": "eda389c0-9105-4608-a3e7-30b589d9a4d5"
},
"outputs": [
{
"data": {
"text/plain": [
"'chronixx'"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"enc(\"ytjmnsdd\")"
]
},
{
"cell_type": "markdown",
"id": "caf9d65d",
"metadata": {
"id": "caf9d65d"
},
"source": [
"# Functions (variable scoping)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b7dcfd7",
"metadata": {
"id": "4b7dcfd7"
},
"outputs": [],
"source": [
"# Variables inside a function have no relation to variables outside the function\n",
"# See what happens when \"i\" is printed inside and outside the function\n",
"\n",
"i = 0\n",
"def func(x):\n",
" i = 10\n",
" print(\"i=\",i, \"x=\",x)\n",
" \n",
"func(5)\n",
"print(\"i=\",i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2ca5b3cb",
"metadata": {
"id": "2ca5b3cb"
},
"outputs": [],
"source": [
"# Variables across functions have no relation to each other\n",
"# A variable declared inside a function will have no relation to outside the function\n",
"# Why is there an error?\n",
"\n",
"def func1(x):\n",
" j = 10\n",
" print(\"j=\",j, \"x=\",x)\n",
"\n",
"def func2(x):\n",
" j = 5\n",
" func1(x)\n",
" print(\"x=\",x)\n",
" \n",
"func2(3)\n",
"print(j)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "041a93d5",
"metadata": {
"id": "041a93d5"
},
"outputs": [],
"source": [
"# Why is this the output?\n",
"\n",
"k = 0\n",
"for i in range(3):\n",
" print(k)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc45f73a",
"metadata": {
"id": "bc45f73a"
},
"outputs": [],
"source": [
"# How about here?\n",
"\n",
"k = 0\n",
"for i in range(3):\n",
" k = 6\n",
" print(k)"
]
},
{
"cell_type": "markdown",
"id": "0150c921",
"metadata": {
"id": "0150c921"
},
"source": [
"# Advanced for loop keywords"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a55cbc77",
"metadata": {
"id": "a55cbc77"
},
"outputs": [],
"source": [
"# This is a reminder of what a loop looks like\n",
"\n",
"for i in range(20):\n",
" print('still in loop')\n",
"print('Loop completed!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33d241d5",
"metadata": {
"id": "33d241d5"
},
"outputs": [],
"source": [
"# There are two keywords for loops that you will need to know\n",
"# The first is \"break\"\n",
"# Break will exit a for loop once it is executed\n",
"\n",
"for i in range(4):\n",
" print(i)\n",
" if i == 2:\n",
" break\n",
" print(\"---\")\n",
"print('Loop completed!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7af28f7",
"metadata": {
"id": "c7af28f7"
},
"outputs": [],
"source": [
"# The other keyword you need to know is \"continue\"\n",
"# \"continue\" will make it so that the rest of the loop body is not executed\n",
"# but ONLY for the time it is called\n",
"# Notice that \"10\" is not printed\n",
"\n",
"\n",
"for i in range(4):\n",
" print(i)\n",
" if i == 2:\n",
" continue\n",
" print(\"---\")\n",
"print('Loop completed!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e1dedbd",
"metadata": {
"id": "0e1dedbd"
},
"outputs": [],
"source": [
"# You can have continue and break in the same loop\n",
"\n",
"for i in range(6):\n",
" if i == 2:\n",
" continue\n",
" if i == 4:\n",
" break\n",
" print(i)\n",
"print('Loop completed!')"
]
},
{
"cell_type": "markdown",
"id": "040941df",
"metadata": {
"id": "040941df"
},
"source": [
"# While loops"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a8abea4",
"metadata": {
"id": "3a8abea4"
},
"outputs": [],
"source": [
"# This is the generic syntax for a while loop\n",
"\n",
"while CONTIDION:\n",
" LOOP_BODY"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee1d6ba8",
"metadata": {
"id": "ee1d6ba8"
},
"outputs": [],
"source": [
"# A \"while loop\" is a loop that executes while CONDITION is true\n",
"# The CONDITION is evaluated once the loop body finishes, and only then\n",
"\n",
"i = 0\n",
"while i < 10:\n",
" print(i)\n",
" i += 1\n",
"print('Loop completed!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8391eae",
"metadata": {
"id": "d8391eae"
},
"outputs": [],
"source": [
"# If CONDITION is False at the start of the loop, the loop body will not execute!\n",
"\n",
"i = 0\n",
"while i < 0:\n",
" print(i)\n",
" i += 1\n",
"print('Loop completed!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "088d0433",
"metadata": {
"id": "088d0433"
},
"outputs": [],
"source": [
"# What is different about this and the one two above?\n",
"\n",
"i = 0\n",
"while i < 10:\n",
" i += 1\n",
" print(i)\n",
"print('Loop completed!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5571a3b",
"metadata": {
"id": "e5571a3b"
},
"outputs": [],
"source": [
"# WARNING!!!!\n",
"# You can have loops that run forever.\n",
"# These are really really bad.\n",
"# Make sure the loop terminates.\n",
"\n",
"while True:\n",
" print('hi!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd512235",
"metadata": {
"id": "cd512235"
},
"outputs": [],
"source": [
"# WARNING!!!!!\n",
"# Even if you don't mean to, the loop can run forever.\n",
"# DO NOT do this.\n",
"\n",
"i = 0\n",
"while i < 10:\n",
" print(i)\n",
"print('Loop completed!') # This will never execute!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df22170b",
"metadata": {
"id": "df22170b"
},
"outputs": [],
"source": [
"# You can use \"continue\" in while loops\n",
"# The syntax and behavior is exactly the same as in for loop\n",
"\n",
"i = 0\n",
"while i < 10:\n",
" i += 1\n",
" if i == 5:\n",
" continue\n",
" print(i)\n",
"print('Loop completed!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c7d6e23",
"metadata": {
"id": "4c7d6e23"
},
"outputs": [],
"source": [
"# WARNING!!!!\n",
"# This loop will also run forever. Why?\n",
"# Don't do this.\n",
"\n",
"i = 0\n",
"while i < 10:\n",
" if i == 5:\n",
" continue\n",
" print(i)\n",
" i += 1\n",
"print('Loop completed!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a4b4624e",
"metadata": {
"id": "a4b4624e"
},
"outputs": [],
"source": [
"# You can also use \"break\" in while loops\n",
"# The syntax and usage is exactly the same as in for loops\n",
"\n",
"i = 0\n",
"while True:\n",
" if i > 10:\n",
" break\n",
" print(i)\n",
" i += 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7c993c3",
"metadata": {
"id": "f7c993c3"
},
"outputs": [],
"source": [
"# What will happen?\n",
"\n",
"i = 0\n",
"while True:\n",
" i += 1\n",
" if i > 10:\n",
" break\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe1a7cbd",
"metadata": {
"id": "fe1a7cbd"
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f11abcc",
"metadata": {
"id": "3f11abcc"
},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Slideshow",
"colab": {
"name": "jamcoders_day4_lec1_2.ipynb",
"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.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}