{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ " # [EEP 147]: Introduction to Programming and the ESG" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
from (package) import (stuff)
, where the \"stuff\" we're importing can range from a specific function in that package to a whole library of functions, as is the case when we type import (package) as (name)
."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from datascience import *\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"import numpy as np\n",
"import pandas as pd\n",
"plt.style.use('fivethirtyeight')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Section 1: Math in Python \n",
"\n",
"Python is the programming language that we will use in this lab. Although this lab will go over some basics, should you be more interested in learning Python feel free to check out the following resources:\n",
"\n",
"* **[Python Tutorial](https://docs.python.org/3.5/tutorial/)**: Introduction to Python from the creators of Python\n",
"* **[Composing Programs](http://composingprograms.com/pages/11-getting-started.html)**: An introduction to programming with Python from CS 61A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Mathematical Expressions**\n",
"In Python, we can carry out all the mathematical processes you know and love:\n",
"\n",
"* Add using `+`\n",
"* Subtract using `-`\n",
"* Multiply using `*`\n",
"* Divide using `/`\n",
"* Exponentiate using `**`\n",
"* Floor divide using `//`\n",
"* Take the remainder / modulo using `%`\n",
"\n",
"The most of these you should be familiar with, but let's go over some of the more obscure processes while beginning to implement some python code!\n",
"\n",
"To run the code in the following cells, press Shift + Enter/Return!"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# So what exactly does floor divide do?\n",
"10 // 3"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# What about modulo?\n",
"10 % 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Very cool! Now we'll let you try, and notice that we can use parentheses to organize our order of operations.\n",
"\n",
"**Exercise**: Take the product of *three* and *three to the power of six* and subtract 168."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Insert your code where the dots are:\n",
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Awesome job! Feel free to add more cells using the + button in the upper left hand corner of the lab and play around with more mathematical expressions later! In the meantime, let's move on to the next section."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Section 2: Variables\n",
"\n",
"As you might recall, a name that is used to denote a value is called a variable. In python, variables can be declared and values can be assigned. Here are a few examples of variables and their assignment:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 2\n",
"m = 3\n",
"b = 4\n",
"y = m*x + b\n",
"# Look familiar? Press shift + enter to see the value!\n",
"y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Output and Printing**\n",
"\n",
"As you might have noticed at the end, there is a difference between returning and printing:\n",
"\n",
"* **Return**: A value that is not necessarily printed, but is stored away inside a computer if we assign or bind it to a name.\n",
"* **Printing**: A value that pops up on our screen.\n",
"We print using a **print** function and return a value using the **return** function.\n",
"\n",
"**Functions?**\n",
"\n",
"You might recall that a function receives input and correspondingly will output something. In Python, we have numerous functions, such as:\n",
"\n",
"* `print`: The command `print('hi')` will print 'hi' out to the screen.\n",
"* `sum`: The command `sum(2,3,4)` will sum up the values enclosed in the parentheses and return the value.\n",
"* And more!\n",
"\n",
"The best thing about functions is that, in Python, we can make our own functions! We will discuss this more in depth later, but for now just remember that to call a function, we write the name of the function, like `print()` and we place our arguments inside the parentheses. \n",
"\n",
"Let's try it for ourselves!\n",
"\n",
"**Exercise**: Try printing out the phrase 'Hello World!'"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Ellipsis"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Section 3: Functions and Loops\n",
"\n",
"A function is a block of organized, reusable code that is used to perform a single, related action. Take for example a factorial, denoted `x!`, which takes the initial value `x` and multiplies it by `x-1` and `x-2` and so on and so forth until it gets to 1! Typing this all out would look something like this:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"120"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let's pick a random value for x:\n",
"x = 5\n",
"factorial = 5 * 4 * 3 * 2 * 1\n",
"factorial"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This might not seem too troublesome now, but imagine doing this by hand for a larger number like 123! Instead, let's consider writing a function that can take in any value (such as 123) and output the factorial!\n",
"\n",
"**Function Structure**\n",
"\n",
"So how can we begin writing a function? Well there is a very simple structure to them:\n",
"\n",
"**def** function_name(arguments):\n",
" [function procedures]\n",
" **return** [output]
\n",
" \n",
"There are some aspects of a function that are required no matter what kind of function you are writing. You will always begin writing a function by writing **def**, followed by the name of your function. Following the name of your function, you will want to specify your inputs by using parentheses and giving your inputs names. These names can be anything you'd like, but generally you'd like them to be memorable and symbolic of what you're trying to do.\n",
"\n",
"Before typing in your functions procedure in the **body** of your function, you'll want to end the first line with a `:`. Then you're ready to proceed to the body and second line of your function! You will want to indent (press tab or space 4 times) and write what you'd like your function to do.\n",
"\n",
"Lastly, you'll want to end the function by writing what you'd like your function to **return**.\n",
"\n",
"**Example**: Let's look at what a factorial function would look like!"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"120"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def factorial_func(x):\n",
" product = 1\n",
" while x > 0:\n",
" product = product * x\n",
" x = x - 1\n",
" return product\n",
"\n",
"# Now let's test out our new factorial function!\n",
"factorial_func(5)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Try calculating the factorial value for the big number from before:\n",
"factorial_func(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Amazing! However, you might have noticed there were some new features used, which brings us to our next small topic.\n",
"\n",
"**Loops**\n",
"\n",
"Something that came in handy for this equation was a loop. A loop is a piece of code that repeats a block of code **while** a condition is true or **for** a certain number of times. Like we just not-so-subtly hinted, there are two very important kinds of loops: for loops and while loops. In the case of our function above, the code under the while loop was repeated **while** `x > 0`. On the other hand, a for loop will continue looping **for** a specified number of times."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Section 4: Data Structures\n",
"\n",
"So now that we know how to calculate things and create functions to do so, how can we organize large amounts of information?\n",
"\n",
"The solution to our problem is a data structure! A data structures is simply a means by which to contain and organize our data or information. They include:\n",
"* **List**: A list holds an ordered collection of items similar to a grocery list.\n",
"* **Dictionary**: Like an addressbook in which keys are associated with values (similar to names and phone numbers in addressbooks).\n",
"* **Set**: An unordered collection of items, and they operate similar to how Venn Diagrams do.\n",
"\n",
"Here is how we can use lists:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Helen', 'Nadeem', 'Alma', 'Nika']"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Creating a list using brackets and commas in between:\n",
"names = ['Helen', 'Nadeem', 'Alma', 'Nika']\n",
"names"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Helen'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The first name in our list, located at position 0:\n",
"names[0]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Helen', 'Nadeem', 'Alma', 'Nika', 'Sam']"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Adding a name, feel free to change the name to yours!\n",
"names.append('Sam')\n",
"names"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise**: Now you try creating a list with the names of some of your friends or pets!"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# Create your list below:\n",
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As opposed to a list (or array), a dictionary contains **keys** and **values** that, when defined, are separated by a colon. Take a look at the example below:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# Creating a dictionary\n",
"dictionary = {'Helen': 'Math', 'Nadeem': 'Physics', 'Alma': 'Data Science', 'Nika': 'MCB'}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To call a certain value in a dictionary, we use the corresponding key:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Math'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dictionary['Helen']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calling an index won't work here! Feel free to play around with the dictionary or create your own. One thing to note is that in a given dictionary, the keys must be unique, but values do not have to be."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to the data structures listed above, we can also organize our information in a table. Similar to Google Sheets or Microsoft Excel, we will be organzing our data into nice-looking tables."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Section 5: Tables\n",
"In this section, we'll cover some basic table functions. In order to begin filtering through information stored in a table, we'll have to \"read in\" the information. Most of the time, information to be displayed as a table is stored as a `.csv` file which stands for **comma separated values**.\n",
"\n",
"To read in a file, we use the following command:\n",
"\n",
"`Table.read_table('file_name.csv')`\n",
"\n",
"and in order to store it, we'll assign it a name or label. We'll begin by reading in the file that you'll be using for the remainder of the lab:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"# Just run this code block!\n",
"ESG_table = Table.read_table('ESGPorfolios_forcsv.csv')"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"Group | Group_num | UNIT NAME | Capacity_MW | Heat_Rate_MMBTUperMWh | Fuel_Price_USDperMMBTU | Fuel_Cost_USDperMWH | Var_OandM_USDperMWH | Total_Var_Cost_USDperMWH | Carbon_tonsperMWH | FixedCst_OandM_perDay | Plant_ID | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|
Big Coal | 1 | FOUR CORNERS | 1900 | 11.67 | 3 | 35 | 1.5 | 36.5 | 1.1 | $8,000 | 11 | \n", "
Big Coal | 1 | ALAMITOS 7 | 250 | 16.05 | 4.5 | 72.22 | 1.5 | 73.72 | 0.85 | $0 | 12 | \n", "
Big Coal | 1 | HUNTINGTON BEACH 1&2 | 300 | 8.67 | 4.5 | 39 | 1.5 | 40.5 | 0.46 | $2,000 | 13 | \n", "
Big Coal | 1 | HUNTINGTON BEACH 5 | 150 | 14.44 | 4.5 | 65 | 1.5 | 66.5 | 0.77 | $2,000 | 14 | \n", "
Big Coal | 1 | REDONDO 5&6 | 350 | 8.99 | 4.5 | 40.44 | 1.5 | 41.94 | 0.48 | $3,000 | 15 | \n", "
... (37 rows omitted)
" ], "text/plain": [ "Group | Group_num | UNIT NAME | Capacity_MW | Heat_Rate_MMBTUperMWh | Fuel_Price_USDperMMBTU | Fuel_Cost_USDperMWH | Var_OandM_USDperMWH | Total_Var_Cost_USDperMWH | Carbon_tonsperMWH | FixedCst_OandM_perDay | Plant_ID | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|
Old Timers | 7 | BIG CREEK | 1000 | nan | 0 | 0 | 0 | 0 | 0 | $15,000 | 61 | \n", "
Fossil Light | 8 | HELMS | 800 | nan | 0 | 0 | 0.5 | 0.5 | 0 | $15,000 | 72 | \n", "
Fossil Light | 8 | DIABLO CANYON 1 | 1000 | 1 | 7.5 | 7.5 | 4 | 11.5 | 0 | $20,000 | 75 | \n", "
Bay Views | 4 | MOSS LANDING 6 | 750 | 6.9 | 4.5 | 31.06 | 1.5 | 32.56 | 0.37 | $8,000 | 33 | \n", "
Bay Views | 4 | MOSS LANDING 7 | 750 | 6.9 | 4.5 | 31.06 | 1.5 | 32.56 | 0.37 | $8,000 | 34 | \n", "
... (37 rows omitted)
" ], "text/plain": [ "Group | Group_num | UNIT NAME | Capacity_MW | Heat_Rate_MMBTUperMWh | Fuel_Price_USDperMMBTU | Fuel_Cost_USDperMWH | Var_OandM_USDperMWH | Total_Var_Cost_USDperMWH | Carbon_tonsperMWH | FixedCst_OandM_perDay | Plant_ID | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|
Big Gas | 2 | KEARNY | 200 | 19.9 | 4.5 | 89.56 | 0.5 | 90.06 | 1.06 | $0 | 26 | \n", "
Fossil Light | 8 | HUNTERS POINT 4 | 250 | 16.53 | 4.5 | 74.39 | 1.5 | 75.89 | 0.88 | $1,000 | 74 | \n", "
Beachfront | 5 | ELLWOOD | 300 | 16.69 | 4.5 | 75.11 | 0.5 | 75.61 | 0.89 | $0 | 44 | \n", "
Big Coal | 1 | ALAMITOS 7 | 250 | 16.05 | 4.5 | 72.22 | 1.5 | 73.72 | 0.85 | $0 | 12 | \n", "
East Bay | 6 | POTRERO HILL | 150 | 15.41 | 4.5 | 69.33 | 0.5 | 69.83 | 0.82 | $0 | 56 | \n", "
Big Coal | 1 | HUNTINGTON BEACH 5 | 150 | 14.44 | 4.5 | 65 | 1.5 | 66.5 | 0.77 | $2,000 | 14 | \n", "
Big Gas | 2 | NORTH ISLAND | 150 | 14.44 | 4.5 | 65 | 0.5 | 65.5 | 0.77 | $0 | 24 | \n", "
Beachfront | 5 | ETIWANDA 5 | 150 | 13.64 | 4.5 | 61.39 | 1.5 | 62.89 | 0.72 | $1,000 | 43 | \n", "
Bay Views | 4 | OAKLAND | 150 | 13.48 | 4.5 | 60.67 | 0.5 | 61.17 | 0.72 | $0 | 35 | \n", "
East Bay | 6 | PITTSBURGH 7 | 700 | 13.16 | 4.5 | 59.22 | 0.5 | 59.72 | 0.7 | $4,000 | 53 | \n", "
... (32 rows omitted)
" ], "text/plain": [ "Group | Group_num | UNIT NAME | Capacity_MW | Heat_Rate_MMBTUperMWh | Fuel_Price_USDperMMBTU | Fuel_Cost_USDperMWH | Var_OandM_USDperMWH | Total_Var_Cost_USDperMWH | Carbon_tonsperMWH | FixedCst_OandM_perDay | Plant_ID\n", "Big Gas | 2 | KEARNY | 200 | 19.9 | 4.5 | 89.56 | 0.5 | 90.06 | 1.06 | $0 | 26\n", "Fossil Light | 8 | HUNTERS POINT 4 | 250 | 16.53 | 4.5 | 74.39 | 1.5 | 75.89 | 0.88 | $1,000 | 74\n", "Beachfront | 5 | ELLWOOD | 300 | 16.69 | 4.5 | 75.11 | 0.5 | 75.61 | 0.89 | $0 | 44\n", "Big Coal | 1 | ALAMITOS 7 | 250 | 16.05 | 4.5 | 72.22 | 1.5 | 73.72 | 0.85 | $0 | 12\n", "East Bay | 6 | POTRERO HILL | 150 | 15.41 | 4.5 | 69.33 | 0.5 | 69.83 | 0.82 | $0 | 56\n", "Big Coal | 1 | HUNTINGTON BEACH 5 | 150 | 14.44 | 4.5 | 65 | 1.5 | 66.5 | 0.77 | $2,000 | 14\n", "Big Gas | 2 | NORTH ISLAND | 150 | 14.44 | 4.5 | 65 | 0.5 | 65.5 | 0.77 | $0 | 24\n", "Beachfront | 5 | ETIWANDA 5 | 150 | 13.64 | 4.5 | 61.39 | 1.5 | 62.89 | 0.72 | $1,000 | 43\n", "Bay Views | 4 | OAKLAND | 150 | 13.48 | 4.5 | 60.67 | 0.5 | 61.17 | 0.72 | $0 | 35\n", "East Bay | 6 | PITTSBURGH 7 | 700 | 13.16 | 4.5 | 59.22 | 0.5 | 59.72 | 0.7 | $4,000 | 53\n", "... (32 rows omitted)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Replace the ellipsis below with the correct command:\n", "ESG_table.sort(\"Total_Var_Cost_USDperMWH\", ... )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a wide variety of table methods, but here are the highlights, followed with examples:\n", "\n", "* `table.where(column, value_you_want)`, where *column* is the column you'd like to select from and *value_you_want* is the item you're searching for. The output will be a table that only contains elements that are the value you want for the column you specified.\n", "* `table.column(column)`, where *column* is again the column you'd like to select. However, this method returns the entire column as an **array** of the items in that column!\n", "\n", "Note that when specifying a column, you can use either the string label or the index of the column. And don't forget that in python, we begin counting (or indexing) at 0. Below are some examples:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "Group | Group_num | UNIT NAME | Capacity_MW | Heat_Rate_MMBTUperMWh | Fuel_Price_USDperMMBTU | Fuel_Cost_USDperMWH | Var_OandM_USDperMWH | Total_Var_Cost_USDperMWH | Carbon_tonsperMWH | FixedCst_OandM_perDay | Plant_ID | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|
Big Coal | 1 | FOUR CORNERS | 1900 | 11.67 | 3 | 35 | 1.5 | 36.5 | 1.1 | $8,000 | 11 | \n", "
Big Coal | 1 | HUNTINGTON BEACH 1&2 | 300 | 8.67 | 4.5 | 39 | 1.5 | 40.5 | 0.46 | $2,000 | 13 | \n", "
Big Coal | 1 | REDONDO 5&6 | 350 | 8.99 | 4.5 | 40.44 | 1.5 | 41.94 | 0.48 | $3,000 | 15 | \n", "
Big Coal | 1 | REDONDO 7&8 | 950 | 8.99 | 4.5 | 40.44 | 1.5 | 41.94 | 0.48 | $5,000 | 16 | \n", "
Big Coal | 1 | HUNTINGTON BEACH 5 | 150 | 14.44 | 4.5 | 65 | 1.5 | 66.5 | 0.77 | $2,000 | 14 | \n", "
Big Coal | 1 | ALAMITOS 7 | 250 | 16.05 | 4.5 | 72.22 | 1.5 | 73.72 | 0.85 | $0 | 12 | \n", "