Basic Python and Numpy Coding

Finish the following Python and Numpy coding following the instructions inside the file

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

{
“cells”: [
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“## Numpy Class Exercises\n”,
“The code is removed but the output is provided for you to check the correctness of your work. You can download 2 copies of the notebook to compare the result with the original copy”
]
},
{
“cell_type”: “code”,
“execution_count”: 1,
“metadata”: {},
“outputs”: [],
“source”: [
“import numpy as np”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Array Creation Important Attributes and Functions”
]
},
{
“cell_type”: “code”,
“execution_count”: 6,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[ 2.6 7.8 9.3 11.6]\n”
]
}
],
“source”: [
“# Create an 1-dimensional array with elements 2.6, 7.8, 9.3. 11.6 and print it\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 7,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[[1 2]\n”,
” [3 4]\n”,
” [5 6]]\n”
]
}
],
“source”: [
“# Create a 2-dimensional (3 X 2) array with any number you like. Pass the values in as a tuple of 3 tuples\n”,
“# Change the tuples to lists or a mixture of lists and tuples and recreate the numpy array\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 5,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[[ 1.3, 21. , 11. ],\n”,
” [72. , 4.4, 9. ]],\n”,
“\n”,
” [[15. , 62. , 9.9],\n”,
” [71. , 28. , 5. ]]])”
]
},
“execution_count”: 5,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Create a 3-dimensional array with nested lists, tuples or a combination of them.\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 8,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“(3, 2)\n”,
“2\n”,
“6\n”,
“(2, 2, 3)\n”
]
}
],
“source”: [
“# print out the shape, number of dimensions and size of the arrays you created before\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 7,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[0. 0.07142857 0.14285714 0.21428571 0.28571429 0.35714286\n”,
” 0.42857143 0.5 0.57142857 0.64285714 0.71428571 0.78571429\n”,
” 0.85714286 0.92857143 1. ]\n”
]
}
],
“source”: [
“# Create a one-dimensional array ‘a’ using linspace() function, \n”,
“# start with 0 and end with 1. Create 15 elements\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 8,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“0.42857142857142855\n”
]
}
],
“source”: [
“# Get the 7th element of ‘a’\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 11,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[[ 3 6 9 12 15 18 21 24 27]\n”,
” [30 33 36 39 42 45 48 51 54]]\n”
]
}
],
“source”: [
“# Create a one-dimensional array using arange() function starting at 3 and ending at 54. \n”,
“# The elements are the multiples of 3. Reshape it to a 3X6 array. Name the array ‘b’\n”,
“# When calling reshape(), can you passing -1 as an argument? Try it\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 12,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[30 33]\n”,
“[[12 15 18]\n”,
” [30 33 36]\n”,
” [48 51 54]]\n”,
“[ 3 6 9 12 15 18]\n”
]
}
],
“source”: [
“# Get number 30 and 33 from ‘b’\n”,
“\n”,
“# Get the last 3 columns from ‘b’\n”,
“\n”,
“# only one index is needed if the entire row is retrieved\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 12,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[10, 10, 10],\n”,
” [10, 10, 10],\n”,
” [10, 10, 10]])”
]
},
“execution_count”: 12,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Create a 3X3 array using full() function that takes 10 as the values for all elements\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 14,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[[ 0. 0. 0. 0.]\n”,
” [ 0. 22. 22. 0.]\n”,
” [ 0. 0. 0. 0.]\n”,
” [ 1. 1. 1. 33.]\n”,
” [ 1. 1. 1. 33.]\n”,
” [ 1. 1. 1. 33.]]\n”,
“[[ 0. 0. 0. 0. 1. 1. 1. 33.]\n”,
” [ 0. 22. 22. 0. 1. 1. 1. 33.]\n”,
” [ 0. 0. 0. 0. 1. 1. 1. 33.]]\n”
]
}
],
“source”: [
“# Create a zero array with dimension 3X4 in variable ‘arr0’. \n”,
“# Create another 3X4 array with all 1s in variable ‘arr1’\n”,
“\n”,
“# Set the 2 middle values of arr0 in row 2 to 22\n”,
“\n”,
“# Set the last column of arr1 to 33\n”,
“\n”,
“# Use vstack function to stack up the 2 arrays vertically into ‘arr3’\n”,
“\n”,
“# Use concatenate function to merge the 2 arrays horizontally into ‘arr4’\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Array Math and Stats”
]
},
{
“cell_type”: “code”,
“execution_count”: 19,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[[ 0.6 0.6 0.6 0.6]\n”,
” [ 0.6 22.6 22.6 0.6]\n”,
” [ 0.6 0.6 0.6 0.6]]\n”
]
}
],
“source”: [
“# Simple math operation. Add 0.6 to all elements of ‘arr0’\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 20,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[[ 1.6 1.6 1.6 33.6]\n”,
” [ 1.6 23.6 23.6 33.6]\n”,
” [ 1.6 1.6 1.6 33.6]]\n”
]
}
],
“source”: [
“# Add ‘arr0’ and ‘arr1’\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 17,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[1. , 1. , 1. , 5.74456265],\n”,
” [1. , 1. , 1. , 5.74456265],\n”,
” [1. , 1. , 1. , 5.74456265]])”
]
},
“execution_count”: 17,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the square root of ‘arr0’\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 22,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“51.20000000000001\n”,
“0.6\n”,
“22.6\n”,
“4.2666666666666675\n”
]
}
],
“source”: [
“# Get sum, min, max, mean of ‘arr0’\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 18,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[ 0. 22. 22. 0.]\n”,
“[0. 0. 0. 0.]\n”,
“[ 0. 22. 22. 0.]\n”,
“[0. 7.33333333 7.33333333 0. ]\n”
]
}
],
“source”: [
“# Get column sum, min, max, mean of ‘arr0’ \n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 19,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[ 0. 44. 0.]\n”,
“[0. 0. 0.]\n”,
“[ 0. 22. 0.]\n”,
“[ 0. 11. 0.]\n”
]
}
],
“source”: [
“# Get row sum, min, max, mean of ‘arr0’\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 60,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[ 1., 1., 1., 33.],\n”,
” [ 2., 2., 2., 66.],\n”,
” [ 3., 3., 3., 99.]])”
]
},
“execution_count”: 60,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get cumulative sum of ‘arr1’ along the columns\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Random Number Generation”
]
},
{
“cell_type”: “code”,
“execution_count”: 20,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[[51 74 77 22 90]\n”,
” [24 63 89 96 43]\n”,
” [24 23 86 43 58]]\n”
]
}
],
“source”: [
“# Create a 3X5 array with random integers between 1 and 100. Your result will be different\n”,
“# Assign it to a variable ‘rand_array’ to be used later\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 22,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[22, 51, 74, 77, 90],\n”,
” [24, 43, 63, 89, 96],\n”,
” [23, 24, 43, 58, 86]])”
]
},
“execution_count”: 22,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Sort ‘rand_array’ along the row. the result should be consistent with your array\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 25,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[22, 24, 43, 58, 86],\n”,
” [23, 43, 63, 77, 90],\n”,
” [24, 51, 74, 89, 96]])”
]
},
“execution_count”: 25,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Sort the random array along the column. the result should be according to your array\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 26,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[0.80223791, 0.310615 ],\n”,
” [0.3799499 , 0.20464882]])”
]
},
“execution_count”: 26,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Generate a random 2 by 2 array with elements between 0 and 1. Your elements will differ\n”,
“# from mine. Execute the code multiple times, ensure the values change with execution.\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 32,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([117.64052346, 104.00157208, 109.78737984, 122.40893199,\n”,
” 118.6755799 , 90.2272212 , 109.50088418, 98.48642792])”
]
},
“execution_count”: 32,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# set a random seed to be 0. Create an 1 X 8 array by sampling from a normal distrubtion\n”,
“# with mean 100 and standard deviation 10. Assign it to a variable ‘arr_normal’. \n”,
“# With the same seed value, your result will be the same as mine below.\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Array Indexing, Slicing and Setting”
]
},
{
“cell_type”: “code”,
“execution_count”: 33,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“109.78737984105739”
]
},
“execution_count”: 33,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# from the array ‘arr_normal’, get value at index 2\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 41,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[104.00157208 109.78737984]\n”,
“[104.00157208 109.78737984]\n”,
“[109.78737984 122.40893199 118.6755799 90.2272212 109.50088418\n”,
” 98.48642792]\n”,
“[104.00157208 109.78737984 122.40893199 118.6755799 90.2272212\n”,
” 109.50088418]\n”,
“[117.64052346 122.40893199 109.50088418]\n”,
“[ 98.48642792 109.50088418 90.2272212 118.6755799 122.40893199\n”,
” 109.78737984 104.00157208 117.64052346]\n”
]
}
],
“source”: [
“# User ‘arr_normal’:\n”,
“# Get the 2nd and 3rd items from the left and from the right\n”,
“\n”,
“\n”,
“# Get items from the 3rd to the end\n”,
“\n”,
“# Get items from the 2nd to the one before the end\n”,
“\n”,
“# Get every 3rd item starting from the first one\n”,
“\n”,
“# Get elements back in reverse order\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 42,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([117.64052346, 104.00157208, 109.78737984, 122.40893199,\n”,
” 118.6755799 , 100. , 100. , 98.48642792])”
]
},
“execution_count”: 42,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Set the values in 6th and 7th element in ‘arr_normal’ to be 100\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 45,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[188, 112, 158, 165, 139],\n”,
” [187, 146, 188, 181, 137],\n”,
” [125, 177, 172, 109, 120],\n”,
” [180, 169, 179, 147, 164]])”
]
},
“execution_count”: 45,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# generate an integer 4X5 array with random integers between 100 and 200\n”,
“# assign it to a variable ‘arr_int’.\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 46,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“[187 146 188 181 137]\n”,
“[[125 177 172 109 120]\n”,
” [180 169 179 147 164]]\n”
]
}
],
“source”: [
“# get the 2nd row. Again your result should be consistant with your array\n”,
“\n”,
“# get the 3rd and 4th rows\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 47,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“158\n”,
“[112 146 177 169]\n”,
“[[146 188 181]\n”,
” [177 172 109]]\n”
]
}
],
“source”: [
“# Your result should be consistant with your array\n”,
“# get the element in the first row and 3rd column\n”,
“\n”,
“# get the second column\n”,
“\n”,
“# get the middle 6 elements \n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Boolean Indexing and Fancy Indexing”
]
},
{
“cell_type”: “code”,
“execution_count”: 50,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([188, 158, 165, 187, 188, 181, 177, 172, 180, 169, 179, 164])”
]
},
“execution_count”: 50,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# from ‘arr_int’, get elements >= 150\n”,
“# Again your result should be consistant with your array\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 51,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([112, 109, 120])”
]
},
“execution_count”: 51,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# from ‘arr_int’, get elements between 100 and 120\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 53,
“metadata”: {},
“outputs”: [],
“source”: [
“# Create a 1X5 array named ‘label’\n”,
“label = np.array([‘a’, ‘b’, ‘c’, ‘d’, ‘e’])”
]
},
{
“cell_type”: “code”,
“execution_count”: 54,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[188, 139],\n”,
” [187, 137],\n”,
” [125, 120],\n”,
” [180, 164]])”
]
},
“execution_count”: 54,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# select values corresponding to labels ‘a’, or ‘e’ in ‘arr_int’\n”,
“# Again your result should be consistant with your array\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 55,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([100. , 104.00157208, 109.78737984, 109.78737984,\n”,
” 100. , 118.6755799 , 104.00157208])”
]
},
“execution_count”: 55,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Fancy indexing\n”,
“# use ‘arr_normal’ created above\n”,
“# Get values in index 6, 1, 2, 2, 5, 4, 1 in this order\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 56,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[187, 146, 188, 181, 137],\n”,
” [180, 169, 179, 147, 164],\n”,
” [125, 177, 172, 109, 120],\n”,
” [187, 146, 188, 181, 137],\n”,
” [125, 177, 172, 109, 120]])”
]
},
“execution_count”: 56,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# From ‘arr_int’, get the rows in this index order: 1, 3, 2, 1, 2\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 57,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[112, 165, 158],\n”,
” [146, 181, 188],\n”,
” [177, 109, 172],\n”,
” [169, 147, 179]])”
]
},
“execution_count”: 57,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# From ‘arr_int’, Get the 2nd, 4th and 3rd columns\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 65,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“array([[188, 1, 158, 165, 1],\n”,
” [187, 146, 188, 181, 1],\n”,
” [ 1, 177, 172, 1, 1],\n”,
” [180, 169, 179, 147, 164]])”
]
},
“execution_count”: 65,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# use numpy where to set the values in ‘arr_int’ to be 1 if less than 140\n”
]
}
],
“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.7.6”
}
},
“nbformat”: 4,
“nbformat_minor”: 4
}

{
“cells”: [
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“## Intro to Python – Exercise\n”,
“It is OK if you cannot complete all the exercises in class. You and your partner only needs to submit one copy of the code. As long as you complete 70% of the code successfully, you get full credit for this exercise. Use the Notebook accompanying the series of videos on Intro to Python and Intro to Python 2 as a reference. The examples will get you started quickly. I also have the desired output below each cell for you to double check your work for all cells except the first section \”Simple Python\”. The more your practice, the more proficient you will get. Have fun coding!!\n”,
“\n”,
“### Simple Python”
]
},
{
“cell_type”: “code”,
“execution_count”: 21,
“metadata”: {},
“outputs”: [],
“source”: [
“# Create a list of names: Amy, John, Mia, Kevin\n”,
“list1 = [\”Amy\”, \”John\”, \”Mia\”, \”Kevin\”]”
]
},
{
“cell_type”: “code”,
“execution_count”: 22,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“Kevin\n”
]
}
],
“source”: [
“# Print out the last element of names on the list using index. \n”,
“print(list1[-1])”
]
},
{
“cell_type”: “code”,
“execution_count”: 23,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“John\n”
]
}
],
“source”: [
“# Print out the second name using index. \n”,
“print(list1[1])”
]
},
{
“cell_type”: “code”,
“execution_count”: 24,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘Ella’, ‘John’, ‘Mia’, ‘Kevin’]”
]
},
“execution_count”: 24,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Replace the name Amy in the list with name Ella\n”,
“list1[0] = \”Ella\”\n”,
“list1”
]
},
{
“cell_type”: “code”,
“execution_count”: 25,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘Ella’, ‘John’, ‘Mia’, ‘Kevin’, ‘Emily’, ‘Kale’]”
]
},
“execution_count”: 25,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Add two more names to the end: Emily and Kale\n”,
“list1.append(\”Emily\”)\n”,
“list1.append(\”Kale\”)\n”,
“list1”
]
},
{
“cell_type”: “code”,
“execution_count”: 26,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘Ella’, ‘John’, ‘Mia’, ‘Emily’, ‘Kale’]”
]
},
“execution_count”: 26,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Remove Kevin\n”,
“list1.remove(\”Kevin\”)\n”,
“list1”
]
},
{
“cell_type”: “code”,
“execution_count”: 27,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“5”
]
},
“execution_count”: 27,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Print the length of the list \n”,
“len(list1)”
]
},
{
“cell_type”: “code”,
“execution_count”: 28,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“Emily is not the 3rd person\n”
]
}
],
“source”: [
“# Use an if statement to check whether the 3rd element is Emily. Print out “Emily is \n”,
“# the 3rd person” if it is true and “Emily is not the 3rd person” if this is not true. \n”,
“if list1[2] == \”Emily\”:\n”,
” print(\”Emily is the 3rd person\”)\n”,
“else:\n”,
” print(\”Emily is not the 3rd person\”)\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 29,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“Ella is in the list\n”,
“John is in the list\n”,
“Mia is in the list\n”,
“Emily is in the list\n”,
“Kale is in the list\n”
]
}
],
“source”: [
“# Use a for loop to print out contents in the names list. The printed result should \n”,
“# be like: “Ella is in the list”, “John is in the list”, and so on.\n”,
“for item in list1:\n”,
” print (item + \” is in the list\”)”
]
},
{
“cell_type”: “code”,
“execution_count”: 33,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[11, 13, 15, 17, 19, 21, 23, 25, 27, 29]”
]
},
“execution_count”: 33,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Create an empty list named numbers. Use a for loop to add odd numbers between 10 \n”,
“# and 30 to the list. Then print out the content of the list. (Use range function.)\n”,
“list2 = (list(range(10,30)))\n”,
“[ele for ele in list2 if ele%2 != 0]\n”,
” \n”,
“\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### String manipulation”
]
},
{
“cell_type”: “code”,
“execution_count”: 34,
“metadata”: {},
“outputs”: [],
“source”: [
“# This is a quote of Michael Jordan. Note the use of \”\\\” to escapt the single quote\n”,
“# and the use of it to wrap the line\n”,
“quote = ‘I\\’ve missed more than 9,000 shots in my career. I\\’ve lost almost 300 games. \\\n”,
” 26 times I\\’ve been trusted to take the game winning shot and missed. \\\n”,
” I\\’ve failed over and over and over again in my life and that is why I succeed.'”
]
},
{
“cell_type”: “code”,
“execution_count”: 35,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“239”
]
},
“execution_count”: 35,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the number of characters in the quote\n”,
“len(quote)”
]
},
{
“cell_type”: “code”,
“execution_count”: 36,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[\”I’ve missed more than 9,000 shots in my career\”,\n”,
” \” I’ve lost almost 300 games\”,\n”,
” \” 26 times I’ve been trusted to take the game winning shot and missed\”,\n”,
” \” I’ve failed over and over and over again in my life and that is why I succeed\”,\n”,
” ”]”
]
},
“execution_count”: 36,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Split the quote into a list of sentences\n”,
“quote_list = quote.split(‘.’)\n”,
“quote_list”
]
},
{
“cell_type”: “code”,
“execution_count”: 30,
“metadata”: {},
“outputs”: [],
“source”: [
“# Do you see extra white spaces in the quotes? Can you remove them? \n”,
“# Concatenate the sentences in the list back to new_quote (variable name)\n”,
“# Make sure the sentences are ended with a . and seperated by a space.\n”,
“# There should not be extra spaces in the last sentence.\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 31,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“223”
]
},
“execution_count”: 31,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Check the number of characters in the quote again. Do you see the difference?\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 24,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘I’ve’,\n”,
” ‘missed’,\n”,
” ‘more’,\n”,
” ‘than’,\n”,
” ‘9,000’,\n”,
” ‘shots’,\n”,
” ‘in’,\n”,
” ‘my’,\n”,
” ‘career.’,\n”,
” ‘I’ve’,\n”,
” ‘lost’,\n”,
” ‘almost’,\n”,
” ‘300’,\n”,
” ‘games.’,\n”,
” ’26’,\n”,
” ‘times’,\n”,
” ‘I’ve’,\n”,
” ‘been’,\n”,
” ‘trusted’,\n”,
” ‘to’,\n”,
” ‘take’,\n”,
” ‘the’,\n”,
” ‘game’,\n”,
” ‘winning’,\n”,
” ‘shot’,\n”,
” ‘and’,\n”,
” ‘missed.’,\n”,
” ‘I’ve’,\n”,
” ‘failed’,\n”,
” ‘over’,\n”,
” ‘and’,\n”,
” ‘over’,\n”,
” ‘and’,\n”,
” ‘over’,\n”,
” ‘again’,\n”,
” ‘in’,\n”,
” ‘my’,\n”,
” ‘life’,\n”,
” ‘and’,\n”,
” ‘that’,\n”,
” ‘is’,\n”,
” ‘why’,\n”,
” ‘I’,\n”,
” ‘succeed.’]”
]
},
“execution_count”: 24,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# split the original quote (stored in variable ‘quote’) into words\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 32,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘I’ve missed more than 9,000 shots in my career. I’ve lost almost 300 games. 26 times I’ve been trusted to take the game winning shot and missed. I’ve failed over and over and over again in my life and that is why I succeed.'”
]
},
“execution_count”: 32,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Another way to remove extra spaces is to call join() on space. you\n”,
“# should get the same result as in \”new_quote\”\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 35,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“4”
]
},
“execution_count”: 35,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# How many times the word \”I’ve\” have been used in the ‘new_quote’?\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 36,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“\”We’ve missed more than 9,000 shots in my career. We’ve lost almost 300 games. 26 times We’ve been trusted to take the game winning shot and missed. We’ve failed over and over and over again in my life and that is why We succeed.\””
]
},
“execution_count”: 36,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Replace all occurances of ‘I’ with ‘We’\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 39,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“300\n”,
“26\n”
]
}
],
“source”: [
“# Get all the numbers in the quote\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 40,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘ted to take'”
]
},
“execution_count”: 40,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the characters between 100 and 110 in the variable \”new_quote\”\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 42,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“150”
]
},
“execution_count”: 42,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# find the position of the word ‘fail’ in the variable \”new_quote\”\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 44,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“213”
]
},
“execution_count”: 44,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# find the position of the last occurance of the word \”I\” in the variable \”new_quote\”\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### List manipulation and list comprehension”
]
},
{
“cell_type”: “code”,
“execution_count”: 75,
“metadata”: {},
“outputs”: [],
“source”: [
“# Create 2 lists\n”,
“lst1 = [‘MBA’, ‘MSIS’, ‘MSA’, ‘3/2 MBA’]\n”,
“lst2 = [2, 1, 1.5, 1]”
]
},
{
“cell_type”: “code”,
“execution_count”: 76,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘MBA’, ‘MSIS’, ‘MSA’, ‘3/2 MBA’, ‘MSF’]”
]
},
“execution_count”: 76,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Add MSF to lst1 and 1 to lst2 using append()\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 77,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[2, 1, 1.5, 1, 1]”
]
},
“execution_count”: 77,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: []
},
{
“cell_type”: “code”,
“execution_count”: 78,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘MBA’, ‘MSIS’, ‘MSA’, ‘3/2 MBA’, ‘MSF’]”
]
},
“execution_count”: 78,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# pop() the 2 newly added elements from the lists and insert them again using insert()\n”,
“# use len of the list to get the index to insert the element\n”,
“\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 79,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[2, 1, 1.5, 1, 1]”
]
},
“execution_count”: 79,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: []
},
{
“cell_type”: “code”,
“execution_count”: 80,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘Undergrad’, ‘Online MBA’]”
]
},
“execution_count”: 80,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# create another list lst3 with \”Undergrad\” and \”Online MBA\”\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 81,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘MBA’, ‘MSIS’, ‘MSA’, ‘3/2 MBA’, ‘MSF’, ‘Undergrad’, ‘Online MBA’]”
]
},
“execution_count”: 81,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# use extend() method to add lst3 to the end of lst1\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 82,
“metadata”: {},
“outputs”: [],
“source”: [
“# create another list lst4 with 4 and 2\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 83,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[2, 1, 1.5, 1, 1, 4, 2]”
]
},
“execution_count”: 83,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# use + to add lst4 to the end of lst2\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 84,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘MSF’, ‘Undergrad’]”
]
},
“execution_count”: 84,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Slice lst1 to get MSF and Undergrad\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 85,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[1, 4, 2]”
]
},
“execution_count”: 85,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Slice for the last 3 elements of lst2\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 86,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“3”
]
},
“execution_count”: 86,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the index of ‘3/2 MBA’\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 87,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘3/2 MBA’, ‘MBA’, ‘MSA’, ‘MSF’, ‘MSIS’, ‘Online MBA’, ‘Undergrad’]”
]
},
“execution_count”: 87,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Sort lst1\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 88,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘Undergrad’, ‘Online MBA’, ‘MSIS’, ‘MSF’, ‘MSA’, ‘MBA’, ‘3/2 MBA’]”
]
},
“execution_count”: 88,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Reverse the order of lst1\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 90,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘UNDERGRAD’, ‘ONLINE MBA’, ‘MSIS’, ‘MSF’, ‘MSA’, ‘MBA’, ‘3/2 MBA’]”
]
},
“execution_count”: 90,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# use list comprehension to convert all elements in lst1 to uppercase\n”,
“# That is, the first letter should be capitalized\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 91,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘Undergrad’, ‘Online Mba’, ‘Msis’, ‘Msf’, ‘Msa’, ‘Mba’, ‘3/2 Mba’]”
]
},
“execution_count”: 91,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# use list comprehension to convert all elements to lst1 to proper case\n”,
“# That is, the first letter should be capitalizedb\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 92,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘Un’, ‘On’, ‘Ms’, ‘Ms’, ‘Ms’, ‘Mb’, ‘3/’]”
]
},
“execution_count”: 92,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# use list comprehension to get the first 3 letters of each element in lst1\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 93,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[True, False, True, True, True, True, False]”
]
},
“execution_count”: 93,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# use list comprehension to check if each element in lst1 contains \n”,
“# letters only. If so, return True, otherwise, False\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Sets and Dictionaries”
]
},
{
“cell_type”: “code”,
“execution_count”: 98,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Billed’,\n”,
” ‘Closed’,\n”,
” ‘Pending Approval’,\n”,
” ‘Pending Billing’,\n”,
” ‘Pending Fulfillment’}”
]
},
“execution_count”: 98,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Create a set of the sales order status named status: ‘Pending Approval’, \n”,
“# ‘Pending Fulfillment’, ‘Pending Billing’, ‘Billed’, ‘Closed’\n”,
“status = {‘Pending Approval’, ‘Pending Fulfillment’,\n”,
” ‘Pending Billing’, ‘Billed’, ‘Closed’}\n”,
“status”
]
},
{
“cell_type”: “code”,
“execution_count”: 99,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Billed’,\n”,
” ‘Closed’,\n”,
” ‘Partially Fulfilled’,\n”,
” ‘Pending Approval’,\n”,
” ‘Pending Billing’,\n”,
” ‘Pending Fulfillment’}”
]
},
“execution_count”: 99,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Add a status: ‘Partially Fulfilled’\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 100,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“6”
]
},
“execution_count”: 100,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# how many elements do you have in status?\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 101,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Billed’, ‘Cancelled’, ‘Pending Billing’}”
]
},
“execution_count”: 101,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# create another set with status named status2: ‘Pending Billing’, ‘Billed’ and ‘Cancelled’\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 103,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Billed’,\n”,
” ‘Cancelled’,\n”,
” ‘Closed’,\n”,
” ‘Partially Fulfilled’,\n”,
” ‘Pending Approval’,\n”,
” ‘Pending Billing’,\n”,
” ‘Pending Fulfillment’}”
]
},
“execution_count”: 103,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the union of the 2 sets\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 104,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Billed’, ‘Pending Billing’}”
]
},
“execution_count”: 104,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the intersection of the 2 sets\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 105,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Closed’, ‘Partially Fulfilled’, ‘Pending Approval’, ‘Pending Fulfillment’}”
]
},
“execution_count”: 105,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the difference of the 2 sets\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 106,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Cancelled’}”
]
},
“execution_count”: 106,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: []
},
{
“cell_type”: “code”,
“execution_count”: 107,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“False”
]
},
“execution_count”: 107,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Check if \”Pending\” is in status\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 110,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Make’: ‘Honda’, ‘Model’: ‘Accord’, ‘Color’: ‘Silver’, ‘Price’: 20000}”
]
},
“execution_count”: 110,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# create a dictionary for a car\n”,
“car = {‘Make’: ‘Honda’, ‘Model’: ‘Accord’, ‘Color’: ‘Silver’, ‘Price’: 20000}\n”,
“car”
]
},
{
“cell_type”: “code”,
“execution_count”: 111,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Make’: ‘Honda’,\n”,
” ‘Model’: ‘Accord’,\n”,
” ‘Color’: ‘Silver’,\n”,
” ‘Price’: 20000,\n”,
” ‘Year’: 2019}”
]
},
“execution_count”: 111,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Add Year = 2019 to the dictionary\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 112,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘Accord'”
]
},
“execution_count”: 112,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the model of car\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 113,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Make’: ‘Honda’,\n”,
” ‘Model’: ‘Accord’,\n”,
” ‘Color’: ‘Silver’,\n”,
” ‘Price’: 20000,\n”,
” ‘Year’: 2020}”
]
},
“execution_count”: 113,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Update the year to be 2020\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 115,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“dict_keys([‘Make’, ‘Model’, ‘Color’, ‘Price’, ‘Year’])”
]
},
“execution_count”: 115,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# get the keys of the car\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 116,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“dict_values([‘Honda’, ‘Accord’, ‘Silver’, 20000, 2020])”
]
},
“execution_count”: 116,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# get the values of the car\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 117,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“Make = Honda\n”,
“Model = Accord\n”,
“Color = Silver\n”,
“Price = 20000\n”,
“Year = 2020\n”
]
}
],
“source”: [
“# iterate through the items of the dictionary, print out the content\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 118,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Honda’: [‘Accord’, ‘Civic’, ‘CR-V’, ‘Fit’],\n”,
” ‘Toyota’: [‘Camery’, ‘Corolla’, ‘Prius’, ‘Avalon’],\n”,
” ‘Ford’: [‘Focus’, ‘Mustang’, ‘Ranger’, ‘Galaxy’]}”
]
},
“execution_count”: 118,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# create a dictionary of cars. Add 3 keys: Honda, Toyota, Ford\n”,
“# For each key, add a list of car models as value\n”,
“# Honda has Accord, Civic, CR-V, Fit\n”,
“# Toyota has Camery, Corolla, Prius, Avalon\n”,
“# Ford has Focus, Mustang, Ranger, Galaxy\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 119,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘CR-V'”
]
},
“execution_count”: 119,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the 3rd car of Honda\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 120,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“False”
]
},
“execution_count”: 120,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# check if GM is in the dictionary of cars\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 121,
“metadata”: {
“scrolled”: true
},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘Honda’: [‘Accord’, ‘Civic’, ‘CR-V’, ‘Fit’],\n”,
” ‘Toyota’: [‘Camery’, ‘Corolla’, ‘Prius’, ‘Avalon’],\n”,
” ‘Ford’: [‘Focus’, ‘Mustang’, ‘Ranger’, ‘Puma’]}”
]
},
“execution_count”: 121,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Set the last car for Ford to be Puma\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Datetime”
]
},
{
“cell_type”: “code”,
“execution_count”: 122,
“metadata”: {},
“outputs”: [],
“source”: [
“# import the needed classes\n”,
“from datetime import date, time, datetime, timedelta”
]
},
{
“cell_type”: “code”,
“execution_count”: 123,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“datetime.date(2020, 8, 8)”
]
},
“execution_count”: 123,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get today’s date. Your result will be different of course\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 124,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“datetime.datetime(2020, 8, 8, 9, 1, 4, 607450)”
]
},
“execution_count”: 124,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the current time and save it in a variable ‘current’\n”,
“# Your result will be different of course\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 126,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“5”
]
},
“execution_count”: 126,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get the weekday of ‘current’. In Python, Monday is 0\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 131,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“2020”
]
},
“execution_count”: 131,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Get year of the ‘current’ variable\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 132,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘Sat'”
]
},
“execution_count”: 132,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# practice using strftime(). Get the following from ‘current’\n”,
“# full and short names of the weekday, weekday as a number\n”,
“# day of month in number, Month name in short and full version, \n”,
“# month in number. use this url: \n”,
“# https://www.w3schools.com/python/python_datetime.asp\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 139,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“’08′”
]
},
“execution_count”: 139,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: []
},
{
“cell_type”: “code”,
“execution_count”: 143,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“datetime.datetime(2020, 12, 25, 0, 0)”
]
},
“execution_count”: 143,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Create a datetime object for the Christmas day. Save it in variable ‘xmas’\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 141,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘Friday'”
]
},
“execution_count”: 141,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# What weekday it is for the christmas day this year?\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 144,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“datetime.timedelta(days=138, seconds=53935, microseconds=392550)”
]
},
“execution_count”: 144,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# How long is it from now to the christmas day?\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 145,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“datetime.datetime(2020, 8, 20, 9, 1, 4, 607450)”
]
},
“execution_count”: 145,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# What date it is 12 days from now?\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 154,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“datetime.datetime(1969, 12, 28, 8, 20, 33)”
]
},
“execution_count”: 154,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# if you have a string in the following format, convert it to a datetime\n”,
“# https://www.programiz.com/python-programming/datetime/strptime\n”,
“# Change the year from 69 to 68 and see how Python interpret century\n”,
“dt_string = \”69|12|28-08_20_33\”\n”,
“\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {},
“source”: [
“### Functions”
]
},
{
“cell_type”: “code”,
“execution_count”: 155,
“metadata”: {},
“outputs”: [],
“source”: [
“# create two lists\n”,
“lst1 = [‘MBA’, ‘MSIS’, ‘MSA’, ‘3/2 MBA’, ‘Undergrad’, ‘PhD’]\n”,
“lst2 = [2, 1, 1.5, 1, 4, 5]”
]
},
{
“cell_type”: “code”,
“execution_count”: 156,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“((‘MBA’, 2),\n”,
” (‘MSIS’, 1),\n”,
” (‘MSA’, 1.5),\n”,
” (‘3/2 MBA’, 1),\n”,
” (‘Undergrad’, 4),\n”,
” (‘PhD’, 5))”
]
},
“execution_count”: 156,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# use the zip function to return the program names and time to complete them\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 163,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[315.0,\n”,
” 469.34999999999997,\n”,
” 853.9440000000001,\n”,
” 1158.8519999999999,\n”,
” 591.713,\n”,
” 946.693]”
]
},
“execution_count”: 163,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Convert the elements in growth rate to a list of decimal numbers. Use the old \n”,
“# sales data to get a list of new sales data. You can use float() to convert \n”,
“# the growth rate but need to strip out ‘%’ first. map() and anonymous function needed\n”,
“growth_rate = [‘5%’, ‘4.3%’, ‘9.2%’, ‘7.7%’, ‘5.1%’, ‘-2.1%’]\n”,
“sales_old = [300, 450, 782, 1076, 563, 967]\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 164,
“metadata”: {},
“outputs”: [],
“source”: [
“# Define a function named ‘get_revenue’ to calculate sales revenue. \n”,
“# There are 3 arguments: unit_sold, unit_price and discount. discount \n”,
“# has default value of 0. Calculate the revenue and return it\n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 169,
“metadata”: {},
“outputs”: [],
“source”: [
“# Call your function by passing in some different values. Make sure it\n”,
“# works in different scenarios. \n”,
“\n”
]
},
{
“cell_type”: “code”,
“execution_count”: 166,
“metadata”: {},
“outputs”: [],
“source”: [
“# The following lists contains product names, units sold and unit prices\n”,
“# Discount is also passed in as a list\n”,
“products = {‘Apple’: 1.99, ‘Lettuce’: 0.99, ‘Pear’: 2.59, ‘Peach’: 2.99}\n”,
“units_sold = [35, 50, 30, 55]\n”,
“discounts = [0.05, 0.1, 0.12, 0]”
]
},
{
“cell_type”: “code”,
“execution_count”: 168,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[66.1675, 44.550000000000004, 68.37599999999999, 164.45000000000002]”
]
},
“execution_count”: 168,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“# Calculate the sales revenue for each product using map() and the \n”,
“# function you created above: get_revenue()\n”
]
}
],
“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.7.3”
}
},
“nbformat”: 4,
“nbformat_minor”: 4
}

Calculate your order
Pages (275 words)
Standard price: $0.00
Client Reviews
4.9
Sitejabber
4.6
Trustpilot
4.8
Our Guarantees
100% Confidentiality
Information about customers is confidential and never disclosed to third parties.
Original Writing
We complete all papers from scratch. You can get a plagiarism report.
Timely Delivery
No missed deadlines – 97% of assignments are completed in time.
Money Back
If you're confident that a writer didn't follow your order details, ask for a refund.

Calculate the price of your order

You will get a personal manager and a discount.
We'll send you the first draft for approval by at
Total price:
$0.00
Power up Your Academic Success with the
Team of Professionals. We’ve Got Your Back.
Power up Your Study Success with Experts We’ve Got Your Back.

Order your essay today and save 30% with the discount code ESSAYHELP