Python and Algebra


In the previous post, you saw the very basics of Python. Now I am going to show you how you can integrate Algebra with Python. More specifically, sequences.

A quick review
A few things you are going to need how to do in Python in order to keep track with this post. First of all you are going to need to know how to create a loop. Writing a loop looks something like this:
for i in range(10):
print(i)
Here, i is the variable and we are trying to display a range of 10, with 10 being the upper boundary. This means that when you execute this script the range will start from 0 and go up to 9:
0
1
2
3
4
5
6
7
8
9
So, if you want to start from 1 and go to 10, you would write a script like:
for i in range(1,11):
print(i)

One more thing that you will need to learn how to do is 'sum', but I am going to hold off on teaching you that as I think it will be better explained when we go through the examples.

Example 1: Create a loop to represent the first 100 terms of the sequence Un=2n
Let's break down this question. We know that our sequence is Un=2*n, and that has a defined range of 1 to 100. In order to write a script for this, we are going to need to include a few things: a loop with a range 1 to 101 since 101 is the top boundary, multiplication, and print. Knowing all of this we can write a script that looks like:

for n in range(1,101):
    print(2*n)

Example 2: Calculate the first 100 terms of the sequence Un=n²-7
This one is very simple to do, especially when comparing it to the previous example. To make it different let's add a variable, because why not. Here, the variable is going to represent the number of terms we are trying to find which is 100.

A=100
For n in range(1,A+1):
     print(n**2 - 7)

The purpose of the variable A, at least what I was told, is just to be able to quickly change the number of terms. I see how it can be quicker but, then again I don't think it is necessary. Also if you noticed, in the loop it is written A+1, and that goes back to the same concept as mentioned previously that if you simply write 100, the script will only show you 1 to 99 and not to 100. Hence, you need to add 1 to make 101 the top boundary.

Example 3: Calculate the sum of the first 100 whole numbers
This example, introduced us to 'sum' in python. So, to start off we needed to tell the sum to begin at 0, hence sum=0. We then created another variable 'N' and a loop just like the previous example. What is different about this program is the way we use sum. Here, to make sure you are getting the sum of the first 100 whole numbers, we have to add the previous sum to the new sum, hence sum=sum+i. Finally, we obviously have a print, but one thing that you should know about print, is what the indentation can change. If you don't have an indentation like in my program, you'll only print the final sum. However, if the print is indented, it will print every sum it gets until it reaches the sum of 100 whole numbers.

sum=0
N=100

for i in range(1,N+1):
sum = sum+i

print(sum)

Example 4: U1 = 1/1+1/2    U2= 1/2+1/3+1/4   U3=1/3+1/4+1/5+1/6 etc. Calculate the sum of the first 1000 terms
So, this example had completely stumped the class. Either because A) We are all idiots (hey we are learning), B) We tried to create a script that was way too complicated, or C) This was a difficult question to give us on our first class ever. I'm just going to go with B and C, but I will also get into detail what I tried doing and why I originally failed.

So, where did I go wrong? For the most part, I tried to make it way more complicated than it needed to be which ultimately left me confused and stumped. I had way too many variables and loops for it to make any sense. Thankfully, my teacher was kind enough to save us from our self-inflicted confusion by giving us the program below.

N=1000
u=1/N

for i in range(1,N+1):
       u=u+1/(i+N)
print(u)



Thank you for reading this! If you have any input or feedback I would love to hear from you.

Comments

Popular Posts