Differences between Element Groups and arrays when using the in operator in python

Source: Internet
Author: User
Sometimes, to determine whether a number is in a sequence, the in operator is used to determine the membership. if the condition is true, true is returned. if the condition is false, returns a flase. Such an operator is called a Boolean operator, and its true value is called a Boolean value. In python, you can use the in symbol to determine whether the specified element exists in the list. However, I found that there are differences between tuples and arrays. The following is the detailed experiment result.

>>> 'Bitscn. com 'in ['haotu. net', 'bitscn. com '] True> 'bitscn. com 'in ('haotu. net', 'bitscn. com ') True> 'bitscn. com 'In ['bitscn. com/codes ', 'haotu.. net '] False >>> 'bitscn. com 'in ('bitscn. com/codes ', 'haotu.. net ') False >>> 'bitscn. com 'In ['bitscn. com/codes '] False >>> 'bitscn. com 'in ('bitscn. com/codes ') True

The first five Tests all conform to our expectation. there is only the last one. after the array is replaced with a tuples, python treats the tuples as strings when there is only one element in the tuples, I don't know why there is such a processing method, but I should pay attention to it during development. when I only use the in statement to determine whether there are elements in the array, it is best to use the [] array instead of the tuples.

The above is all the content of this article. I hope you will like it.

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.