# Measure some strings using a for loop
= ['cat', 'window', 'defenestrate']
words for w in words:
print(w, len(w))
cat 3
window 6
defenestrate 12
Loops are a very important part of Python, all languages as a whole.
for
loops are covered in most languages, in Python the same applies as well.
# Measure some strings using a for loop
words = ['cat', 'window', 'defenestrate']
for w in words:
print(w, len(w))
cat 3
window 6
defenestrate 12
A for loop in programming is a control structure that allows the repeated execution of a set of statements for each item in a sequence, such as elements in a list or numbers in a range, enabling efficient iteration and automation of tasks. for loops are useful when we know exactly the number of iterations we require. If the number is unknown, use while loop
in
to check the condition of the for loopAll for loop conditions apply when using range EXCEPT:
When using a range the loop stops one short of the last value. What does that mean? It means it does NOT include the last number. Look at the example below:
range(0, 10)
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
x = 11
What’s important is to note that even though the maximum value of the range is 12 the actual range is 12-1=11
range(x,y)
x = 0
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 8
x = 9
x = 10
range( x, y, incr)
if you remember indexing starts with the first object being [0], so a[0] = ‘Mary’ and a[4]= ‘lamb’
len(a)
= 5
range(len(a)) = range(5) = range(0,5)
it means we will have 5-0= 5 iterations, starting with 0 and ending at 4
Consider this example:
fruits = ["apple", "banana", "orange"]
for index, fruit in enumerate(fruits):
print(f"At position {index}, I found a {fruit}")
At position 0, I found a apple
At position 1, I found a banana
At position 2, I found a orange
Here is another one:
a = ['Mary', 'had', 'a', 'little', 'lamb']
for x, i in enumerate(a):
print(f"We are at position {x}, and the word is {i}")
We are at position 0, and the word is Mary
We are at position 1, and the word is had
We are at position 2, and the word is a
We are at position 3, and the word is little
We are at position 4, and the word is lamb
If you haven’t used while before, think of while as an until loop. while loop will repeat itself while the condition is met. It’s like saying “keep dancing till the music stops”. while loops are useful when the number of iterations is unknown, and will continue till a condition is met
while condition: \n execute code
don’t forget the indentationImportant: whatever counter you are using it needs to be incremented otherwise you’ll get stuck in an infinite loop
If you ever catch yourself in an infinite loop, use CTRL+C to stop the script from running
for
loop the prints out all the element between -5 and 5 using the range function.rock
R&B
Soundtrack
R&B
soul
pop
Multiplication table of 6:
6* 0 = 0
6* 1 = 6
6* 2 = 12
6* 3 = 18
6* 4 = 24
6* 5 = 30
6* 6 = 36
6* 7 = 42
6* 8 = 48
6* 9 = 54
Multiplication table of 7:
7* 0 = 0
7* 1 = 7
7* 2 = 14
7* 3 = 21
7* 4 = 28
7* 5 = 35
7* 6 = 42
7* 7 = 49
7* 8 = 56
7* 9 = 63
PlayListRatings
.PlayListRatings
is given by: PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]
while
loop to copy the strings 'orange'
of the list squares
to the new list new_squares
'orange'
squares = ['orange', 'orange', 'purple', 'blue ', 'orange']
new_squares = []
i = 0
while(i < len(squares) and squares[i] == 'orange'):
new_squares.append(squares[i])
i = i + 1
print (new_squares)
['orange', 'orange']
while
loop to search for animals made up of 7 lettersNew