Lists and For loops

Introduction

This notebook covers lists and for-loops. These will prove to useful in future notebooks. Naturally this tutorial is broken up into lists, for-loops, and for-loops over lists.

Lists

A list comprises strings, numbers, and even abstract class instances. Items in a list are changeable in the sense that we can alter add, remove, or alter existing list entries. A list also permits duplicates, and thus, items in a list do not have to be unique. A given item in a list may also be another list.

Each item in a list can be referenced by its index, i.e., the order of its appearance in the list. Consider the following list

Code
import numpy as np

todays_terms = ["Bernoulli", "streamline", "streamwise", "blobs", np.pi, np.exp(1), True]
print(todays_terms[0])
print(todays_terms[-1])
print(todays_terms[2])
print(todays_terms[5])
Bernoulli
True
streamwise
2.718281828459045

As noted above, python starts counting at 0. A list has a length, corresponding to the number of items it contains. Items in a list can be removed; items can also be appended to a list.

Code
todays_terms.remove("Bernoulli")
print(todays_terms)
todays_terms.append(0.001)
print(todays_terms)
todays_terms.insert(3, "adding") # "adding" inserted in position 3
print(todays_terms)
tomorrows_terms = todays_terms.copy()
print(tomorrows_terms)
tomorrows_terms.pop(5) # Removing the 5th entry
print(tomorrows_terms)
['streamline', 'streamwise', 'blobs', 3.141592653589793, 2.718281828459045, True]
['streamline', 'streamwise', 'blobs', 3.141592653589793, 2.718281828459045, True, 0.001]
['streamline', 'streamwise', 'blobs', 'adding', 3.141592653589793, 2.718281828459045, True, 0.001]
['streamline', 'streamwise', 'blobs', 'adding', 3.141592653589793, 2.718281828459045, True, 0.001]
['streamline', 'streamwise', 'blobs', 'adding', 3.141592653589793, True, 0.001]

Below you will find an example of a list comprising instances of a class called Cards.

Code
class Cards(object):
    def __init__(self, name):
        self.name = name

my_card_1 = Cards("hearts")
my_card_2 = Cards("spades")
my_card_3 = Cards("diamonds")

my_list = [my_card_1, my_card_2, my_card_3]
print(my_list)
[<__main__.Cards object at 0x107b0ce50>, <__main__.Cards object at 0x107b0cf90>, <__main__.Cards object at 0x107b0d090>]

For loops

In python the for loop is used for iterating. However, unlike many other languages, it can iterate over a list, strings, or even other sequences (such as dictionaries and tuples). First, consider the standard use where we iterate across a range of integers.

Code
for i in range(0, 10): # iterating between 0 and 9
    print(i)
0
1
2
3
4
5
6
7
8
9
Code
for i in range(0, 10, 2): # iterating between 0 and 9, whilst skipping every 2nd entry
    print(i)
0
2
4
6
8
Code
for card in my_list: # iterating our list of cards
    print(card.name)
hearts
spades
diamonds
Code
for value in todays_terms:
    print(value)
streamline
streamwise
blobs
adding
3.141592653589793
2.718281828459045
True
0.001

These loops can also start counting in reverse, i.e.,

Code
for value in reversed(todays_terms):
    print(value)
0.001
True
2.718281828459045
3.141592653589793
adding
blobs
streamwise
streamline
Code
for j in range(10, 0, -1): # Starting at 10 iterate to 1. 
    print(j)
10
9
8
7
6
5
4
3
2
1