Str, List and Tuple


In addition to numerical types, sage also has 7 sequential data types; in other words, data types that are sets that may be iterated through. Three of these are particularly relevant to coding in Sage, so we will cover those here. These types are str, list and tuple, constructed with the commands str(), list() and tuple(). An str object is essentially a sequence of characters, or in other words basically standard text. list and tuple are sequences of any valid data type (including list and tuple themselves), with the difference being items in a list may be changed after creation while items in a tuple may not.

Sage Cell


Strings are constructed using the command str(), or automatically if python sees text within single ('') or double ("") quotes. They are used mainly for options in other Sage commands (for instance if selecting a color for a plot, a string is used to determine the color) or printing information with the print command.

Lists and Tuples

Lists and tuples are mostly the same. However, lists are contained in square brackets and tuples in parentheses to signify their different types. This is because data in tuples cannot be changed. Lists and tuples are both common in more advanced sage functions such as linear algebra operations. Tuples are also used as ordered pairs when plotting points or scatter plots. Lists and tuples are constructed by listing items separated by commas in square brackets [] for lists or parentheses () for tuples. Alternatively, they may be constructed using the commands list() and tuple().


a = str('Queens of the Stone Age')
b = 'Broken Bells'
a = [ 2, 3, 5, 7, 11 ]
print(type(a), a)
b = ( 13, 17, 19, 23, 29 )
print(type(b), b)




Primary Tags—Programming: Python

Secondary Tags—Python: Data types

Related Cells





Date: 28 Mar 2019 16:09

Submitted by: Zane Corbiere

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License