2026 Apr 2nd – UQ PUG 27

Welcome to UQ Python User Group! Check out our general info for how we work and what we do. Below you’ll find the details of this month’s gathering.

Overview

Welcome to the April UQ PUG! Today we continue our 9-month series through The Python Tutorial.

This month, we’ll look at sections 4.1-4.7: More Control Flow Tools.

You can also add questions to our interactive document.

Structure

  1. We start today by adding our names to the table below
  2. Add your questions to this page
  3. This month’s presentation
  4. Finally, we spend the rest of the session answering the questions you’ve brought!

Teams

If you’d like to receive regular updates and post questions during the month, join our Teams channel!

Training Resources

We offer Python training sessions and resources, you can find our introductory guide here.

Introduce yourself

What’s your name? Where are you from? Why are you here?
Name Organisation/School Reason for coming
Cameron Library + SMP Teach and help out with all things Python
Damchen Master of Data Science Wanted to learn Python more
Νayomi Medicine Refresh myself with the python basics to use python in image data analysis
Irene Health and Rehabilitation Science learn Python for future data analysis and research (eg machine learning)
Zhuan Yi Neoh QAAFI Using python for data analysis and statistics analysis
Steph Library Here to learn, and help if I can :)

Questions

If you have any Python questions you’d like to explore with the group, please put your question and name in the sections below.

If you think you can help, feel free to contribute to the answers section!

if…, elif… without else?

Can we use an if and elif without else?

# Yes! 

x = 1
if x == 2:
    print("x=2")
elif x == 1:
    print("x=1")
x=1

Modifying iterable during for loop

Why is the iterable of a for loop modifyable?

Answers

It’s because, behind the scenes, a for loop converts the iterable into an iterator, and then calls next(...) on that iterator at the end of each iteration.

Best illustrated with an example.

Take the following loop:

nums = [1,2,3]
for i in nums:
    print(i)
1
2
3

Behind the scenes, it

  1. Makes an iterator out of nums with iter(nums)
  2. Makes i the first value in the iterator with next(<iterator>)
  3. Runs one iteration of the loop
  4. Makes i the next value in the iterator with next(<iterator>) and runs the next iteration
  5. Continues the run-next loop until next(<iterator>) yields a StopIteration error

Writing this out manually,

nums = [1,2,3]
it = iter(nums) # 1.

# First iteration
i = next(it)
print(i)

# Second iteration
i = next(it)
print(i)

# Third iteration
i = next(it)
print(i)

# Fourth iteration (nothing left in nums)
i = next(it)
1
2
3
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
Cell In[4], line 17
     14 print(i)
     16 # Fourth iteration (nothing left in nums)
---> 17 i = next(it)

StopIteration: 

When you actually run the loop, instead of printing the StopIteration error it quits the loop.

Workflows

Advice for a good workflow of using Python (i.e. a flowchart of steps for data science, and how to make sure code is right)

Answers

We had a bit of a discussion with a few ideas.

A diagram displaying the data science cycle: Import -> Tidy -> Understand (which has the phases Transform -> Visualize -> Model in a cycle) -> Communicate. Surrounding all of these is Program.
some_project_folder/
    data/
        data_1.csv
        data_2.csv
    plots/
        plot_1.png
        plot_2.png
    analysis_1.py
    analysis_2.py
    common_functions.py
  • Note the common_functions.py file: for large projects with analyses that reuse code, it can be helpful to move repeated sections into a common file which you then import with import common_functions