Section2.5Sage and Python
Sage is built on top of Python. It will do no harm to find out a little about this programming language and will aid your understanding of some of the examples used in this manual.
Subsection2.5.1Lists, Tuples, and Dictionaries
Python has a number of built in data types. We will look at three that will come up again and again in this manual.
Subsubsection2.5.1.1Lists
Creating Lists
-
To create a list of items then use the [] operator filled with the items:
-
Creating an empty list and then adding items.
Notice that the items inside the list can be different types. In this cas a sting and a number.
-
Creating lists using python methods.
This is not a python tutorial so have a look here for more information on range() and append().
Accessing items in a list
Subsubsection2.5.1.2Tuples
A tuple is an immutable list. A tuple can not be changed in once it is created.
Tuples are defined exactly like lists except by using () brackets rather than [] brackets.
Tuples are handy structures for thing like \((x,y,z)\) coordinates and are used extensively in the examples and exercises in this manual. For example, as co-ordinate ranges for plots. See for example the utility method plot_tangentline() 3.3.1.
Subsubsection2.5.1.3Dictionaries (Associative Arrays)
Unlike lists, which are indexed by a numbers, dictionaries are indexed by keys, which can be any immutable type.
A dictionary is an unordered set of key : value pairs, with the requirement that the keys are unique (within the dictionary). A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary.