Author: Franco Saliola <saliola at gmail.com>
To create a list of objects, use square brackets.
Create the list [63, 12, -10, 'a', 12] , assign it to the variable L , and print the list. (Hint : Variable assignment in Sage/Python is done with = . For example, a = 3 defines the a to be 3.)
Use the len command to find the length of the list L.
To access an element of the list, use the syntax L[i] , where i is the index of the item. What is L[3]?
What is L[1]?
What is the index of the first item of L?
What is L[-1], L[-2]?
Access the last item in L.
An important concept about lists is that (like dictionaries, but unlike many objects in Sage), they can be directly changed. This property is known as mutability.
Change L[3] to 17.
This concept can lead to some confusion at first. See if you can guess what the output of the following commands will be.:
This result makes sense when you understand that a and b are both labels attached to the same list. Compare that result with the following.:
In this case, we changed the object that b is attached to (to the object 2 plus the object 1, which is the object 3), while a continues to be attached to the object 2. This concept will be useful to keep in mind, as we discuss some methods which can be used to modify lists.
By typing L.<tab key>, you get a list of methods for L. Use one of these methods to append 17 to the end of L.
Insert the letter ‘b’ at index position 2 (do not change the element in position 2, but add a new element).
Remove the second occurrence of from L.
Redefine L to be the list [3, 1, 4, 1, 5, -1, 0].
Reverse the list L.
Sort the list L.
Guess the result of the following commands.
{{{id=16| L = [3, 1, 2] M = L.sort() print L, M /// }}}
Now try the following.
{{{id=17| L = [3, 1, 2] M = sorted(L) print L, M /// }}}
The range command provides an easy way to construct a list of integers.
Read the documentation (type: range? and hit enter or tab). Use it to
create the list .
Create the list of even numbers between 1 and 100 (including 100).
The step argument in the range command can be negative. Use
range to construct the list .
Sage (but not Python!) includes syntax to simplify creating lists like the above easier. What is the output of the command [2, 4, .., 100] ?
Create the list using Sage’s special
syntax. Compare this with the output of range(1,5,0.5) .
We already know how to create the list :
Using a list comprehension, we can now create the list
Exercises:
Create two lists:
Use a list comprehension to construct the list
Using a list comprehension and the command sum, compute
The sum of the squares of the first ten natural numbers is:
The square of the sum of the first ten natural numbers is:
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
A list can be filtered using a list comprehension. For example, to create a list of the squares of the prime numbers between 1 and 100, we use a list comprehension as follows:
Exercise: Use a list comprehension to list all the natural numbers below 20 that are multiples of 3 or 5. Hints:
- To get the remainder of 7 divided by 3 use 7 % 3.
- To test for equality use two equal signs (==); for example, 3 == 7.
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.