Skip to content

Instantly share code, notes, and snippets.

@salamituns
Created February 7, 2021 14:39
Show Gist options
  • Save salamituns/086076777e6d26f500889655c504c7a5 to your computer and use it in GitHub Desktop.
Save salamituns/086076777e6d26f500889655c504c7a5 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Loops\n",
"Sometimes, you might want to repeat a given operation many times. Repeated executions like this are performed by loops. We will look at two types of loops, for 'loops' and 'while' loops."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The 'for' loop enables you to execute a code block multiple times.\n",
"It is used for a controlled flow of repetition."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1982\n",
"1980\n",
"1973\n"
]
}
],
"source": [
"#Let's try to use a 'for' loop to print all the years presented in the list dates:\n",
"\n",
"dates = [1982,1980,1973]\n",
"N = len(dates)\n",
"\n",
"for i in range(N):\n",
" print(dates[i])\n",
" \n",
"#The code in the indent is executed N times, \n",
"#each time the value of i is increased by 1 for every execution, \n",
"#The statement executed is to print out the value in the list at index i as shown here:"
]
},
{
"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"
]
}
],
"source": [
"#Example of for loop\n",
"#In this example we can print out a sequence of numbers from 0 to 7:\n",
"\n",
"for i in range(0, 8):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1982\n",
"1980\n",
"1973\n"
]
}
],
"source": [
"dates = [1982,1980,1973]\n",
"\n",
"for year in dates: \n",
" print(year) \n",
"\n",
"#For each iteration, the value of the variable years behaves like the value of dates[i] in the first example:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Before square 0 is red\n",
"After square 0 is white\n",
"Before square 1 is yellow\n",
"After square 1 is white\n",
"Before square 2 is green\n",
"After square 2 is white\n",
"Before square 3 is purple\n",
"After square 3 is white\n",
"Before square 4 is blue\n",
"After square 4 is white\n"
]
}
],
"source": [
"#Use 'for' loop to change the elements in list\n",
"\n",
"squares = ['red', 'yellow', 'green', 'purple', 'blue']\n",
"\n",
"for i in range(0, 5):\n",
" print(\"Before square \", i, 'is', squares[i])\n",
" squares[i] = 'white'\n",
" print(\"After square \", i, 'is', squares[i])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can access the index and the elements of a list as follows:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 red\n",
"1 yellow\n",
"2 green\n",
"3 purple\n",
"4 blue\n"
]
}
],
"source": [
"# Loop through the list and iterate on both index and element value\n",
"\n",
"squares=['red', 'yellow', 'green', 'purple', 'blue']\n",
"\n",
"for i, square in enumerate(squares):\n",
" print(i, square)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The 'while' loop exists as a tool for repeated execution based on a condition.\n",
"Use the 'while' loop if we want to keep executing a code block until a certain condition is met"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let’s say we would like to iterate through list dates and stop at the year 1973, \n",
"then print out the number of iterations. \n",
"\n",
"This can be done with the following block of code:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1982\n",
"1980\n",
"It took 2 repetitions to get out of loop.\n"
]
}
],
"source": [
"#While Loop Example\n",
"\n",
"dates = [1982, 1980, 1973, 2000]\n",
"\n",
"i = 0\n",
"year = dates[0]\n",
"\n",
"while(year != 1973): \n",
" print(year)\n",
" i = i + 1\n",
" year = dates[i]\n",
" \n",
"\n",
"print(\"It took \", i ,\"repetitions to get out of loop.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Quiz."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-5\n",
"-4\n",
"-3\n",
"-2\n",
"-1\n",
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n"
]
}
],
"source": [
"#Write a 'for' loop that prints out all the element between -5 and 5 using the range function.\n",
"\n",
"for i in range(-5, 6):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the elements of the following list: Genres=['rock','R&B','Soundtrack', 'R&B', 'soul', 'pop'] Make sure you follow Python conventions."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"rock\n",
"R&B\n",
"Soundtrack\n",
"R&B\n",
"soul\n",
"pop\n"
]
}
],
"source": [
"Genres=['rock','R&B','Soundtrack', 'R&B', 'soul', 'pop']\n",
"for Genre in Genres:\n",
" print(Genre)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a for loop that prints out the following list: squares=['red', 'yellow', 'green', 'purple', 'blue']"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"red\n",
"yellow\n",
"green\n",
"purple\n",
"blue\n"
]
}
],
"source": [
"squares=['red', 'yellow', 'green', 'purple', 'blue']\n",
"for color in squares:\n",
" print(color)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a while loop to display the values of the Rating of an album playlist stored in the list PlayListRatings. If the score is less than 6, exit the loop. The list PlayListRatings is given by: PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"9.5\n",
"10\n",
"8\n",
"7.5\n"
]
}
],
"source": [
"PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]\n",
"i = 1\n",
"Rating = PlayListRatings[0]\n",
"\n",
"while(i < len(PlayListRatings) and Rating >= 6):\n",
" print(Rating)\n",
" Rating = PlayListRatings[i]\n",
" i = i + 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a while loop to copy the strings 'orange' of the list squares to the list new_squares. Stop and exit the loop if the value on the list is not 'orange':"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['orange', 'orange']\n"
]
}
],
"source": [
"squares = ['orange', 'orange', 'purple', 'blue ', 'orange']\n",
"new_squares = []\n",
"\n",
"i = 0\n",
"while(i < len(squares) and squares[i] == 'orange'): #while i is within the square list and equal to orange\n",
" new_squares.append(squares[i]) #add new values to the new_\n",
" i = i + 1\n",
"print(new_squares)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment