Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
isinstance()Sometimes a Python program receives data without knowing exactly what type it will be.
For example:
The user might enter:
But Python receives:
which is a string.
Before performing an operation, it can be useful to check what type of value we have.
Python provides two commonly used tools:
and:
type()The type() function tells us the type of an object.
Example:
Output:
You can also compare the result of type():
Output:
This checks whether the object's exact type is int.
isinstance()isinstance() checks whether a value belongs to a particular type.
Syntax:
Example:
Output:
Another example:
Output:
If the type doesn't match:
Output:
type() vs isinstance()For beginners, the difference can be remembered like this:
Example:
Both produce:
For normal type checking, isinstance() is usually more flexible.
isinstance() More Flexible?Python supports inheritance.
Inheritance means that one class can be based on another class.
You don't need to know how to create classes yet. Just remember that an object can sometimes belong to a more specialized type while still being related to another type.
A simple example is Python's Boolean type:
bool is a subclass of int.
Therefore:
returns:
But:
returns:
Why?
Because the exact type of True is bool, not int.
So:
isinstance() ExamplesLet's check several values.
Output:
This is useful when your program needs to verify incoming data.
Sometimes a value can be one of several acceptable types.
For example, a price might be:
or:
We can check both types using a tuple:
Output:
The tuple:
means:
int or float."Output:
For a string:
Output:
So the tuple is useful when a function accepts multiple possible types.
This is correct:
This is incorrect:
The second argument of isinstance() must be:
A list is not valid here.
Python 3.10 introduced another syntax for expressing type alternatives.
You can write:
Output:
You may also see:
Both can express the idea of accepting an integer or a float.
For now, learn this form first:
It is simple and widely understood.
Remember:
is a subclass of:
Therefore:
Output:
This can sometimes cause unexpected results.
Suppose you want to accept only actual integers and not Boolean values.
You can write:
Test it:
Output:
This is an advanced detail, but it is useful to know.
Let's create a function that expects a number.
Now we can use it:
Output:
But if someone passes a string:
the function raises:
This is called input validation.
Defensive programming means writing code that checks unexpected or invalid input before using it.
Imagine this function:
What if someone passes:
The result may not be what you expect.
A safer version can validate the input:
Now the function clearly communicates what it expects.
Output:
If we pass:
Python raises a TypeError because the name should be a string.
isinstance() with StringsString checking is very common.
Output:
You can combine type checking with other conditions:
Here:
isinstance() with ListsYou can also check collections.
Output:
You can also check a tuple:
Output:
type() vs isinstance() — Comparison| Feature | isinstance() | type() |
|---|---|---|
| Check an object's type | Yes | Yes |
| Check exact type | Not its main purpose | Yes |
| Understands inheritance | Yes | No when using exact comparison |
| Check multiple types | Yes, using tuple | Requires extra logic |
| Common for input validation | Yes | Sometimes |
| Beginner-friendly | Yes | Yes |
Output:
type()?type() is very useful when you simply want to inspect a value.
For example:
Output:
It is also useful when you intentionally need to check for an exact type.
Example:
However, for general type validation, isinstance() is usually the better choice.
Let's create a simple calculator that accepts integers or floats.
Output:
The function accepts:
and:
because both int and float are allowed.
isinstance() incorrectlyWrong:
Correct:
isinstance(True, int) is FalseIt is actually:
Output:
because bool is a subclass of int.
This:
does not become an integer just because you check:
The result is:
If you need an integer, convert it:
asks:
asks:
These are different operations.
| Situation | Don't | Do |
|---|---|---|
| Check general type | Always use type(x) == int | Prefer isinstance(x, int) |
| Multiple types | isinstance(x, [int, float]) | isinstance(x, (int, float)) |
| Need conversion | Only check with isinstance() | Use int(), float(), etc. |
| Validate function input | Assume input is correct | Check important inputs |
| Exact type required | Use flexible subclass check | type(x) is SomeType can be appropriate |
| Boolean as integer | Assume isinstance(True, int) is false | Remember bool is a subclass of int |
isinstance(25, int) return?value is either an int or a float?isinstance(True, int)?True?Create a function that safely validates student information before registering the student.
Your function should accept:
name must be a string.age must be an integer.course must be a string.TypeError.Try:
The function should reject the input because "21" is a string, not an integer.
In this lesson, you learned:
type() doesisinstance() doestype() and isinstance()isinstance() is useful for general type checkingint | float syntaxisinstance(True, int) is TrueTypeError for invalid inputIn the next lesson, we will continue your Python learning journey with Integer, Float, Complex (4: Numbers).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Type Conversion (Casting & type()) | Integer, Float, Complex |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.