Additional programming techniques
Using Lists (Arrays) in Python
Lists in Python are used to store multiple items in a single variable. Lists can be one-dimensional (1D) or two-dimensional (2D), and they provide a way to organize and manipulate data efficiently.
One-Dimensional (1D) Arrays
Declaration and Initialization
Empty List:
colours = []
Fixed Length with Default Values:
colours = [None] * 5 # Creates a list with 5 `None` elements
With Values Assigned:
colours = ["Blue", "Pink", "Green", "Yellow", "Red"]
Access and Assignment
- Accessing Elements:
print(colours[2])
# Output: Green - Assigning Values:
colours[3] = "Orange"
# Changes Yellow to Orange
Example
colours = ["Blue", "Pink", "Green", "Yellow", "Red"]
print(colours[2]) # Output: Green
colours[3] = "Orange"
When outputting all elements within an array a for loop is used
for i in range(len(colours)):
print(colours[i])
This will output each element on a seperate line
Blue
Pink
Green
Orange
Red
Two-Dimensional (2D) Arrays
A 2d array is one were each element is an array in itself.
below the first element is gameboard[0]
gameboard = [
["Pawn", None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
# ... other rows ...
]
That is the row
["Pawn", None, None, None, None, None, None, None]
Pawn
is in position 0
in this array
Therefore gameboard[0][0]
is how we access Pawn
I could assign a new element into this array
gameboard[1][0] = "Knight"
gameboard is now equal to
gameboard = [
["Pawn", None, None, None, None, None, None, None],
["Knight", None, None, None, None, None, None, None],
# ... other rows ...
]
Declaration and Initialization
With Values Assigned:
gameboard = [
["Pawn", None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
# ... other rows ...
]
Accessing Elements in a 2d array with a loop
Lets setup our gameboard so it matches an actual Chess board during the initial setup.
gameboard = [
["Castle", "Bishop", "Knight", "King", "Queen", "Knight", "Bishop", "Castle"],
["Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn"],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
["Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn"],
["Castle", "Bishop", "Knight", "King", "Queen", "Knight", "Bishop", "Castle"],
]
print("Initial gameboard:")
# accesses the rows
for i in range(8):
#accesses the columns
for j in range(8):
# prints out the gameboard row by row
print(gameboard[i][j])
Now our nested for loop will give us all of the pieces on the board. But to make it more useable we can use a few tricks
Firstly we can provide the coordinate of each position using the i
and j
values of the loop
print("Row "+str(i)+": Col "+str(j)+" = "gameboard[i][j])
gameboard = [
["Castle", "Bishop", "Knight", "King", "Queen", "Knight", "Bishop", "Castle"],
["Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn"],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
["Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn", "Pawn"],
["Castle", "Bishop", "Knight", "King", "Queen", "Knight", "Bishop", "Castle"],
]
print("Initial gameboard:")
# accesses the rows
for i in range(8):
#accesses the columns
for j in range(8):
# prints out the gameboard row by row
print("Row "+str(i)+": Col "+str(j)+" = "gameboard[i][j])
Even better would be to use the python built in , end=" "
which stops a new line being printed. But then using the end of the row loop to print a new line.
# accesses the rows
for i in range(8):
#accesses the columns
for j in range(8):
# prints out the gameboard row by row
print(gameboard[i][j], end=" ")
# new line once row finished
print()