INF1-IP: Week 7

Lecture 12 - Collections 2: Tuples and collection operations

 

Learning outcomes
  • Explaining which values can be dictionary keys or set members
  • Creating and unpacking tuples
  • Accessing and updating two-dimensional lists
  • Selecting parts of sequences with slicing
  • Looping with enumerate() and zip()
  • Using collection methods and comprehensions to process data

 

Key terms
Click to expand.

Hash

An integer or string that identifies an object, remaining stable over an object's lifetime, with equal objects having equal hashes.

Hashability

The property of a type that determines whether it can be hashed or not. Required for dictionary keys and set members.

Hash collision

When a hash function has assigned two objects the same hash; resolved by checking the objects themselves to see whether they're equal or not.

Tuple

An immutable ordered sequence. It is hashable only if all of its items are hashable.

Unpacking

Assigning the items of a sequence to several names in one statement.

Two-dimensional list

A list whose items are lists, often used as rows of a grid or table.

Slice

A selection from a sequence, specified by start, stop, and optionally step.

Comprehension

An expression that builds a collection by transforming, and optionally filtering, items from an iterable.

 

 

New syntax
Click to expand.

hash(value)

Returns the hash of a hashable value; raises TypeError for an unhashable value.

(a, b), (a,), ()

A two-item tuple, a one-item tuple, and an empty tuple.

first, second = pair

Unpacks a two-item sequence into two names.

grid[row][column]

Selects an item in a nested list using two indices.

sequence[start:stop:step]

Selects items with an exclusive stop boundary and an optional step.

enumerate(items, start=0)

Supplies counter-and-item tuples; the counter starts at zero unless specified.

zip(first, second)

Supplies tuples of corresponding items, stopping at the shortest iterable by default.

text.split(separator), separator.join(strings)

Splits text into strings, or combines strings with a separator.

[expression for item in source if condition]

Builds a list with an optional filter. Use curly brackets for a set, or {key: value for ...} for a dictionary.

 

 

Further practice

 


 

Lecture 13 - Exception handling and floating-point values

 

Learning outcomes
  • Recognising syntax errors, runtime exceptions, and logic errors
  • Handling expected failures with try...except blocks
  • Using else and finally to separate success and cleanup
  • Throwing manual exceptions with raise
  • Explaining floating-point approximation and comparing floats with math.isclose()

 

Key terms
Click to expand.

Syntax error

Code that breaks Python's grammar and cannot be run as written.

Runtime exception

A problem reported while an operation is running.

Logic error

Code that runs but does not produce the intended behaviour.

Exception handler

An except block that responds to a matching exception.

Propagation

An exception moving back through callers until a matching handler catches it or it crashes the program.

Cleanup

Work done when an operation finishes, even if the operation failed.

Floating-point approximation

The small difference caused by representing a number with a finite binary float.

Tolerance

An acceptable difference when comparing approximate numbers, either relative to magnitude or an absolute amount.

 

New syntax
Click to expand.

try:

Begins a block containing an operation that may raise an exception.

except XError:

Handles a matching exception raised by the try block.

except XError as error:

Also gives the caught exception a name so its message can be inspected.

else: (after except)

Runs a block when try completes normally without an exception or early exit.

finally:

Runs a block as execution leaves the statement, whether or not an exception occurred. When used inside a function, runs before any return statements inside the earlier try...except branches.

raise XError(message)

Raises an XError with an explaining message.

ZeroDivisionError

An exception raised by division by zero.

math.isclose(a, b, rel_tol=..., abs_tol=...)

Compares numbers within relative or absolute tolerances, returning a Boolean.

 

Further practice

 


 

Tutorial

This week's tutorial time will be used for Assignment 2.

 


 

Assessment

Assignment 2 will take place on Friday (30 October), during tutorial time.

For details on the assignment, see Assessment > Assignment 2: Processing Data on Learn.

 


 

License
All rights reserved The University of Edinburgh