INTERACTIVE: Checking object types in Python#

This is our first interactive exercise. Click the button at the top to open this notebook in Colab, and run through the exercises interactively there!

In order to check the type of an object, we use type(<input_object>), where <input_object> is your input. Try the following (remember + to run the cells!):

print(type(10))
<class 'int'>
print(type(10.0))
<class 'float'>
print(type(-3))
<class 'int'>
print(type(3.14))
<class 'float'>
print(type("this is a string"))
<class 'str'>
print(type('this is also a string'))
<class 'str'>
print(type(True))
<class 'bool'>
print(type("True"))
<class 'str'>
print(type(None))
<class 'NoneType'>

Did all of that make sense? Was any answer unexpected? If so, ask one of us! This course relies on us discussing anything that’s not clear, so make sure to talk to us.

Note that we have used the print( .. ) command here. Anything within print( .. ) will be printed after the cell runs.

Quick Exercise#

What is the correct type of

  • a) 4.5

  • b) 1.0

  • c) 2

  • d) “Hello World”

  • e) False

  • f) “False”

Please check your answers in the cell below, by using type(). You can put multiple print( .. ) statements in the same cell, try it!

Printing results#

In order to print results in the same cell, one below the other. Use print (<your_expression>) as follows

print("You can use multiple")
print("lines in a cell")
print("This works for all python operations")
print("not just the print statement")
You can use multiple
lines in a cell
This works for all python operations
not just the print statement