Discussion on Python data type judgment and List script operation _python

Source: Internet
Author: User
Tags numeric value in python

Data type judgment

Use variables in Python (more than version 3.0) and make value comparisons. There are times when the following error occurs:

Typeerror:unorderable Types:nonetype () < int ()

or a similar type error.

This is because the data type of the one variable is unknown (Python cannot be judged), so there is an error.

In general, you can define and assign values to the variables you want to use in advance, for example:

Var= ' or var=0

Wait a minute.

However, if a variable is assigned by calling a function or other expression before it is compared, the above method may not work because a type error occurs if the called function has an error or if there is no output or the output type is incorrect.

So you can determine the type of the variable before you compare it to the format:

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 cosmetic or string type. Use a judgment statement to make a variable comparison if the data type is correct.

The If type (x) is types. Inttype:
Var1<var2

When this error is recommended, check to see if every step of your program is wrong.

List Script actions

The list of + and * operators are similar to strings. The + number is used to combine the list, and the * is used to repeat the list.

For a combination operation, you can also do the following:

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])

Examples below

In this program the PLUS + both sides are still list type data, so it is a combination operation.

In the following operation, the Plus + + previous is a list, the latter is a single number, the type is inconsistent, so an error.

For a single numeric value, you can make it into a list type by adding [], that is, [a[2]].

The operation of the string is the same as the above.

This operation can be used in loops and stored as dynamic numbers in read usage.

Attention

The following operations are not the same, the following operations are logarithmic addition operations.

A.append (B[i]+b[j])

The above article on the Python data type judgment and List script operation is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.