- Iteration: repeat i.e., the ability to run a code block; before introduction has been achieved in two ways iteration: recursive and
for
cycle;- Another method describes the following:
while
loop;
(A) re-assignment
- Multiple assignments for variables is legal;
- Assign values to variables are useful, but need to be careful; frequent assignment to a variable will cause decline in code readability, not easy to debug;
=
And mathematics symbols are not the same:
- Mathematically, a symmetrical relationship is equal, but the assignment is not a symmetrical relationship;
- Mathematics equal proposition, either true or false, but in python, the assignment can make two variables equal, but these two variables does not necessarily have to maintain this relationship;
(Ii) update the variable
Each variable assignment is a common form of update:
x = x + 1
- Attempt to update a variable that does not exist error occurs;
- So before the update needs to be initialized;
(C) while statement
The computer is often used to handle repetitive tasks automatically. The computer is very good at not repeating the same or similar careless leak to the task, and humans do well in this regard. In the computer program, also referred to repeated iterations (Iteration)
def countdown(n):
while n > 0:
print(n)
n = n -1
print('Blastoff')
-
while
Statement of the implementation process:- Determine the condition is true or false;
- If false, exit the while loop, perform the following sentence;
- If true, the theme running while loop, the end of the run to return the first step;
-
Loop body should change the value of one or more variables, so as to make the final judgment condition is false, otherwise, the loop will not be terminated;
(Iv) break
break
You can jump one cycle;
while
Cycle of such an approach is very common because you can judge anywhere cycling conditions (not just the beginning of the cycle), and you can actively express termination condition ( "When this happen is terminated"), rather than passively represent ( "continue until this situation.")
(Five) square root
Circulation commonly used in numerical calculation procedures, such procedures are generally started, iteratively calculated from the value of a schematic;
Newton's method to calculate the square root
This method can be used to calculate the square root of a
>>> a = 4
>>> x = 3
>>> y = (x + a/x) / 2
>>> y
2.166666666667
It can be seen first close to the square root calculations have 4, i.e. 2, recalculated
>>> x = y
>>> y = (x + a/x) / 2
>>> y
2.00641025641
As it can be seen, along with increasing the number of iterations, more and more close to the real value;
- In general, we do not know should be calculated several times to get the correct answer, but when the estimated value does not change, you get the correct answer
- When
x == y
you can stop the calculation; - But directly compare two floating-point numbers are equal is dangerous because only about floating-point representation, some real numbers can not be accurately represented by floats, so there is a need to determine its accuracy
epsilon
as a threshold value;
- When
while True:
print(x)
y = (x + a/x) / 2
if abs(y - x) < epsilon:
break
x = y
(Vi) algorithm (Algorithm)
What algorithm is:
What is not the algorithm:
- In learning-digit multiplication, it may back out of the multiplication table. In fact, you just remember the 100 definite answer. This knowledge is not algorithmic of;
What is the algorithm:
- If you compare "lazy", you might find some tips. For example, in order to calculate the product of n and 9 n-1 you can put the product as the first digit, then 10-n as the second digit, to obtain their product. The trick is to be any single-digit multiplication and 9 ** ** universal solution. This is an algorithm;
- One of the features is the algorithm does not require too much mental calculations. Algorithm is a mechanical process, each step is based on a simple set of rules to follow on step performed;
- Execution algorithm was boring, but the design algorithm is challenging what is the core of computer science;
(Vii) commissioning
When you start writing more complex programs, you will find that most of the time spent on debugging. More code means a higher probability of error, and there will be more bug hiding places;
-
One way to reduce debugging time is halved debugging , namely:
- If the program has 100 lines, one line you a check, you need 100 steps;
- Try to split the problem into two halves. Code portion in the middle or near the place, to find a middle value can be checked. Add a line
print
statement (or other code having verifiable effects), and then run the program; - Analyzing the wrong place or at the lower half of the upper half portion; subsequent similar operation;
- It can be excluded so that each half of the codes; for 100 lines of code, take up to 6 steps in response to the positioning error line;
-
In practice, it may not well determine what the "middle part" of the program is that there may not be so good to check. Calculate the number of rows and whichever middle row is meaningless. In contrast, more consideration under the program which places relatively easy problems, or which places more easily be checked. Then select a checkpoint, bug probability appear before and after this break almost; ====