Chapter 3 · Data

Tuples, loops and grids

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

A tuple is a fixed group

In this lesson you group values in tuples, stop loops early, and build grids from lists of lists.

point = (3, 4)
  • A tuple is values in round brackets: a fixed group. point[0] is 3 and len(point) is 2, as with a list.
  • A tuple cannot change. point[0] = 5 stops with TypeError: 'tuple' object does not support item assignment.
  • A tuple of one value needs a comma: (5,). (5) is only the number 5.
point = (3, 4)
point[0] = 5
print(point)
print((5,), (5))
def min_max(xs):
    return min(xs), max(xs)


a, b = 1, 2
a, b = b, a
print(a, b)
lo, hi = min_max([7, 2, 9])
print(lo, hi)
print(min_max([7, 2, 9]))
names = ["Ada", "Alan", "Grace"]
scores = [91, 85, 78]
for place, name in enumerate(names, start=1):
    print(place, name)
for i, name in enumerate(names):
    print(i, name)
for name, score in zip(names, scores):
    print(f"{name:<6}{score:>4}")
def low_high(xs):
    # the lowest and the highest, in one loop
    return 0, 0


low, high = low_high([7, 2, 9, 4])
print(low, high)
print(low_high([5]))
nums = [4, 0, 7, -2, 5, -9]
found = None
for i, n in enumerate(nums):
    if n == 0:
        continue
    if n < 0:
        found = i
        break
    print("checked", n)
if found is None:
    print("none")
else:
    print("first negative at", found)
scores = [72, 65, 48, 90, 30]
# find the first score under 50 and its place
for r in range(1, 4):
    for c in range(1, 5):
        print(f"{r * c:>4}", end="")
    print()
n = 4
for i in range(1, n + 1):
    # print the numbers 1 to i, with end=""
    print()
board = []
for r in range(3):
    row = []
    for c in range(3):
        row.append(".")
    board.append(row)
board[1][0] = "X"
for row in board:
    for cell in row:
        print(cell, end=" ")
    print()
print(board[1][0], len(board), len(board[0]))
grid = [[0] * 3] * 3
grid[0][0] = 1
print(grid)
grid = []
# 1. build 4 rows of 4 numbers
# 2. print each row, each number in 3 columns
print(len(grid), "rows,", len(grid[0]), "columns")
board = [
    ["X", "O", "O"],
    ["O", "X", "O"],
    ["X", "X", "X"],
]
# find the first row that is all "X"
board = [
    ["X", "X", "O"],
    ["O", "X", "O"],
    ["X", "X", "O"],
]
wins = []
# one True or False per column
print(wins)
Terminal
Step 1 of 13Try it: a new point

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)