Python Novice Learning Basics Operator--member operations and identity operations

Source: Internet
Author: User

Member operators

Python has a member operator that can determine whether an element is in a sequence. For example, you can tell if a character belongs to the string, you can tell if an object is in the list, and so on.

The syntax for using member operators in Python is:

obj [not] in sequence

The return value of this operator is true or false.

Let's look at a code example:

Identity operator

Python supports the comparison of the object itself, and the syntax for comparison is:

is [not]  obj2

The identity operator is used to compare whether 2 objects are the same object, while the = = in the previous comparison operator is used to compare the values of 2 objects for equality.

Here we need to tell you how the identity operator is judged.

The variables in Python have 3 properties: Name, ID, value.

Name can be understood as variable name, ID can be combined with memory address to understand, value is a variable. The IS operator is judged by this ID, which returns true as the ID, otherwise false.

Like what:

A = [1, 2, 3= [1, 2, 3]print(a = = b)print is b)

The result of this code output is true and false, because the value of variable A and variable B is the same, so the value of the variable is compared with the = = operator, so returns True. But when using is, the comparison is id,a and B's ID is different (can use ID (a) to see the ID of a), so return false.

But not all of this is true, for small integers, python caches all integers from 5 to 257, in total, 262. So the following code:

A= + = +printis =

Returns the result one is true, one is the same as above, and the result of true is that Python false,false small integers, and strings are the same, and using is will return equality.

More learning content, just in the code bud Net Http://www.mayacoder.com/lesson/index

Python Novice Learning Basics Operator--member operations and identity operations

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.