Skip to main content

Type Checking in Python Tutorial

Learn about type checking, different types of the type system in various languages along with duck typing, and type hinting.
May 2020  · 7 min read

The help of its own Interpreter does type checking in Python. Since Python is a dynamic language, it doesn't force the user to enforce the type of the objects, which can lead to bugs, and errors will be hard to find. To overcome this, Python can be used in conjunction with various other tools and implement features of static type checking along with its own Duck Typing.

There are two different types of type systems followed by different programming languages, which are shown below.

    1. Static Typed Language
    2. Dynamic Typed Language

Static Typed Language

The type checking of the variable type is done at compile-time. Also, the type system of the language forces to explicitly declare the 'data-type' of the variable before its usage. The programming languages which are statically typed are: Scala, Java, C++, etc.
For example, the declaration of String in Scala is defined below:
var myCar:String = "Mercedes";
The above code shows that the variable name 'myCar' is explicitly declared as 'String' data-type with the value as 'Mercedes'.

Dynamic Typed Language

The type checking of the variable type is done at run-time. Also, the type system of the language doesn't force to explicitly declare the 'data-type' of the variable before its usage. The programming languages which are dynamically typed are: Python, Javascript, Ruby, etc.
For example, the String in Python is defined below:
myCar = "Mercedes"
The above code shows that the variable name 'myCar' doesn't need to be explicitly declared.

'type()' and 'isinstance()' in Python

The variable types can be known using Python's own 'type()'. The 'type()' can be used at runtime for the debugging purpose to identify the exact variable types in the program. Let's look at the type of some variables in the example below.

my_var = 12

The above code will give output as 'int'. Since the data type of the variable is integer where 'type()' plays a role in identifying it.

The 'insinstance('obj','class')' of Python can be used to know whether the 'obj', which is the object is the instance of the class or not. The returned output value is boolean can be one of True or False.

my_var = "Hello"

The above code will give output as True. Since the data type of the variable is a string, where 'isinstance()' plays a role in identifying it.

Duck typing in Python

There is a popular principle in Python, "If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck." Simply the principle is saying that the types of object or class don't matter, but object needs to contains similar methods and properties, then the object can be used for a particular purpose.
Let's look at an example to clarify the above statement in more detail.

Let's look at the below example, where there are two classes 'Duck' and 'Human'. Also, Duck is the class which contains methods as a quack and fly. Similarly, Human is a class with its own set of methods 'quack' and 'fly'. The Duck and Human are a different class and also a different object or type. However, Human contains similar methods as 'fly' and 'quack' in Duck. There is also a method 'fly_quack', which mostly resembles with the 'Duck' than 'Human' because it can fly and quack.

class Duck:
    def quack(self):
    def fly(self):
class Human:
    def quack(self):
        print('Trying to Quack like a duck')
    def fly(self):
        print('Spreading my arm to flap like a duck')      
def fly_quack(thing):

Let's make an instance of 'Duck' as du and pass to function 'fly_quack(du)'. Similarly, for Human instance 'h' is made and pass to function 'fly_quack(h)'.

du = Duck()
h = Human()

The output of the above code is below:

Trying to Quack like a duck
Spreading my arm to flap like a duck

Since the class, 'Human' is a different class than 'Duck', but the Python does call the function 'fly_quack' for human too. Though object types were different since the 'Human' has similar quack and fly methods, it behaves and provides the method required.

Type Hint and mypy

There is a powerful aspect of Dynamic typed Language like Python, which helps the code with accessibility and readable, but there are some downsides too. One of the downsides is to find the run time errors because Python doesn't enforce the type of the variable, or it is Dynamic typed language. The result of this can produce error and bugs, which will be difficult to find when the code length becomes larger and larger.

Python has implemented Type Hints from version 3.5. The older version might not support the functionality of TypeHints.

You'll see the simple example of without type hinting and mypy.

The function below is for the subtraction of an integer value, which subtracts two integers and returns the value.

def sub_this(x,y):
    return 'Subtraction'

Here the function above needs to accept two integers 'x' and 'y', but since the rules are not imposed, can take any data type. Also, the return value can be anything where the 'str' value('Subtraction') is returned, but expected was 'int'.

Let's see the similar implementation of the above program, but by using type hints and 'mypy', which can help to implement static type checking and reduce error and bugs in the program quite easily.

'mypy' is a python module which helps in a static type checking, which uses the Python's own dynamic checking or duck typing with the type-hinting.

You need to install 'mypy' packages
pip install mypy

You can create a file named '' in your local machine and type the following code.
The below code is a simple program that accepts two integers as an input in the parameter and after '->' shows the returned data type, which is also an 'int'. However, the function needs to return int, but string 'Subtracted two integers' is returned.

def sub_this(x:int,y:int) -> int:
    return 'Subtracted two integers'

Run your above code in the terminal with the following code.


Then the error will be shown, which indicates that the unexpected return value "str" is found and needs to be resolved with "int".

Error message

Let's change the return type to be the subtraction of the two integers so that the integer value gets returned.

def sub_this(x:int,y:int) -> int:
    return x - y

Success message

The above results show that the success message gets printed out, and no issues were found.


Congratulations, you have made it to the end of this tutorial!

In this tutorial, you've learned about type checking, different types of the type system in various languages with Python method('type()', and 'isinstance()') along with duck typing with Python and type hinting combined with 'mypy.'

If you would like to learn more about Python, take DataCamp's Software Engineering for Data Scientists in Python.


  1. Corey's Youtube video Duck Typing

Introduction to Python

4 hours
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course

Intermediate Python

4 hours
Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

Introduction to Importing Data in Python

3 hours
Learn to import data into Python from various sources, such as Excel, SQL, SAS and right from the web.
See all coursesRight Arrow

The 23 Top Python Interview Questions & Answers

Essential Python interview questions with examples for job seekers, final-year students, and data professionals.

Abid Ali Awan

22 min

Working with Dates and Times in Python Cheat Sheet

Working with dates and times is essential when manipulating data in Python. Learn the basics of working with datetime data in this cheat sheet.
DataCamp Team's photo

DataCamp Team

Plotly Express Cheat Sheet

Plotly is one of the most widely used data visualization packages in Python. Learn more about it in this cheat sheet.
DataCamp Team's photo

DataCamp Team

0 min

Getting started with Python cheat sheet

Python is the most popular programming language in data science. Use this cheat sheet to jumpstart your Python learning journey.
DataCamp Team's photo

DataCamp Team

8 min

Python pandas tutorial: The ultimate guide for beginners

Are you ready to begin your pandas journey? Here’s a step-by-step guide on how to get started. [Updated November 2022]
Vidhi Chugh's photo

Vidhi Chugh

15 min

Python Iterators and Generators Tutorial

Explore the difference between Python Iterators and Generators and learn which are the best to use in various situations.
Kurtis Pykes 's photo

Kurtis Pykes

10 min

See MoreSee More