Python data type judgment and list script operations, python Data Types

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.