Python Isinstance With Examples (Guide)
Python Isinstance With Examples (Guide)
com/python-isinstance-explained-with-examples/
PYnative
Python Programming
Learn Python Exercises Quizzes Code Editor Tricks
1 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming How To Use
Learn isinstance()
Python Exercises Quizzes Code Editor Tricks
Function in Python
Syntax:
isinstance(object
• It takes two
Python isinstance()
arguments, and
both are
mandatory.
• The
isinstance()
function checks
if the object
argument is an
instance or
subclass of
classinfo class
argument
2 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming
For example, isinstance(x, int) to check
Learn Python Exercises Quizzes Code Editor Tricks
if x is an instance of a class int .
classinfo is a type name or Class name
you want to check against the variable. Here
you can specify data type name or Class
name.
You can also pass multiple classes/types in a
tuple format. For example, you can pass
int , str , list , dict , or any user-created
class.
Example
num = 90
result = isinstance(num, int)
if result:
print("Yes")
else:
print("No")
Run
Output:
Yes
3 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming
Learn Python Exercises Quizzes Code Editor Tricks
Note: If the classinfo argument is not a
Class, type, or tuple of types, a TypeError
exception is raised.
print(isinstance(number, float))
# output False
pi = 3.14
# Check 3.14 is an instance of class float
print(isinstance(pi, float))
# Output True
4 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming
print(isinstance(student_report, dict))
Learn Python Exercises Quizzes Code Editor Tricks
# Output True
Run
var = None
# empty but not None
s1 = ''
print(isinstance(var, float))
# Output False
print(isinstance(s1, str))
# Output True
Run
5 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming
Learn Python Exercises Quizzes Code Editor Tricks
Example
def check_number(var):
if isinstance(var, (int, float)):
print('variable', var, 'is instance of numeric type
else:
print('variable', var, 'is not instance of numeric
num1 = 80
check_number(num1)
# Output variable 80 is instance of numeric type
num2 = 55.70
check_number(num2)
# Output variable 55.7 is instance of numeric type
num3 = '20'
check_number(num3)
# Output variable '20' is not instance of numeric type
Run
class Employee:
6 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming
self.salary = salary
Learn Python Exercises Quizzes Code Editor Tricks
class Person:
Run
7 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming
class Learn Python Exercises
Developer(object): Quizzes Code Editor Tricks
# Constructor
def __init__(self, name):
self.name = name
def display(self):
print("Developer:", self.name, "-"
class PythonDeveloper(Developer):
# Constructor
def __init__(self, name, language):
self.name = name
self.language = language
def display(self):
print("Python Developer:", self.name
# Object of PythonDeveloper
dev = PythonDeveloper("Eric", "Python")
# is PythonDeveloper object an instance of a PythonDevelope
print(isinstance(dev, PythonDeveloper))
# Output True
Run
8 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming
isinstance() function:
Learn Python Exercises Quizzes Code Editor Tricks
Checking if an object is an
instance of a list type
Run
9 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming
# Output Yes ['Jordan', 'Donald', 'Sam'] is a nested list
Learn Python Exercises Quizzes Code Editor Tricks
Run
# String List
print(string_list)
# Output ['Emma', 'Stevan', 'Eric']
# Number list
print(number_list)
# Output [12, 45.6, (1+2j)]
Run
Next steps
10 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming
Let me know your comments and feedback in the
Learn Python Exercises Quizzes Code Editor Tricks
section below.
Solve:
About Vishal
Founder of PYnative.com I am a
Python developer and I love to
write articles to help
developers. Follow me on Twitter. All the best
for your future Python endeavors!
15+ Topic-specific
Exercises
Exercises and
Quizzes
11 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
Comments
ZAINAB says
JA NUA R Y 1 5 , 2 0 2 2 AT 7 : 4 6 P M
REPLY
REPLY
Vishal says
JA NUA R Y 7 , 2 0 2 1 AT
10:07 AM
REPLY
Spoorthi says
O C TO B E R 1 3 , 2 0 2 0 A T 5 : 1 5 A M
REPLY
Vishal says
O C TO B E R 1 6 , 2 0 2 0 A T
4:45 PM
REPLY
12 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
PYnative
Python Programming
Alberta says
Learn Python Exercises Quizzes Code Editor Tricks
J U LY 1 7 , 2 0 2 0 A T 1 2 : 1 5 A M
REPLY
Vishal says
J U LY 2 0 , 2 0 2 0 A T 1 : 1 6 P M
REPLY
peter says
JUNE 1 4 , 2 0 2 0 AT 9 : 1 5 P M
REPLY
deborgher says
M AY 2 5 , 2 0 2 0 AT 9 : 0 7 P M
REPLY
Handy says
M AY 2 4 , 2 0 2 0 AT 9 : 2 3 P M
REPLY
Vishal says
M AY 2 5 , 2 0 2 0 AT 4 : 0 1 P M
REPLY
Leave a Reply
13 of 14 12/20/2022, 4:19 PM
Python isinstance() With Examples [Guide] https://pynative.com/python-isinstance-explained-with-examples/
Comment *
Name * Email *
Post Comment
PYnative.com is for Python • Learn Python To get New Python Tutorials, • About Us
lovers. Here, You can get • Python Basics Exercises, and Quizzes • Contact Us
Tutorials, Exercises, and
• Python Databases • Twitter We use cookies to improve
Quizzes to practice and
• Python Exercises your experience. While using
improve your Python skills. • Facebook
PYnative, you agree to have
• Python Quizzes • Sitemap
read and accepted our Terms
• Online Python Code Editor
Of Use, Cookie Policy, and
• Python Tricks Privacy Policy.
Copyright © 2018–2022
pynative.com
14 of 14 12/20/2022, 4:19 PM