Section 1.4 Data Types
In computer science, Data types represent data based on properties of the data. Python and Sage use data types to implement these classes. Since Sage builds upon Python, it inherits all the built-in Python data types. Sage also provides classes that are well-suited for mathematical computations.
Let’s ask Sage what type of object this is.
The
type()
function reveals that 2
is an instance of the Integer class. Sage includes numerous classes for different types of mathematical objects.In the following example, Sage does not evaluate an approximation of
sqrt(2) * log(3)
. Sage will retain the symbolic value.
String: a
str
is a sequence of characters used for text. You can use single or double quotes.
Boolean: The type
bool
can be one of two values, True
or False
.
List: A mutable collection of items within a pair of square brackets
[]
. If an object is mutable, you can change its value after creating it.Lists are indexed starting at
0
. Here, we access the first element of a list by asking for the value at index zero.Lists have many helpful methods.
Tuple: An immutable collection within a pair of parenthesis
()
. If an object is immutable, you cannot change the value after creating it.
set: A collection of items within a pair of curly braces
{}
. set()
with lowercase s
is built into Python. The items in a set are unique and unordered. After printing the set, we see there are no duplicate values.
Set is a built-in Sage class.
Set
with a capital S
has added functionality for mathematical operations.We start by defining a
list
within square brackets []
. Then, the Set()
function creates the Sage set object.
Dictionary: A collection of key-value pairs.
Use the
pprint
module to print the dictionary in a more readable format.