|
[0, 1, 2, 3, x] [0, 1, 2, 3, x] |
Sage indexes from 0. This means that L[4] is "x".
1 1 |
x x |
|
[0, 1, 2, 3, x, 9, 7, 5] [0, 1, 2, 3, x, 9, 7, 5] |
8 8 |
If we want the list of all integers in a range, we can use range(), or, better, srange()
[1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] |
<type 'int'> <type 'int'> |
<type 'sage.rings.integer.Integer'> <type 'sage.rings.integer.Integer'> |
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
We can step through the elements of a list using a for loop.
[0, 1, 2, 3, x] [0, 1, 2, 3, x] |
0 1 4 9 x^2 0 1 4 9 x^2 |
2 5 3 10 5 26 7 50 2 5 3 10 5 26 7 50 |
Much of what we do in this course will be manipulating lists.
We've seen concatenating lists using
|
[0, 1, 2, 3, x, x + 3] [0, 1, 2, 3, x, x + 3] |
1 1 |
Traceback (click to the left of this block for traceback) ... ValueError: 7 is not in list Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_37.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("TC5pbmRleCg3KQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmp5UEC19/___code___.py", line 3, in <module> exec compile(u'L.index(_sage_const_7 ) File "", line 1, in <module> ValueError: 7 is not in list |
|
[0, 1, 2, 3, x, x - 1, x + 3] [0, 1, 2, 3, x, x - 1, x + 3] |
x - 1 x - 1 |
|
x - 1 x - 1 |
[0, 1, 2, 3, x, x + 3] [0, 1, 2, 3, x, x + 3] |
[x + 3, x, 3, 2, 1, 0] [x + 3, x, 3, 2, 1, 0] |
[0, 1, 2, 3, x, x + 3] [0, 1, 2, 3, x, x + 3] |
[9, 7, 5] [9, 7, 5] |
|
[5, 7, 9] [5, 7, 9] |
|