Built-in types

Some of them are:

It is always useful to consult documentation on each to see what you can and can’t do with them.

Container types

  • Some of these stand for objects that are collection of other objects. These are called containers.
  • For example, you may have a variable pointing to a single number (a numeric type). But you can also a variable pointing to a collection of numbers, or strings, or even other collections of numbers.
  • Think like “drawers” or “boxes” in a bookshelf

Subscriptable types

  • Remember our metaphor: objects are buildings, variables are addresses
    • As we have types of houses (residential, commercial), we also have types of objects (integer, float, string)
  • How is an address when we have a condo or apartment building - i.e., multiple “houses” in the same address?
    • “1234 Smith Ave Apt 101
  • It is also possible to have containers whose contents can be accessed by some “complement”. These are called subscripts.
    • To access a value, we write the variable followed by some value inside square brackets []

For example, a Python list:

condo = ["room 1", "room 2", "room 3"]
print(condo[0])
room 1

Notice that if we try to “subscript” an object which is not subscriptable, we get an error:

house = 3
print(house[0])
TypeError: 'int' object is not subscriptable

Sequence vs mapping types

  • In sequence types such as lists, tuples and strings, subscripts are integer numbers, which are called indexes.
    • Attention: indexes always starts at zero!
    • index 0 is for 1st element, index 1 is for 2nd, and so on…
condo = "room 1", "room 2", "room 3"
print(condo[0])
print(condo[1])
print(condo[2])
room 1
room 2
room 3
  • If the index can’t be found, we will have an error:
condo = "room 1", "room 2", "room 3"
print(condo[4])
IndexError: tuple index out of range
  • In mapping types, however, subscripts can be any kind of object (given that it is an immutable object — e.g., lists are not allowed), which are called, in this case, keys.
  • This is the case of the dictionary type (dict):
band = {"vocals": "John Anderson", "guitar":"Steve Howe", "bass": "Chris Squire", "keyboard": "Rick Wakeman"}
print(band["vocals"])
print(band["guitar"])
John Anderson
Steve Howe

Type properties

  • Subscriptable types: containers whose objects can be accessed using square brackets [];
  • Ordered types: containers whose elements are ordered and thus can be accessed by an index (integer number);
  • Immutable types: objects whose elements cannot be changed;
  • Callable types: functions and classes (check, for example, type(print) or type(math.cos))
    • You “call” these objects to execute some code by typing the object followed by parenthesis (and eventually pass some information inside the parenthesis): print("hi")
    • See what happens if you try calling a non-callable object:
a = 1
a()
TypeError: 'int' object is not callable

Testing some syntax…

a = 1
a[0]
TypeError: 'int' object is not subscriptable
a = 1
a(0)
TypeError: 'int' object is not callable
a = 1
a{0}
SyntaxError: invalid syntax (2290390298.py, line 2)

Summarizing properties

Type Container Subscriptable Ordered (sequence) Mutable
Numbers and booleans No No No No
Lists Yes Yes (integers) Yes Yes
Tuples Yes Yes (integers) Yes No
Strings Yes (only characters) Yes (integers) Yes No
Dictionaries Yes Yes (immutable objects) No Yes
Sets Yes (only immutable objects) No No Yes