Data Definitions for Lab 1 in CSC 202
1 Using built-in types in Python
2 Defining new kinds of data in Python
3 Using Data Definitions
8.16

Data Definitions for Lab 1 in CSC 202🔗

In Lab 1, many questions ask you to provide "Data Definitions". What is a data definition?

In general, a data definition shows us what kind of Python values we are going to use to represent a piece of real-world data.

Also, these data definitions are going to generally take the form of python type definitions. You may be familiar with these, but you may not; either is fine.

Before we start defining our own types, we should take a look at how types can be used in Python.

1 Using built-in types in Python🔗

Python comes with a number of built-in types. The simplest of these are types like int (the type of integers) and str (the type of strings).

We can define a function that appends two strings, like this:

# append two strings

def str_append(a : str, b : str) -> str:

    return a + b

 

 

print(str_append("ab","cd"))

If you’ve seen Python types before, this might not be surprising. If you haven’t, then maybe there are a couple of things that are new. First, we are specifying that the two input arguments, a and b, are both strings. We’re also specifying that the return type should be a string as well.

Once we have learned how to run the type-checker, mypy, we can type-check this program:

(csc202-2254) ENG-CLEMENTS-1:/tmp/foo/lab-1-jbclements (git)-[master]- clements> mypy lab1.py

Success: no issues found in 1 source file

What if we get it wrong? What if the body of the function doesn’t actually return a string?

Here’s a broken version:

# append two strings

def str_append(a : str, b : str) -> str:

    return 14

 

print(str_append("ab","cd"))

Now, when we run the type-checker, we get an error:

(csc202-2254) ENG-CLEMENTS-1:/tmp/foo/lab-1-jbclements (git)-[master]- clements> mypy lab1.py

lab1.py:9: error: Incompatible return value type (got "int", expected "str")  [return-value]

Found 1 error in 1 file (checked 1 source file)

Nice! The type-checker caught an error.

2 Defining new kinds of data in Python🔗

Can we define our own new kinds of data, in Python? Indeed we can. The syntax for this is extremely simple. Suppose we want to define Height as a new kind of data, which is actually just a floating point number representing a number of centimeters? That definition looks like this:

Height : TypeAlias = float # in cm

Now, we can use Height just like we did int and str:

# return twice the height

def double_height(h : Height) -> Height:

    return h * 2

 

print (double_height(343.3),686.6)

This is a funny example; it would be pretty reasonable just to use the type float for this. One advantage of doing it in this way is that we’ve provided an explicit comment indicating that something that has the type Height should be a floating point number representing a number of centimeters.

In general, though, I would agree that the data definitions used in Lab 1 are pretty simple; we’re on our way to defining more interesting data definitions, and these are mostly just warm-ups.

3 Using Data Definitions🔗

There’s one more piece to the puzzle. Once we’ve created a data definition, how and where can we use it?

One way to use it is in the function header, as shown above. In the double_height function shown above, we use the new Height type as an input type, attached to h, and as an output type, specified after the arrow (->).

We can also use it in a variable declaration. So, for instance, to indicate that my_height is a Height, and that it should have the value 149, I would write

my_height : Height = 149.0