Home >Backend Development >Python Tutorial >A brief discussion on Python data type judgment and list script operations
Data type judgment
When using variables in python (version 3.0 or above) and performing value comparisons. Sometimes the following error occurs:
TypeError: unorderable types: NoneType()
or similar type errors.
This is because the data type of one variable is unknown (python cannot determine it), so an error occurs.
Under normal circumstances, the variables to be used can be defined and assigned values in advance, for example:
var=' ' or var=0
, etc.
However, if the variable is assigned by calling a function or other expression before comparison, the above method may not work, because if the called function has an error or has no output or the output type is wrong, the type will appear mistake.
So you can judge the type of the variable before comparison. The format is:
import types
type(x) is types.IntType
or
type(x) is types.StringType
The above is to determine whether the data type of the variable is an integer or a string type. Use a judgment statement to compare variables if the data type is correct.
if type(x) is types.IntType: It is recommended that when this error occurs, first check whether every step of your program has errors. List script operations The list operators + and * are similar to string operators. The + sign is used for combined lists, and the * sign is used for repeated lists. For combined operations, you can also perform the following operations: a=[] The following example In this program, both sides of the plus sign + are still list type data, so it is a combined operation. In the following operations, the plus sign + the former one is a list and the latter one is a single number. The types are inconsistent, so an error occurs. For a single value, you can make it a list type by adding [ ], that is, [a[2]]. The operations on strings are the same as above. This operation can be used in a loop and stored as a dynamic number for reading. Note The following operations are different from the above operations. The following operations are logarithmic addition operations. a.append(b[i]+b[j]) The above article briefly discusses Python data type judgment and list script operations. I have compiled all the content shared with you. I hope it can give you a reference. I also hope that everyone will support the PHP Chinese website. For more articles on Python data type judgment and list script operations, please pay attention to the PHP Chinese website!
var1
b=[1, 2,3,4,5,6,7,8,9,0]
a.append(b[i:j]+b[j+1:k]+[b[k+1]-m ])