-
Introduction 4
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
Lecture1.4
-
-
Production Possibilities Frontier 4
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
-
Trade 3
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
-
Demand 4
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
Lecture4.4
-
-
Supply 2
-
Lecture5.1
-
Lecture5.2
-
-
Equilibrium 4
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
-
-
Curve Movements 4
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
Lecture7.4
-
-
Elasticity and Revenue 5
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
Lecture8.4
-
Lecture8.5
-
-
Taxes 7
-
Lecture9.1
-
Lecture9.2
-
Lecture9.3
-
Lecture9.4
-
Lecture9.5
-
Lecture9.6
-
Lecture9.7
-
-
Consumer and Producer Surplus 8
-
Lecture10.1
-
Lecture10.2
-
Lecture10.3
-
Lecture10.4
-
Lecture10.5
-
Lecture10.6
-
Lecture10.7
-
Lecture10.8
-
-
Imports and Exports 4
-
Lecture11.1
-
Lecture11.2
-
Lecture11.3
-
Lecture11.4
-
-
Tariffs 2
-
Lecture12.1
-
Lecture12.2
-
Production Possibilities Frontier
Solution
for x in range(0,11):
print(x*2)
print((10-x)*4)
print("")
The first line of our solution we already covered. The next two lines prints out the number of hamburgers and then the number of salads. Finally we have print(“”) to print a new line and split up the combinations.
Now we need to cover something else new in python, arrays. What arrays do is they hold a collection of items. For example, the array [4,6,8] holds the numbers 4, 6 and 8. The reason we will use an array is to hold the previous results we had.
Let’s start with how you would initialize an empty array. Hamburgers = [] would make an array named Hamburgers with nothing in it. We will do this for salads as well. What we are going to do is loop through all the combinations and see what we make of each for every x value. Another important aspect of arrays is that they preserve their order (unless you change it). So the first element will be the first element you add.
The way we add elements to an array is through append. Taking our array, we would do Hamburgers.append(x) and we would append x. Append is a function that arrays have which adds an element to the end of the array. Now let’s create two arrays. One will cover what hamburgers we can make for x=0 through 10, and the other will cover what salads we can make for the same range.
Hamburgers = []
Salads = []
for x in range(0,11):
Hamburgers.append(x*2)
Salads.append((10-x)*4)
print(Hamburgers)
print(Salads)
Let’s walk through the code. The first two lines makes our arrays. Next we have our range function which we have covered already a few times; we iterate over the numbers 0 to 10. Now for the code block in the loop we are appending one element to our arrays each loop. For hamburgers, we make 2 hamburgers for every hour of work dedicated to it, thus we append x*2. For salads we make 4, and we get to use the leftover hours (10-x). The last two lines print out our arrays so that you can observe what they look like. The arrays will be:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[40, 36, 32, 28, 24, 20, 16, 12, 8, 4, 0]
As you can see, there is an inverse relationship between the arrays, as one goes up, the other goes down.
Now these arrays would be much more interesting if we could graph them. Python on its own doesn’t have the solution we want for this, so we are going to first learn about importing. Part of the strength of computer programming is that other people can create libraries of code and share them for others to use. By importing them we can extend python functionality. To import we could write “import library”. Now our library is called matplotlib.pyplot, so we could write “import matplotlib.pyplot”. This is totally valid, but one thing we likely want to do is give a shorter nickname to our library so we can reference it and not write our matplotlib.pyplot every time we want to use it. We write “import matplotlib.pyplot as plt”. matplotlib.pyplot is the library, and plt is the nickname we give it. Now check out the below code.
import matplotlib.pyplot as plt
plt.plot(Hamburgers,Salads)
plt.show()
The first line imports the library like we just covered. The next line calls the plot function from our library. The first variable you put is the x variable values, and the second is the y variables. Finally plt.show() shows the graph we have. If you did this correctly the inverse relationship between the hamburgers and salads should be obvious.
Let’s put on some finishing touches to our code with labels, the below code adds an x label and a y label to our graph. You’ll notice once you have imported your library you don’t need to do it again in your file.
plt.plot(Hamburgers,Salads)
plt.xlabel("Hamburgers")
plt.ylabel("Salads")
plt.show()
Challenge