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