Introduction to Coding and Data Analysis for Scientists

Week 3: Loops

Today’s Lecture

  • Lecture 3: Loops
    • Recap: Accessing Noteable
    • Recap: If Statements
    • For Loops
    • While Loops
    • Practical

Recap: Accessing Noteable

  • Open Blackboard
  • Go to Introduction to Coding and Data Analysis for Scientists 2025
  • Click Unit Information and Resources
  • Open Noteable
    • Make sure Jupyter Classic (Legacy) is selected.
    • Click Start
  • Click +GitRepo
  • Paste into Git Repository URL: git@github.com:TomMaullin/SCIF10002-2025.git
  • Press clone

Welcome page for the unit on Blackboard

Important: Assessed Coursework 1

  • The first assignment will be released this week!
    • Released Wednesday 8th October at 12:00PM
    • Due Wednesday 22nd October at 12:00PM
  • This is assessed (15% of your grade!)
  • Submission is via Noteable
  • You can submit as many times as you like up until the submission date

Noteable Submission

  • Download using the Assignments tab on noteable
  • Press Fetch and click on the assignment
  • Fill your answers in and Validate
  • Once done, save your answers and Submit
  • Feedback will be available when you see the (view feedback) option

Recap: If Statements

  • Last week, we looked at the if statement
  • if statements allow us run code only when a specific condition is True
  • We can add elif and else statements to give us finer control over when code is executed
    • An elif runs if it’s Boolean is True and all previous clauses (ifs and elifs) did not execute
    • An else runs if all other clauses did not execute
  • Today, we will look at another feature of Python that gives us finer control over our code - loops

Motivation

  • Suppose we want to do a very repetitive computation
  • For instance, suppose we want to print the square of each item in this list
  • We could just write something like this…

Motivation

  • But it would be nice to be able to do this automatically!
  • This is what loops do!

Iterable Objects

  • An iterable object is an object which contains lots of elements that we could list one by one
  • We’ve already seen an example - the list
  • Other examples include:
    • Tuples - like lists but use round brackets and can’t be changed
    • Dictionaries - records “keys” and “values” so that we can look up data using labels of our choosing
    • A range object - a python object that represents a sequence of numbers, saved in a useful, memory-efficient way
  • A for loop performs an operation on each element in an iterable object

For Loops

  • A for loop lets you run code for every element in an iterable object
  • Let’s look at an example:

Translation:

“For every number in the list, print that number”

While Loops

  • A while loop lets you run code repeatedly so long as a Boolean statement is True

Let’s compare this to an if statement - An if statement executes once if the Boolean is True - An while loop executes repeatedly until the Boolean is False

Practical