2023.01.23 Math 3600 Arrays and lists

245 days ago by calkin

var('cheese') 
       
cheese
cheese
L=[3,5,7,cheese] 
       
L[0] 
       
3
3
print(L[1],L[3]) 
       
(5, cheese)
(5, cheese)
L=srange(20) 
       
print(L) 
       
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
L[10] 
       
10
10
L[10:16] 
       
[10, 11, 12, 13, 14, 15]
[10, 11, 12, 13, 14, 15]

Suppose we want just the last element of L: we can access that by L[len(L)-1], which is a pain to type.  Fortunately there is shorthand for this 

print(L[len(L)-1]) print(L[-1]) 
       
19
19
19
19
print(L[-5:-1]) print(L[-5:]) 
       
[15, 16, 17, 18]
[15, 16, 17, 18, 19]
[15, 16, 17, 18]
[15, 16, 17, 18, 19]

If we wish to square each element of the list, starting with the fifth from the end, going to the end, and add them all up, we could do the following

total=0 for i in L[-5:]: total+=i^2 print(total) 
       
1455
1455

What is the meaning of the
total+=$i^2$
line? It is shorthand for
total = total + $i^2$

Mutiple for loops can be nested, with multiple indentation

print(L) 
       
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
M=matrix(20,20,0) show(M) 
       
for i in srange(len(L)): for j in srange(len(L)): M[i,j]=i+j 
       
show(M) 
       

While loops are easy to get wrong and end up with an infinite loop

i=1 total=0 while i < 10: total+=i 
       
Traceback (click to the left of this block for traceback)
...
__SAGE__
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_30.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aT0xCnRvdGFsPTAKd2hpbGUgaSAgPCAxMDoKICAgIHRvdGFsKz1p"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpY0soex/___code___.py", line 5, in <module>
    exec compile(u'while i  < _sage_const_10 :\n    total+=i
  File "", line 2, in <module>
    
  File "sage/ext/interrupt/interrupt.pyx", line 203, in sage.ext.interrupt.interrupt.sage_python_check_interrupt (/usr/local/sage-6.10/src/build/cythonized/sage/ext/interrupt/interrupt.c:1891)
  File "sage/ext/interrupt/interrupt.pyx", line 88, in sage.ext.interrupt.interrupt.sig_raise_exception (/usr/local/sage-6.10/src/build/cythonized/sage/ext/interrupt/interrupt.c:925)
KeyboardInterrupt
__SAGE__
print(total) 
       
73855412
73855412
i=1 total=0 while i < 10: total+=i i+=1 
       
print(total) 
       
45
45

Example of a function.  Suppose we wish to compute x+sin(x) for llots of values of x.

def f(x): return(x+sin(x)) 
       
f(1) a=f(1) print(a) 
       
sin(1) + 1
sin(1) + 1
f(2) 
       
sin(2) + 2
sin(2) + 2
f(2.) 
       
2.90929742682568
2.90929742682568

Bad code follows!

def f(x): print(x+sin(x)) return(False) 
       
f(1) 
       
sin(1) + 1
sin(1) + 1
a=f(1) 
       
sin(1) + 1
sin(1) + 1
print(a) 
       
False
False