Basic Python and Numpy Coding

profilenieyanan
PythonExercise.ipynb

{ "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 }