"Python" Variable value exchange, determine whether an array contains an element

Source: Internet
Author: User

Originally, these two problems belong to the introduction of programming simple can no longer simple problem, it is not worth writing a record to record.

First, variable value exchange

First of all, the variable value exchange, starting with the C language, we know to set a temporary variable, then the value of an element to override this temporary variable, avoid temporary overwrite, etc., if you do not set temporary variables, there is also the exchange of bit operations

However, Python does not have to be so complex, if you want to exchange variables e1,e2 each other's values, the following line of code is sufficient:

E1,e2=e2,e1;

For example, the following program:

E1=2;e2=3;print "E1:" +str (E1) + ", E2:" +str (E2); E1,e2=e2,e1;print "E1:" +str (E1) + ", E2:" +str (E2);

The results are as follows:


Second, determine whether the array contains an element

Python determines whether an array contains an element that does not encapsulate the corresponding contain method, and the index method does not work, and if the element is not found in the array, it is saved directly.


This does not mean that you need to define a function (method), specifically to find out if the element is in an array, because Python has no encapsulation method, but it has in this keyword.

For example, the following programs:

a=[1,2,3];p rint 3 in A;print 4 in A;
The results of the operation are as follows:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Python" Variable value exchange, determine whether an array contains an element

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.