Object types are the ways we have to represent information in our programming.
Python has some built-in types we have already seen, but now we will see more.
Some of them are:
Numeric types: int, float, complex
Boolean types (logic): bool (True and False)
Sequence types: list, tuple, str, range, bytes, and others
Mapping type: dict
Set types: set, frozenset
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 []
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.
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 =1a()
TypeError: 'int' object is not callable
Testing some syntax…
a =1a[0]
TypeError: 'int' object is not subscriptable
a =1a(0)
TypeError: 'int' object is not callable
a =1a{0}
SyntaxError: invalid syntax (2290390298.py, line 2)