-
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
-
Introducing Sympy
To represent demand and supply we are going to introduce a library called sympy.
While certain libraries we can right away import, there are others that do not come with our program. Sympy is one of those. To download you need to to type this into your notebook once, and it will download the library.
!pip install sympy
Now that we have the library installed, let’s start working with it.
import sympy
x = sympy.Symbol("x")
Our first line imports sympy. Our second line creates a symbol x. What this means is that any time we type x we are referencing the symbol x. Before we might have x=10, and then printed out x*2 and gotten 20. Now, if we type x*2 in we get an equation returned.
equation = x*2+1
print(equation)
The code above creates an equation, and then prints it out. What if we wanted to plug something into our equation?
print(equation.subs(x,5))
What the above code does is takes a symbol (we gave it x) and converts it to a value (we gave it 5). When we print it out we get 11 in return because 2*5+1 = 11.
We can also add equations together!
equation1 = x*2+1
equation2 = x-1
print(equation1 + equation2)
And finally, if we wanted to find where these two equations were equal to one another, we could subtract them and then use sympy’s solving function to find where they cross as so:
print(sympy.solve(equation1-equation2))
This function returns an array of the roots of whatever equation you solved for. In this case we see that the two lines are equal to one and other when x = -2.
Challenge