1#Working of isinstance() with Native Types
2numbers = [1, 2, 3]
3
4result = isinstance(numbers, list)
5print(numbers,'instance of list?', result)
6
7result = isinstance(numbers, dict)
8print(numbers,'instance of dict?', result)
9
10result = isinstance(numbers, (dict, list))
11print(numbers,'instance of dict or list?', result)
12
13number = 5
14
15result = isinstance(number, list)
16print(number,'instance of list?', result)
17
18result = isinstance(number, int)
19print(number,'instance of int?', result)
20
21
22
23# Result
24
25[1, 2, 3] instance of list? True
26[1, 2, 3] instance of dict? False
27[1, 2, 3] instance of dict or list? True
285 instance of list? False
295 instance of int? True
1x = isinstance(5, int)
2y = isinstance(6.5, int)
3print(x)
4print(y)
5
6output:
7True
8False
1sample_list = ['Emma', 'Stevan', 12, 45.6, 1 + 2j, "Eric", ]
2number_list = []
3string_list = []
4for item in sample_list:
5 if isinstance(item, (int, float, complex)):
6 number_list.append(item)
7 elif isinstance(item, str):
8 string_list.append(item)
9
10# String List
11print(string_list)
12# Output ['Emma', 'Stevan', 'Eric']
13
14# Number list
15print(number_list)
16# Output [12, 45.6, (1+2j)]
1#Python isinstance Function
2x = isinstance("Hello", (float, int, str, list, dict, tuple))