Type Checking

Learn about the type hint and why we use it in our code.

We'll cover the following...

Let’s push the relationship between object and type a step further, and look at some more consequences of these rules. Here’s a function definition:

Press + to interact
def odd(n):
return (n%2!=0)
print(odd(3))
print(odd(4))

This function does a little computation on a parameter variable, n. It computes the remainder after division, the modulo. If we divide an odd number by two, we’ll have one left over. If we divide an even number by two, we’ll have zero left over. This function returns a True value for all odd numbers.

What happens when we fail to provide a number? Well, let’s just try it and see (a common way to learn Python!). Passing the string argument to the odd() function, we’ll get something like this:

Traceback (most recent call last):
  File "/usercode/main.py", line 4, in <module>
    print(odd("Hello, world!"))
  File "/usercode/main.py", line 2, in odd
    return (n%2!=0)
TypeError: not all arguments converted during string
...