Introduction
The 2 major types of loops in Python are while loops and for loops. We will discuss these and also use them to solve a simple economics problem.
For Loop in Python
Python has a very particular format for the for loop compared to other languages. Here is the syntax
for i in range(start, stop): // Do Stuff
simply put the variable i will be assigned to the value start, it will then execute all code in the indented section, it will then increment by 1, and execute the code again, continuing until it reaches the value of stop. There are other options for the range function – but this is the basic format. You can explore other options (like changing the increment) at https://www.w3schools.com/python/ref_func_range.asp
For loops are very useful when working with arrays and we will do an example of that later.
While Loops in Python
The format for a Python while loop is very traditional and similar to other languages.
while condition: // Do Stuff
condition is simply something that evaluates to true or false. It can be something like val1 > val2 or any conditional. The while loop also supports break and continue – both of which you should learn and I recommend https://www.w3schools.com/python/python_while_loops.asp
Solving Real Problems – Arrays and For Loops
One of the biggest uses of arrays in Python is calculating values with array input. For example suppose we wanted to know the present value of a piece of equipment given different resale values of 2000, 5000, and 10000 dollars at year 10.
So if we wrote a simple program we could try different values of C1 with an assumed n (interest rate) and r (interest)
Using this information the following code will have the output shown
C = [2000, 5000, 10000] n = 10 r = 0.05 import math for i in range(0,3): PV = C[i] / math.pow((1 + r), n) print("C: {} n: {} r: {} PV: {}".format(C[i], n, r, PV))
and the output
C: 2000 n: 10 r: 0.05 PV: 1227.8265070815182 C: 5000 n: 10 r: 0.05 PV: 3069.566267703796 C: 10000 n: 10 r: 0.05 PV: 6139.132535407592
This example is trivial, but consider the more complex set of iteration if we go through multiple options for the different input variables. Please note the len function which gives us the length of the array.
C = [2000, 5000, 10000] n = [5, 8, 10] r = [0.02, 0.05, 0.10] import math for i in range(0,len(C)): for j in range(0, len(n)): for k in range(0, len(r)): PV = C[i] / math.pow((1 + r[k]), n[j]) print("C: {} n: {} r: {} PV: {}".format(C[i], n[j], r[k], PV))
Which gives us a much more extensive output.
C: 2000 n: 5 r: 0.02 PV: 1811.4616196598317 C: 2000 n: 5 r: 0.05 PV: 1567.0523329369175 C: 2000 n: 5 r: 0.1 PV: 1241.8426461183099 C: 2000 n: 8 r: 0.02 PV: 1706.9807423802229 C: 2000 n: 8 r: 0.05 PV: 1353.678724057374 C: 2000 n: 8 r: 0.1 PV: 933.0147604194661 C: 2000 n: 10 r: 0.02 PV: 1640.6965997503103 C: 2000 n: 10 r: 0.05 PV: 1227.8265070815182 C: 2000 n: 10 r: 0.1 PV: 771.0865788590628 C: 5000 n: 5 r: 0.02 PV: 4528.65404914958 C: 5000 n: 5 r: 0.05 PV: 3917.630832342294 C: 5000 n: 5 r: 0.1 PV: 3104.6066152957746 C: 5000 n: 8 r: 0.02 PV: 4267.451855950557 C: 5000 n: 8 r: 0.05 PV: 3384.196810143435 C: 5000 n: 8 r: 0.1 PV: 2332.536901048665 C: 5000 n: 10 r: 0.02 PV: 4101.741499375776 C: 5000 n: 10 r: 0.05 PV: 3069.566267703796 C: 5000 n: 10 r: 0.1 PV: 1927.716447147657 C: 10000 n: 5 r: 0.02 PV: 9057.30809829916 C: 10000 n: 5 r: 0.05 PV: 7835.261664684588 C: 10000 n: 5 r: 0.1 PV: 6209.213230591549 C: 10000 n: 8 r: 0.02 PV: 8534.903711901114 C: 10000 n: 8 r: 0.05 PV: 6768.39362028687 C: 10000 n: 8 r: 0.1 PV: 4665.07380209733 C: 10000 n: 10 r: 0.02 PV: 8203.482998751551 C: 10000 n: 10 r: 0.05 PV: 6139.132535407592 C: 10000 n: 10 r: 0.1 PV: 3855.432894295314