Python data type judgment and list script operations, python Data Types
Data Type Determination
When using variables in python (Version 3.0 or later) and comparing values. The following errors may occur:
TypeError: unorderable types: NoneType () <int ()
Or similar type error.
This is because the data type of one variable is unknown (which cannot be determined by python), so an error occurs.
In general, you can define and assign values to the variables you want to use in advance. For example:
Var = ''or var = 0
And so on.
However, if the variable is assigned a value by calling a function or another expression before comparison, the above method may not work, because if the called function is incorrect or has no output or the output type is incorrect, A type error occurs.
Therefore, you can determine the type of the variable before comparison. format:
Import types
Type (x) is types. IntType
Or
Type (x) is types. StringType
The preceding statements determine whether the data type of a variable is integer or string. Use a judgment statement. If the data type is correct, compare the variables.
If type (x) is types. IntType:
Var1 <var2
When this error occurs, check whether each step of your program has an error.
List script operations
The operators of list pairs + and * are similar to strings. + Is used to combine the list, and * is used to repeat the list.
You can also perform the following operations for the combined operation:
A = []
B = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
A. append (B [I: j] + B [j + 1: k] + [B [k + 1]-m])
Example
In this program, adding signs + is a list of data on both sides, so it is a combination operation.
In the following operation, the plus sign + the first one is the list, the last one is a single number, and the type is inconsistent, so an error occurs.
For a single value, you can add [] to change it to the list type, that is, [a [2].
The operations on strings are the same as those 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 preceding operations. The following operations are logarithm addition operations.
A. append (B [I] + B [j])
The above discussion about Python data type judgment and list script operations is all the content shared by the editor. I hope to give you a reference and support for the help house.