Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
483 views

This worksheet gets away from the familiar math and draws from the world of programming. The power of the computer is to do repititive tasks without complaint. Also, data usually is dealt with in a list. Lists can contain anything your heart desires. In our case that will usually just be numbers or groups of numbers. I will create a list called L1 that has the numbers 1, 2, and 3 in it. I will also create a list called L2 that has the ordered pairs, (0,4)(0,4) and (5,1)(5,1). Then I will make a list called L3 that contains my first two lists.

L1=[1,2,3] L2=[(0,4),(5,1)] L3=[L1,L2] show(L3)
[[1\displaystyle 1, 2\displaystyle 2, 3\displaystyle 3], [(0\displaystyle 0, 4\displaystyle 4), (5\displaystyle 5, 1\displaystyle 1)]]

Notice the brackets when I made them and all of the brackets when I showed them. As soon as you hit the [ key, sage knows a list is coming. One thing we often will want to do with a list is to access a single element in it. Here's three examples. I will access the first, and second elements in L3. Then I will access the third element of L1.

L1=[1,2,3] L2=[(0,4),(5,1)] L3=[L1,L2] show(L3[0]) #0 corresponds to the first element show(L3[1]) #1 corresponds to the second element show(L1[2]) #2 corresponds to the third element
[1\displaystyle 1, 2\displaystyle 2, 3\displaystyle 3]
[(0\displaystyle 0, 4\displaystyle 4), (5\displaystyle 5, 1\displaystyle 1)]
3\displaystyle 3

I mentioned the idea of the computer performing repetitive tasks. Well, for that we use the "for loop". And most of the time we will want the results put immediately in a list. I will make two different lists using the for loop idea.

  1. the first list will be the first 5 perfect squares, 1, 4, 9, 16 and 25. Recall that they are the results of squaring 1, 2, 3, 4, and 5.

  2. the second list will be 5 square roots of 5 random numbers that popped into my head. These five numbers I will put into a list called 'numbers'.

squares=[n^2 for n in (1..5)] #please note the syntax!!!!!!!!! show(squares) numbers=[5,16,36,3,49] roots=[sqrt(i) for i in numbers] #notice that i represents each of the different nos in numbers. show(roots)
[1\displaystyle 1, 4\displaystyle 4, 9\displaystyle 9, 16\displaystyle 16, 25\displaystyle 25]
[5\displaystyle \sqrt{5}, 4\displaystyle 4, 6\displaystyle 6, 3\displaystyle \sqrt{3}, 7\displaystyle 7]

That's enough to get started. There's lots more that can be done!