Skip to main content
\(\newcommand{\Z}{\mathbb{Z}} \newcommand{\reals}{\mathbb{R}} \newcommand{\real}[1]{\mathbb{R}^{#1}} \newcommand{\fe}[2]{#1\mathopen{}\left(#2\right)\mathclose{}} \newcommand{\cinterval}[2]{\left[#1,#2\right]} \newcommand{\ointerval}[2]{\left(#1,#2\right)} \newcommand{\cointerval}[2]{\left[\left.#1,#2\right)\right.} \newcommand{\ocinterval}[2]{\left(\left.#1,#2\right]\right.} \newcommand{\point}[2]{\left(#1,#2\right)} \newcommand{\fd}[1]{#1'} \newcommand{\sd}[1]{#1''} \newcommand{\td}[1]{#1'''} \newcommand{\lz}[2]{\frac{d#1}{d#2}} \newcommand{\lzn}[3]{\frac{d^{#1}#2}{d#3^{#1}}} \newcommand{\lzo}[1]{\frac{d}{d#1}} \newcommand{\lzoo}[2]{{\frac{d}{d#1}}{\left(#2\right)}} \newcommand{\lzon}[2]{\frac{d^{#1}}{d#2^{#1}}} \newcommand{\lzoa}[3]{\left.{\frac{d#1}{d#2}}\right|_{#3}} \newcommand{\abs}[1]{\left|#1\right|} \newcommand{\sech}{\operatorname{sech}} \newcommand{\csch}{\operatorname{csch}} \newcommand \dd[1] { \,\textrm d{#1} } \newcommand \de[2] { \frac{\mathrm d{#1}}{\mathrm d{#2}} } \newcommand \intl[4]{ \int\limits_{#1}^{#2}{#3}\dd{#4} } \newcommand\at[2]{\left.#1\right|_{#2}} \newcommand{\lt}{ < } \newcommand{\gt}{ > } \newcommand{\amp}{ & } \)

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

  1. To create a list of items then use the [] operator filled with the items:

  2. 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.

  3. 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

Indexing and slicing lists.

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.