Isinstance (1, int) to determine if it is of type int
Isinstance (1.0, float) to determine if it is float type
Isinstance (S, str) determine if it is a string type
Isinstance (A, dict) determines whether object A is a dictionary
Isinstance is an intrinsic function syntax in Python: Isinstance (object, ClassInfo) If the argument object is an instance of ClassInfo, or object is an instance of a subclass of the ClassInfo class, Returns True. If object is not a given type, the returned result is always false. If ClassInfo does not represent a class (type Object), then it is either a tuple of a class or recursively contains such a tuple of data types. Other sequence types are not allowed. If ClassInfo is not a data type or a tuple of data types, a TypeError exception is thrown.Example:>>> isinstance (1, int) determines whether the int type true>>> isinstance (1.0, float) determines whether it is float type true>>> s = ' abc ' & Gt;>>isinstance(s, str) Determine if it is a string type True>>>isinstance (a,dict) Determines whether object A is a dictionary, if true, prints true, such as false, to print false.
Title: The number of strings passed into the function, [number], [Word], [space], and [other], and return the result
# haha= "A b c_$123" haha=[1, "a", "a", [1,2,3],{"name": "AAA", "pwd": "111"}]s=0 # string L=0 # list N=0 # number D=0 # dictionary q=0 # Other Def f_c (a) : for I in A: print (Type (i)) if isinstance (i, str): print (' is a string ') global s s+=1 elif Isinstance (i, list): print (' is List ') global l L + = 1 elif isinstance (i, int): print (' is number ') Global n n+=1 elif isinstance (i,dict): print (' is Dictionary ') global d D + = 1 else: print ( ' Other ') global Q q+=1f_c (haha) print ("String:%s, List:%s, Number:%s, Dictionary:%s, Other:%s."% ( S,L,N,D,Q))
Python isinstance function to determine whether the element is a string, int, float type