Module 4 - Section 0 - Repeat, repeat, repeat!

Site: ΕΛ/ΛΑΚ Moodle
Course: Python Lab
Book: Module 4 - Section 0 - Repeat, repeat, repeat!
Printed by: Guest user
Date: Tuesday, 18 June 2024, 1:40 AM


This section is a repetition of the previous modules

4.0.1 - List methods

Let's review some of the list methods again. Execute the following commands in Thonny CLI one after the other :

>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']

>>> fruits.count('apple')

>>> fruits.count('tangerine')

>>> fruits.index('banana')

>>> fruits.index('banana', 4)  # Find next banana starting a position 4

>>> fruits.reverse()

>>> fruits

>>> fruits.append('grape')

>>> fruits

>>> fruits.sort()

>>> fruits

>>> fruits.pop()

4.0.2 - Items in a list

When we created a list of 20 different numbers (kino) in previous lesson (3.2.5) we asked you if the numbers in the list were unique. The answer is NO because when we add random numbers we can add more than once the same number.

So if you have saved the kino program open it or else go to 3.2.5,  copy the code from there and save it as kino.

After executing the code look at the output find a number and write the commands as in the picture.

kino test

With in command we can search inside a list.

Copy and paste the following code to Thonny and save it as kino2 :

Compare it with the previous code of kino program. Are there 20 unique numbers now?

4.0.3 - Range is the guide

Using loops in computer programming allows us to automate and repeat similar tasks multiple times. 

A for loop implements the repeated execution of code based on a loop counter or loop variable. This means that for loops are used most often when the number of iterations is known before entering the loop, unlike while loops which are conditionally based.

Let’s look at a for loop that iterates through a range of values. Write the following commands in the CLI area of Thonny. After writing every command press the Enter key.

>>>  for i in range(0,5):

When we run this program, the output looks like this:


This for loop sets up i as its iterating variable, and the sequence exists in the range of 0 to 5(not printing).

Then within the loop we print out one integer per loop iteration. Keep in mind that in programming we tend to begin at index 0, so that is why although 5 numbers are printed out, they range from 0-4.

Another example, using start, stop and step is:

>>>for i in range(0,15,3):

In this case, the for loop is set up so that the numbers from 0 to 15(not printing) print out, but at a step of 3, so that only every third number is printed, like so:


We can also use a negative value for our step argument to iterate backwards, but we’ll have to adjust our start and stop arguments accordingly:

>>> for i in range(100,0,-10):

Here, 100 is the start value, 0 is the stop value, and -10 is the range, so the loop begins at 100 and ends at 0(not printing), decreasing by 10 with each iteration. We can see this occur in the output: