Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binarygalwalkin/f42868d72ce94e164d89f3f1a7b94177 to your computer and use it in GitHub Desktop.
Save binarygalwalkin/f42868d72ce94e164d89f3f1a7b94177 to your computer and use it in GitHub Desktop.
Conditions, branching, loops
Display the source blob
Display the rendered blob
Raw
{
"nbformat_minor": 1,
"cells": [
{
"execution_count": 1,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 1,
"metadata": {},
"data": {
"text/plain": "False"
},
"output_type": "execute_result"
}
],
"source": "# Use Equality sign to compare the strings\n\n\"ACDC\" == \"Michael Jackson\""
},
{
"execution_count": 2,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 2,
"metadata": {},
"data": {
"text/plain": "True"
},
"output_type": "execute_result"
}
],
"source": "# Use Inequality sign to compare the strings\n\n\"ACDC\" != \"Michael Jackson\""
},
{
"execution_count": 3,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 3,
"metadata": {},
"data": {
"text/plain": "True"
},
"output_type": "execute_result"
}
],
"source": "# Compare characters\n\n'BA' > 'AB'"
},
{
"execution_count": 4,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "you can enter\nmove on\n"
}
],
"source": "# If statement example\n\nage = 19\n#age = 18\n\n#expression that can be true or false\nif age > 18:\n \n #within an indent, we have the expression that is run if the condition is true\n print(\"you can enter\" )\n\n#The statements after the if statement will run regardless if the condition is true or false \nprint(\"move on\")"
},
{
"execution_count": 5,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "go see Meat Loaf\nmove on\n"
}
],
"source": "# Else statement example\n\nage = 18\n# age = 19\n\nif age > 18:\n print(\"you can enter\" )\nelse:\n print(\"go see Meat Loaf\" )\n \nprint(\"move on\")"
},
{
"execution_count": 6,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "go see Pink Floyd\nmove on\n"
}
],
"source": "# Elif statment example\n\nage = 18\n\nif age > 18:\n print(\"you can enter\" )\nelif age == 18:\n print(\"go see Pink Floyd\")\nelse:\n print(\"go see Meat Loaf\" )\n \nprint(\"move on\")"
},
{
"execution_count": 7,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "do something..\n"
}
],
"source": "# Condition statement example\n\nalbum_year = 1983\nalbum_year = 1970\n\nif album_year > 1980:\n print(\"Album year is greater than 1980\")\n \nprint('do something..')"
},
{
"execution_count": 8,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Album year was in between 1980 and 1989\n\nDo Stuff..\n"
}
],
"source": "# Condition statement example\n\nalbum_year = 1980\n\nif(album_year > 1979) and (album_year < 1990):\n print (\"Album year was in between 1980 and 1989\")\n \nprint(\"\")\nprint(\"Do Stuff..\")"
},
{
"execution_count": 9,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Album was not made in the 1980's\n"
}
],
"source": "# Condition statement example\n\nalbum_year = 1990\n\nif(album_year < 1980) or (album_year > 1989):\n print (\"Album was not made in the 1980's\")\nelse:\n print(\"The Album was made in the 1980's \")"
},
{
"execution_count": 10,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Album year is not 1984\n"
}
],
"source": "# Condition statement example\n\nalbum_year = 1983\n\nif not (album_year == '1984'):\n print (\"Album year is not 1984\")"
},
{
"execution_count": 12,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "This album is Amazing!\n"
}
],
"source": "# Write an if statement to determine if an album had a rating greater than 8. Test it using the rating for the album \u201cBack in Black\u201d that had a rating of 8.5.\n#If the statement is true print \"This album is Amazing!\"\n\nback_in_black = 8.5\nif (back_in_black > 8):\n print (\"This album is Amazing!\")"
},
{
"execution_count": 16,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "1979\n"
}
],
"source": "# Write an if statement to determine if an album came out before 1980 or in the years: 1991 or 1993. \n# If the condition is true print out the year the album came out.\n\nalbum_year = 1979\nif ((album_year<1980) or (album_year==1991) or (album_year==1993)):\n print (album_year)\nelse:\n print (\"different year\")\n "
},
{
"execution_count": 17,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "different year\n"
}
],
"source": "# Write an if statement to determine if an album came out before 1980 or in the years: 1991 or 1993. \n# If the condition is true print out the year the album came out.\n\nalbum_year = 1982\nif ((album_year<1980) or (album_year==1991) or (album_year==1993)):\n print (album_year)\nelse:\n print (\"different year\")"
},
{
"execution_count": 1,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "1982\n1980\n1973\n"
}
],
"source": "# For loop example\n\ndates = [1982,1980,1973]\nN = len(dates)\n\nfor i in range(N):\n print(dates[i]) "
},
{
"execution_count": 2,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "1982\n1980\n1973\n"
}
],
"source": "# Example of for loop, loop through list\n\nfor year in dates: \n print(year) "
},
{
"execution_count": 3,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Before square 0 is red\nAfter square 0 is weight\nBefore square 1 is yellow\nAfter square 1 is weight\nBefore square 2 is green\nAfter square 2 is weight\nBefore square 3 is purple\nAfter square 3 is weight\nBefore square 4 is blue\nAfter square 4 is weight\n"
}
],
"source": "# Use for loop to change the elements in list\n\nsquares = ['red', 'yellow', 'green', 'purple', 'blue']\n\nfor i in range(0, 5):\n print(\"Before square \", i, 'is', squares[i])\n squares[i] = 'weight'\n print(\"After square \", i, 'is', squares[i])"
},
{
"execution_count": 4,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "0 red\n1 yellow\n2 green\n3 purple\n4 blue\n"
}
],
"source": "# Loop through the list and iterate on both index and element value\n\nsquares=['red', 'yellow', 'green', 'purple', 'blue']\n\nfor i, square in enumerate(squares):\n print(i, square)"
},
{
"execution_count": 5,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "1982\n1980\n1973\nIt took 3 repetitions to get out of loop.\n"
}
],
"source": "# While Loop Example\n\ndates = [1982, 1980, 1973, 2000]\n\ni = 0\nyear = 0\n\nwhile(year != 1973):\n year = dates[i]\n i = i + 1\n print(year)\n\nprint(\"It took \", i ,\"repetitions to get out of loop.\")"
},
{
"execution_count": 8,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "-5\n-4\n-3\n-2\n-1\n0\n1\n2\n3\n4\n5\n"
}
],
"source": "#Write a for loop the prints out all the element between -5 and 5 using the range function.\n\nfor i in range(-5,6):\n print(i)"
},
{
"execution_count": 11,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "rock\nR&B\nSoundtrack\nR&B\nsoul\npop\n"
}
],
"source": "#Print the elements of the following list: Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop'] Make sure you follow Python conventions.\n\nGenres=['rock','R&B','Soundtrack','R&B','soul', 'pop']\nn = len(Genres)\nfor i in range (n):\n print(Genres[i])"
},
{
"execution_count": 12,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "rock\nR&B\nSoundtrack\nR&B\nsoul\npop\n"
}
],
"source": "#Print the elements of the following list: Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop'] Make sure you follow Python conventions.\n\nGenres=['rock','R&B','Soundtrack','R&B','soul', 'pop']\nfor genre in Genres:\n print(genre)"
},
{
"execution_count": 13,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "red\nyellow\ngreen\npurple\nblue\n"
}
],
"source": "#Write a for loop that prints out the following list: squares=['red', 'yellow', 'green', 'purple', 'blue']\n\nsquares=['red', 'yellow', 'green', 'purple', 'blue']\nfor colour in squares:\n print(colour)"
},
{
"execution_count": 5,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "10\n9.5\n10\n8\n7.5\n"
}
],
"source": "#Write a while loop to display the values of the Rating of an album playlist stored in the list PlayListRatings. \n#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]\n\nListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]\ni=1\nrating = ListRatings[0]\nwhile (rating>=6):\n print(rating)\n rating = ListRatings[i]\n i=i+1\n"
},
{
"execution_count": 1,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "orange\norange\n"
}
],
"source": "#Write a while loop to copy the strings 'orange' of the list squares to the list new_squares. \n#Stop and exit the loop if the value on the list is not 'orange':\n\nsquares = ['orange', 'orange', 'purple', 'blue ', 'orange']\nnewsquares = []\n\ni = 0\n\nwhile (squares[i]=='orange'):\n newsquares.append(squares[i])\n i=i+1\n \nfor colour in newsquares:\n print (colour)\n\n\n"
},
{
"execution_count": null,
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": ""
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.5",
"name": "python3",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"version": "3.5.5",
"name": "python",
"file_extension": ".py",
"pygments_lexer": "ipython3",
"codemirror_mode": {
"version": 3,
"name": "ipython"
}
}
},
"nbformat": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment