Loops in Python

Contents

Repetition

Sometimes, you want to repeat (read: iterate) an operation many times. Although you can achieve that using copy-pasting skills, sometimes it is not enough. A simple example where copy-pasting will take so much time is when we have to print the same thing 777 times. Repeated operations in programming are performed by “Loops”. There are two ways of doing loops, using “for” and “while” operations, which in my opinion are interchangeable.

Before we are going for the loops, let’s talk about the range object. The range command tells Python where to start and where to end. We can tell the loop when to end, by determining the range. For example, the number 3 has a range from 0 to 2. We can also find this out with the command: range (3)

`range(3)`

For Loop

For loop is one of the main 2 loops that we have in python programming. For example, you want to iterate the number 0-10

```#for loop example
for i in range(11): # this is when you initialized the variable i
print(i)          # this when you use it by printing it```

As we can see above, we initialized the variable “i” and it will iterate 11 times. Every time the code prints, the value of “i” increased by 1.

We can also use for loop to see and print the element of our list. As an example, I have a hand of cards and I want to show my friend my cards.

```#print all the card in my hand

cards = [2,5,7,'jack','king']
card_num = len(cards)

for i in range(card_num):
print(cards[i])```

Since combining for loop to do array operations is very often used, Python has a simpler shortcut for that.

```#shortcut for printing list
cards = [2,5,7,'jack','king']
for card in cards:
print(card)```

Since combining for loop to do array operations is very often used, Python have a simpler shortcut for that.

While Loop

The main difference between the “while” and “for” loop is that the while loop is used when we don't know when we want to loop a certain algorithm. The “while” loop will keep on executing loops until a given logical condition.

```number = 0
while (number < 11):
print(number)
number = number + 1```

To bridge our knowledge regarding for loop, we can see the code above is very similar to what we have created before. However, while loop shines when we want to search for something inside the list.

As an example, in our hand, we want to know in which position of the card is our jack.

```cards = [2,5,7,'jack','king']

i = 0
card = cards[0]

while(card != 'jack'):
print(card)
i = i + 1<<
card = cards[i]
else:
print("Jack is in the position", i+1)```

Quiz

1. Write a for loop that prints out all elements from range -7 until 7
2. Write a for loop that prints out all elements in this list=[9,4,5, ’ace’, ‘jack’]
3. You think that a proper fruit salad needs to have a banana. This is the order in which fruits are added to your salad: ingredients= ["strawberry", "apple", "pear", "orange", "blueberry", "banana", "lime", "raspberry"]. Make a while loop in which the respective fruit added to the salad is printed until the banana is added. Then print "this is a proper fruit salad".
4. You have a bad hand. To win, you want to change your card to be [10, ‘jack’, ‘queen’, ‘king’, ‘ace’]. Implement a for loop to change your hands to the winning hands!
```cards = [2,3,4,5,6]
for i in range (len(cards)):
# TODO insert your code here```

The author of this entry is XX. Edited by Milan Maushart