Section 1.2 Data Types
In computer science, Data types define the properties of data, and consequently the behavior of operations on these data types. Since Sage is built on Python, it naturally inherits all Python’s built-in data types. Sage does also introduces new additional and custom classes and specific data types better-suited and optimized for mathematical computations.
Let’s check the type of a simple integer in Sage.
The
type()
function confirms that 2
is an instance of Sage’s Integer class. Sage supports symbolic computation, where it retain and preserves the actual value of a math expressions rather than evaluating them for approximated values.
String: a
str
is a sequence of characters. Strings can be enclosed in single or double quotes. Boolean: a (
bool
) data type represents one of only two possible values: True
or False
. List: A mutable sequence or collection of items enclosed in square brackets
[]
. (an object is mutable if you can change its value after creating it).Lists are indexed starting at
0
. The first element is at index zero, and can be accessed as follows:The
len()
function returns the number of elements in a list. Tuple: is an immutable sequence of items enclosed in parentheses
()
. (an object is immutable if you cannot change its value after creating it). set (with a lowercase
s
): is a Python built-in type, which represents an unordered collection of unique items, enclosed in curly braces {}
. The printout of the following example shows there are no duplicates.In Sage,
Set
(with an uppercase S
) extends the native Python’s set
with additional mathematical functionality and operations.We start by defining a
list
using the square brackets []
. Then, Sage Set()
function removes any duplicates and provides the mathematical set operations. Even though Sage supports Python sets, we will use Sage Set
for the added features. Be sure to define Set()
with an uppercase S
. A Dictionary is a collection of key-value pairs, enclosed in curly braces
{}
.Use the
pprint
module to improve the dictionary readability.