Python Study Notes (4)
While · general syntax
While control condition: execution statement
The statement is executed until the control condition is false ".
· Infinite Loop
While True: execution statement
Programs that can be executed continuously are very dangerous. We will only see them on certain occasions. For example, some servers need to monitor whether a client is connected at any time. These clients may send requests to the server at any time, so the server's listener must be on standby.
· Counting cycle
This is the most common statement. It uses counters to determine when the loop ends.
Count = 0; while count <10: print count + = 1
The range () function requires one, two, or three parameters.
Range (start, end, step)
If step is 1, it can be omitted. There are two parameters.
If there is only one parameter, start starts counting from 0 by default.
The len () function returns the number of elements in a sequence.
For Loop
Movies = ["Avata", "Xman", "Titannic", "The Dark Knight Rises", "Jurassic Park", "Dances with Wolves"] for movie in movies: print moviefor item in range (len (movies): print item, ":", movies [item]
Like the C language, break breaks out of this loop, while continue does not continue to execute the following code.
For I in range (10): if I = 6: break print I, for I in range (10): if I = 6: continue print I,