Skip to content

Instantly share code, notes, and snippets.

@imbingz
Created May 13, 2019 14:45
Show Gist options
  • Save imbingz/f03d4da0676959a773cf409bcb8a14f6 to your computer and use it in GitHub Desktop.
Save imbingz/f03d4da0676959a773cf409bcb8a14f6 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"3 devided by 3 is 1.0\n",
"6\n",
"6 devided by 3 is 2.0\n",
"9\n",
"9 devided by 3 is 3.0\n"
]
}
],
"source": [
"for i in range(3, 10, 3):\n",
" print(i)\n",
" quotient = i / 3\n",
" print(f\"{i} devided by 3 is {float(quotient)}\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 divided by 2 is 5.0\n",
"11 divided by 2 is 5.5\n",
"12 divided by 2 is 6.0\n",
"13 divided by 2 is 6.5\n",
"14 divided by 2 is 7.0\n"
]
}
],
"source": [
"for i in range(10, 15):\n",
" quotient = i / 2\n",
" print(f\"{i} divided by 2 is {float(quotient)}\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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"
]
}
],
"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, 5):\n",
" print(i)"
]
},
{
"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": [
"# Print the elements of the following list. Make sure to follow Python conventions\n",
"\n",
"Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']\n",
"for genre in Genres:\n",
" print(genre)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"10\n",
"9.5\n",
"10\n",
"8\n",
"7.5\n"
]
}
],
"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: \n",
"\n",
"PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10] \n",
"\n",
"rating = PlayListRatings[0]\n",
"i = 0 \n",
"\n",
"while (rating >= 6):\n",
" print (rating)\n",
" rating = PlayListRatings[i]\n",
" i = i + 1\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['orange', 'orange']\n"
]
}
],
"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':\n",
"\n",
"squares = [\"orange\", \"orange\", \"purple\", \"blue\", \"orange\"]\n",
"\n",
"new_squares = []\n",
"i = 0\n",
"while (squares[i] == \"orange\"):\n",
" new_squares.append(squares[i])\n",
" i = i + 1\n",
"print(new_squares)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world\n"
]
}
],
"source": [
"def sayhi():\n",
" print(\"hello world\")\n",
"sayhi()\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello mike\n",
"hello bing\n"
]
}
],
"source": [
"def sayhi(name):\n",
" print(\"hello \" + name)\n",
"sayhi (\"mike\")\n",
"sayhi (\"bing\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello mike, you are 18\n",
"Hello bing, you are 19\n"
]
}
],
"source": [
"def sayhi (name, age):\n",
" print (\"hello \" + name + \", you are \" + age)\n",
"sayhi (\"mike\", \"18\")\n",
"\n",
"def sayhi (name, age):\n",
" print (\"Hello \" + name + \", you are \" + str(age))\n",
"sayhi (\"bing\", 19)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"27\n",
"125\n",
"64\n",
"8\n",
"27\n",
"64\n"
]
}
],
"source": [
"def cube(num):\n",
" return num*num*num\n",
"result = cube (4)\n",
"print(cube(3))\n",
"print(cube(5))\n",
"print (result)\n",
"\n",
"for n in range(2,5):\n",
" print (cube(n))\n",
" n= n + 1"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def add1(a):\n",
" b = a + 1\n",
" return b\n",
"add1(3)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter first number: 10\n",
"Enter second number: 2\n",
"Enter an operator: /\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.0\n"
]
}
],
"source": [
"# create a calculator\n",
"\n",
"num1 = float(input(\"Enter first number: \"))\n",
"num2 = float(input(\"Enter second number: \"))\n",
"op = input(\"Enter an operator: \")\n",
"\n",
"if op == \"+\": \n",
" print(num1+num2)\n",
"elif op == \"-\":\n",
" print(num1-num2)\n",
"elif op == \"*\":\n",
" print(num1*num2)\n",
"elif op == \"/\":\n",
" print(num1/num2)\n",
"else:\n",
" print(\"invalid operator\")\n",
" "
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"# use while loop to creat a word guess game\n",
"\n",
"secret_word = \"elephant\"\n",
"guess_word = \"\"\n",
"guess_count = 0\n",
"guess_limit = 3\n",
"out_of_guess = False\n",
"\n",
"while guess_word != secret_word and not out_of_guess:\n",
" if guess_count < guess_limit:\n",
" guess_word = input(\"Enter guess word\")\n",
" guess_count += 1\n",
" else:\n",
" out_of_guess = true\n",
" \n",
"if out_of_guess:\n",
" print(\"Out of guess, you lose!\")\n",
"else:\n",
" print(\"You win! Congratulations\")"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter guess word cat\n",
"Enter guess word eat\n",
"Enter guess word fish\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Out of guess, you lose!\n"
]
}
],
"source": [
"# use while loop to creat a word guess game\n",
"\n",
"secret_word = \"elephant\"\n",
"guess_word = \"\"\n",
"guess_count = 0\n",
"guess_limit = 3\n",
"out_of_guess = False\n",
"\n",
"while guess_word != secret_word and not (out_of_guess):\n",
" if guess_count < guess_limit:\n",
" guess_word = input(\"Enter guess word\")\n",
" guess_count += 1\n",
" else:\n",
" out_of_guess = True\n",
" \n",
"if out_of_guess:\n",
" print(\"Out of guess, you lose!\")\n",
"else:\n",
" print(\"You win! Congratulations\")\n",
" \n",
"# make usre the first letters of \"False\" \"True\" need to be capitalized in order to python to recognize a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Tr"
]
}
],
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment