Python Reference Library →

Python isinstance() Function – Check Type of a Variable

Let’s look at isinstance() function – check type of a variable

  1. Notice

    isinstance() checks whether an object belongs to a class or its subclasses.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 3
print(isinstance(number, int))
print(isinstance(number, str))
Live state

Top-level scope

number→Object 1
Object 1 · int3
This step

isinstance() checks whether an object belongs to a class or its subclasses.

Picture isinstance() function – check type of a variable

isinstance() checks whether an object belongs to a class or its subclasses.

Image brief

Alt text
A diagram for Python isinstance() Function – Check Type of a Variable. Instructions: number = 3 print(isinstance(number, int)) print(isinstance(number, str)). Displayed result: True False. The final example bindings are number --> object 1 (int). object 1: 3
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python isinstance() Function – Check Type of a Variable". Teaching focus: isinstance() checks whether an object belongs to a class or its subclasses. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 3 print(isinstance(number, int)) print(isinstance(number, str)) | v DISPLAYED RESULT True False FINAL BINDINGS number --> object 1 (int) object 1: 3 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python isinstance() Function – Check Type of a Variable. Instructions: number = 3 print(isinstance(number, int)) print(isinstance(number, str)). Displayed result: True False. The final example bindings are number --> object 1 (int). object 1: 3

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 3
print(isinstance(number, int))
print(isinstance(number, str))
Live state

Top-level scope

number→Object 1
Object 1 · int3
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
True
False

What do you expect this program to print?

What does this example display?

number = 3
print(isinstance(number, int))
print(isinstance(number, str))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

True False

Follow the printed values in order. The output is: True False

More examples and reference

Python isinstance() Function

The isinstance() function in Python checks whether a variable is an instance of a specific class or a tuple of classes. It's a safe and recommended way to validate data types.

Syntax

isinstance(object, classinfo)

Parameters:

  • object – The value or variable to test.
  • classinfo – A class, type, or a tuple of classes and types.

Returns:

  • True if object is an instance (or subclass instance) of classinfo.
  • False otherwise.

Example 1: Check if a variable is an integer

x = 5
print(isinstance(x, int))
True

Example 2: Check if a string is a list (it isn’t)

name = "Alice"
print(isinstance(name, list))
False

Example 3: Check against multiple types

value = 3.14
print(isinstance(value, (int, float)))
True

Here, value is checked against both int and float. The function returns True if it matches any one.

Use Case: Why Use isinstance()?

  • To validate function arguments
  • To avoid type errors in logic
  • To write type-safe code, especially in large projects
  • To apply different logic based on variable type (e.g., float vs int)

Example 4: Type checking in a function

def square(x):
    if isinstance(x, (int, float)):
        return x * x
    else:
        return "Invalid input"

print(square(4))      # 16
print(square("four")) # Invalid input
16
Invalid input

Common Mistakes

  • Passing a string for classinfo like "int" (should be int without quotes)
  • Confusing type() with isinstance(): isinstance() supports inheritance, type() does not

Interview Tip

isinstance() is often used in questions involving type safety or when a function needs to behave differently based on data types. It’s also commonly used in object-oriented questions.

Summary

  • isinstance(obj, type) returns True if obj is of the given type or its subclass.
  • Use a tuple to check against multiple types.
  • Safer than using type() when inheritance is involved.

Practice Problem

Write a function that accepts a value and returns:

  • "Integer" if it’s an integer
  • "Float" if it’s a float
  • "Other" otherwise
def identify_type(x):
    if isinstance(x, int):
        return "Integer"
    elif isinstance(x, float):
        return "Float"
    else:
        return "Other"

print(identify_type(42))
print(identify_type(3.14))
print(identify_type("hello"))
Integer
Float
Other

What to remember

Connect what you noticed in the example with the idea below.

Remember

isinstance() checks whether an object belongs to a class or its subclasses.

Watch out for this idea

isinstance() tests whether two objects have equal values.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →