Using Python with seismic data
Lesson 4: Iteration and lists 

Iteration is how programs repeat a task over and over. Definite iteration is where the number of repetitions is defined in advance and is performed using a for or a while loop. 

Python can iterate through a range of numbers, or through a list.

Graphical user interface, text, application

Description automatically generated

The range() function
The range() function creates a sequence of integers. range(10) produces ten whole numbers from 0 to 9. 
Python uses 0-based numbering, by default counting starts from 0.
However, range(1, 12, 2) produces the sequence 1, 3, 5, 7, 9, 11, the syntax is range(start, stop, step).

Lists
A list is a changeable collection of data. Lists are defined using square brackets, so mylist=[] creates an empty list.
seislist=['RB30C','RB5E8'] produces a list with two items. An index number is used to refer to an item in a list seislist[0] is 'RB30C', seislist[1] is 'RB5E8'.

 

While

While loops can be used for definite iteration using a counter or some other Boolean expression.
The while loop tests a condition at the start and the loop only runs if that condition is met, for example the following code prints out the numbers 0 to 9:

 

counter = 0

while counter < 10:

    print("The counter is: " + str(counter))

    counter += 1

 

A while loop can also be used to add leading zeros, to pad a string.

 

mynum = input("Enter a whole number: ")

while len(mynum) < 5:

    mynum = "0" + mynum

print(mynum)

 

This code asks the user to enter a number, which is stored in the string variable, mynum.
If mynum has 5 or more characters, the while loop will not run, but if the length of the string is less than 5 characters,
leading zeros are added until the string has a length of 5, when the loop ends and the string is printed.
It is often helpful for strings to be formatted with the same length. 

 

Data validation and indefinite iteration

While loops are useful for data validation, so for example the following code could be used to check that a positive integer is entered.
The code will repeat until a positive whole number is entered.
This is an example of indefinite iteration, the number of repeats is not pre-determined before the code runs. 

 

mynum = "number"

while not mynum.isnumeric():

    mynum = input("Please enter a positive whole number: ")

print("Your number is: " + mynum)

 

In this case, str.isnumeric() is a function that is performed on a string variable, which tests that all of the characters in the string are only from the range 0 to 9.
This is a test that is used on a string. Once this has been established, then casting can be performed using int() to convert the numbers in the string into an integer.
Without first testing that the string contains numbers, it is possible to try converting a string which contains letters and this would result in an error and cause the program to crash.

 

A different method will be needed to test for floating point numbers.


Links
The post on Twitter: https://twitter.com/wmvanstone/status/1251962656795373568

The code on GitHub: https://github.com/wmvanstone/LearnPythonForObspy

The obspy tutorial: https://docs.obspy.org/tutorial/


Lesson 3: python03.html

Lesson 5: python05.html


Links: Links


Home: Index