Professional Documents
Culture Documents
function
Assignment 4
Q2 Speech analysis
-
Example use
nums = [10, 20, 30, 40, 50, 60, 70]
suite
for k in nums:
print(k)
In the for loop version loop variable k automatically iterates over the provided
sequence of values:
For k in nums:
Print(k)
Three things every loop must have
1) Starting point
2) Increment
3) Stopping condition
WHILE loop
Programmers defined
loop variable
Programmer defined
Condition
FOR loop
First element in sequence
1 element at a time
Number of elements in a
sequence
Sometimes you iterate until you find something, not interested in all
elements
More interested in the condition
Range Function
Range( 0, 5 )
0,1,2,3,4 (Does not include the upper limit)
You can also define range() with one parameter
Range( 5 ) = range( 0, 5)
1-10
Range() does not return a list
Actually range does not create a sequence of integers, it creates a generator
function able to produce each next item of the sequence. This saves memory,
especially for long lists.
Therefore, typing range(0,9) in the python shell does not produce a list as expected
it simples echoes out the call to range