Skip to content

Instantly share code, notes, and snippets.

@imbingz
Created May 12, 2019 21:52
Show Gist options
  • Save imbingz/688edc26616b8b9b0db698148e69f875 to your computer and use it in GitHub Desktop.
Save imbingz/688edc26616b8b9b0db698148e69f875 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": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Error in atexit._run_exitfuncs:\n",
"Traceback (most recent call last):\n",
" File \"/home/jupyterlab/conda/lib/python3.6/threading.py\", line 551, in wait\n",
" signaled = self._cond.wait(timeout)\n",
" File \"/home/jupyterlab/conda/lib/python3.6/threading.py\", line 295, in wait\n",
" waiter.acquire()\n",
" File \"/home/jupyterlab/spark-2.3.1/python/pyspark/context.py\", line 245, in signal_handler\n",
" self.cancelAllJobs()\n",
" File \"/home/jupyterlab/spark-2.3.1/python/pyspark/context.py\", line 983, in cancelAllJobs\n",
" self._jsc.sc().cancelAllJobs()\n",
"AttributeError: 'NoneType' object has no attribute 'sc'\n",
"ERROR:root:Invalid alias: The name clear can't be aliased because it is another magic command.\n",
"ERROR:root:Invalid alias: The name more can't be aliased because it is another magic command.\n",
"ERROR:root:Invalid alias: The name less can't be aliased because it is another magic command.\n",
"ERROR:root:Invalid alias: The name man can't be aliased because it is another magic command.\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": "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": []
}
],
"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