Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
2388b65
Intro and virtual environments slides
IgorTatarnikov Sep 18, 2025
9fd778d
WIP basics, working with python
IgorTatarnikov Sep 19, 2025
6656c2e
Done variables and lists
IgorTatarnikov Sep 23, 2025
a45e05a
WIP dicts
IgorTatarnikov Sep 23, 2025
472ce16
Added ipynb checkpoints to gitgnore
IgorTatarnikov Sep 23, 2025
6bb8c74
WIP list comprehension
IgorTatarnikov Sep 23, 2025
8c112c6
Update list comprehension
IgorTatarnikov Sep 23, 2025
b6df5de
Fix md formatting
IgorTatarnikov Sep 23, 2025
462e4e7
Add placeholer first_script slides
IgorTatarnikov Sep 23, 2025
e26bb62
WIP loading and saving
IgorTatarnikov Sep 23, 2025
25c1f32
WIP functions
IgorTatarnikov Sep 24, 2025
297aeaa
Fix merge conflicts
IgorTatarnikov Sep 26, 2025
a551c98
Add matplotlib to requirements
IgorTatarnikov Sep 26, 2025
57bfce9
Swap virtual envs
lauraporta Sep 26, 2025
b0372fb
Ignore generated example files
lauraporta Sep 26, 2025
8ce5be1
Positional and default args
IgorTatarnikov Sep 26, 2025
a88e469
Merge branch 'transfer-google-slides' of https://github.com/neuroinfo…
IgorTatarnikov Sep 26, 2025
9196319
Ignore vscode files
lauraporta Sep 26, 2025
35ee703
Include more incremental / fragments; add logos and render them nicely
lauraporta Sep 26, 2025
ab520dc
WIP functions
IgorTatarnikov Sep 26, 2025
fc3e07f
Merge branch 'transfer-google-slides' of https://github.com/neuroinfo…
IgorTatarnikov Sep 26, 2025
ecc6d7f
Shuffling order
lauraporta Sep 26, 2025
c931a30
Add incremental marks
lauraporta Sep 26, 2025
3d4e0c3
Classes and objects
IgorTatarnikov Sep 26, 2025
beabee0
Add f-strings and <br/>
lauraporta Sep 26, 2025
dadfb19
Add *args **kwargs and <br/>
lauraporta Sep 26, 2025
5674de3
Expand on IDEs
lauraporta Sep 26, 2025
ebc7883
Start to work on venv
lauraporta Sep 26, 2025
ebc948b
WIP errors and exceptions
IgorTatarnikov Sep 26, 2025
67303c3
Merge branch 'transfer-google-slides' of https://github.com/neuroinfo…
IgorTatarnikov Sep 26, 2025
d6fd38e
WIP handling exceptions
IgorTatarnikov Sep 26, 2025
50c0380
WIP windows problem
lauraporta Sep 26, 2025
4542d82
Change to light mode
IgorTatarnikov Sep 26, 2025
c6e6e71
Errors and exceptions
IgorTatarnikov Sep 26, 2025
f4668a6
Errors and exceptions
IgorTatarnikov Sep 26, 2025
3c6e541
Update functions slides
IgorTatarnikov Sep 28, 2025
f09b95b
Small clean up in basics.qmd
IgorTatarnikov Sep 28, 2025
8a78d9b
WIP structuring your code
IgorTatarnikov Sep 28, 2025
888655d
Structuring your code
IgorTatarnikov Sep 28, 2025
c78cd9d
Remove temp func files
IgorTatarnikov Sep 28, 2025
3cb8632
Documenting slides
IgorTatarnikov Sep 28, 2025
916629c
Next steps
IgorTatarnikov Sep 28, 2025
69b482a
Ignore csv output
IgorTatarnikov Sep 28, 2025
dba111f
Add pandas and numpy to requirements
IgorTatarnikov Sep 28, 2025
5d5df2d
Minor clean up
IgorTatarnikov Sep 28, 2025
9d14010
Modules and packaging
IgorTatarnikov Sep 28, 2025
277301a
Change f-string examplese
lauraporta Sep 29, 2025
c18918f
Revert to simpler example to write a csv
lauraporta Sep 29, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,14 @@ index_files/
build/
.DS_Store

.ipynb_checkpoints

# IDE files
.idea/

# Files generated by Python snippets
example.txt
out.csv

# Vscode files
.vscode/
Binary file added img/github-mark-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions img/github-mark-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/github-mark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions img/github-mark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions img/matplotlib_logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/miniforge_window_add_to_path.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions img/numpylogoicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions img/pandas_logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/python-ecosystem.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/python_environment.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions img/pytorch_logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/scikit-image_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/scipy_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
241 changes: 188 additions & 53 deletions index.qmd
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
title: A template presentation
subtitle: so much fun
author: SWC Neuroinformatics Unit
title: Introduction to Python
author: Igor Tatarnikov, Laura Porta
execute:
enabled: true
format:
revealjs:
theme: [default, niu-dark.scss]
logo: img/logo_niu_dark.png
footer: "Edit this footer | 2023-07-26"
theme: [default, niu-light.scss]
logo: img/logo_niu_light.png
footer: "Sainsbury Wellcome Centre | 2025-09-30"
slide-number: c
menu:
numbers: true
Expand All @@ -17,6 +16,7 @@ format:
preview-links: false
view-distance: 10
mobile-view-distance: 10
from: "markdown+emoji"
auto-animate: true
auto-play-media: true
code-overflow: wrap
Expand All @@ -26,9 +26,9 @@ format:
fontFamily: arial
curve: linear
html:
theme: [default, niu-dark.scss]
logo: img/logo_niu_dark.png
date: "2023-07-05"
theme: [default, niu-light.scss]
logo: img/logo_niu_light.png
date: "2025-09-30"
toc: true
code-overflow: scroll
highlight-style: atom-one
Expand All @@ -39,73 +39,208 @@ format:
margin-left: 0
embed-resources: true
page-layout: full
my-custom-stuff:
my-reuseable-variable: "I can use this wherever I want in the markdown, and change it in only once place :)"
---

## Contents
## Schedule {.smaller}

::: {.incremental}
* Aims
* Why learn Python?
* Basics
+ Variables
+ Data types
+ Loops
+ Conditional statements
+ List comprehensions
* How to work with Python? (using IDEs, environments, etc...)
* Writing your first Python script
* Loading and saving data
:::

Some example slides - [also look at example RevealJS slides in the Quarto docs](https://quarto.org/docs/presentations/revealjs/)
## Schedule {.smaller}
::: {.incremental}
* Recap and Q&A
* Using third party libraries from pip and conda
* Functions and methods
* Classes and objects
* Errors and exceptions
* Organising your code
* Documenting your code
:::

* Non-executable and executable code-blocks
* bullet points with highlighting
* two-column slides
* how to include a slide from a separate MD file
* preview and link to a webpage
## Aims
::: {.incremental}
* Introduce Python
* Introduce some basic programming concepts
* Show you *one* (our) way of doing things
+ Not necessarily the best or only way
* Give you the confidence to start doing it yourself
* Prepare you for other courses
:::

## Just a code block, nothing gets executed...
::: {.fragment}
Please ask any **questions at any time**!
:::

... but there is some fancy highlighting
# Why learn Python?

```{.python code-line-numbers="1|3|4-9"}
from pathlib import Path
## Why learn Python? {.smaller}
::: {.fragment}
Popularity

home_path = Path.home()
if home_path.exists():
data_path = home_path / "data"
else:
pass
# raise some error maybe?
```
- Python is the most popular programming language for data science[$^1$](https://aimresearch.co/product/data-science-skills-study-2023/)
:::

## A code block that's actually executed at render-time
::: {.fragment}
Free and open source

```{python}
#| echo: true
#| code-fold: true
- Unlike MATLAB, IGOR, etc...
- Anyone can use your code
+ 9.7M open Python repositories[$^2$](As 2024-04-06, github.com/oprogramador/github-languages) on ![GitHub logo](img/github-mark-white.svg){width=1em}
:::

from pathlib import Path
::: {.fragment}
Versatile
Not just for data analysis / machine learning

print("Hello world")
```
- Visualisation
- Web development
- Data acquisition
:::

## You can execute code without showing that you have by using #|echo: false
```{python}
#| echo: false
## Why learn Python? {.smaller}
Packages for everything!

from pathlib import Path
::: {.fragment}
![NumPy logo](img/numpylogoicon.svg){width=2em style="margin-bottom: 0px; vertical-align: bottom;"} Numpy: arrays
:::

print("Hello world")
```
::: {.fragment}
![Pandas logo](img/pandas_logo.svg){width=2em style="margin-bottom: 0px; vertical-align: bottom;"} Pandas: dataframes
:::

::: {.fragment}
![SciPy logo](img/scipy_logo.png){width=2em style="margin-bottom: 0px; vertical-align: bottom;"} SciPy: scientific computing
:::

::: {.fragment}
![Scikit-image logo](img/scikit-image_logo.png){width=2em style="margin-bottom: 0px; vertical-align: bottom;"} Scikit-image: image analysis
:::

::: {.fragment}
![PyTorch logo](img/pytorch_logo.svg){width=2em style="margin-bottom: 0px; vertical-align: bottom;"} PyTorch: machine learning
:::

::: {.fragment}
![Matplotlib logo](img/matplotlib_logo.svg){width=2em style="margin-bottom: 0px; vertical-align: bottom;"} Matplotlib: plotting
:::

::: {.fragment}
*People have spent lots of time optimising these packages! Don't reinvent the wheel!*

:::

# Basics of Python

{{< include slides/basics.qmd >}}

# Writing your first Python script

{{< include slides/first_script.qmd >}}

# Loading and saving data

{{< include slides/loading_and_saving.qmd >}}

# Functions

{{< include slides/functions.qmd >}}

# Classes and objects

{{< include slides/classes_and_objects.qmd >}}

# Errors and exceptions

{{< include slides/errors_and_exceptions.qmd >}}

{{< include slides/extra_slide.qmd >}}
# How to work with Python?

## An example image
{{< include slides/working_with_python.qmd >}}

Include an image:
# Virtual environments

![](img/bg_logo_wide.png){width=900 fig-align=center}
{{< include slides/virtual_environments.qmd >}}

# Modules and packages

## Link and a preview a webpage:
{{< include slides/modules_and_packages.qmd >}}

::: {style="text-align: center; margin-top: 1em"}
[interoperable Python-based tools for computational neuroanatomy](https://brainglobe.info/index.html){preview-link="true" style="text-align: center"}
# Organising your code

{{< include slides/structuring_your_code.qmd >}}

# Documenting your code

{{< include slides/documenting_your_code.qmd >}}

# Next steps
## Next steps {.smaller .incremental}
* We've covered some of the basics
* Next steps -- Practice!:
+ Mess around with code
+ Solve problems
+ Google stuff
+ Contact
+ Additional courses
:::

## Further resources {.smaller .incremental}
* [CS50](https://pll.harvard.edu/course/cs50-introduction-computer-science)
* UCL ARC courses
+ [An introduction to programming for research using Python](https://rits.github-pages.ucl.ac.uk/doctoral-programming-intro/)
+ [Research software engineering with Python](https://github-pages.ucl.ac.uk/rsd-engineeringcourse/)
+ [Software carpentry courses](https://software-carpentry.org/lessons/)
+ [Exercism](https://exercism.org/tracks/python)

## Troubleshooting tips {.smaller .incremental}
* Make sure the correct environment is activated
+ `which python` or `which pip` to check
* Check the scope of your variable
* Pay attention to whether an operation is `in_place` or not
+ Especially with `pandas` and `numpy`
* Structure your code as a package as soon as you can
+ `pyproject.toml` → more on this in future sessions!
* Look out for deep vs shallow copies
+ Especially with collections (lists, dicts, sets)

## Question
::::: {.columns}
::: {.column .incremental width="55%"}
* What does this return?
:::

::: {.column width="45%"}
:::: {.fragment .fade-in}
```{python}
#| echo: true
#| output-location: fragment
a = [1, 2, 3]
b = a
b[0] = 42

print(a[0])
```
::::
:::
:::::

## Use a variable several times

Variables defined in the metadata is re-useable anywhere
## Troubleshooting tips {.smaller .incremental}
* Read the error messages!
+ Google is your friend
+ LLMs can help too
+ Ask a colleague or contact us
* Use a debugger (e.g. `pdb`, or IDE built-in debuggers)
* Write tests for your code (e.g. using `pytest`)

* {{< meta my-custom-stuff.my-reuseable-variable >}}
* {{< meta my-custom-stuff.my-reuseable-variable >}}
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
jupyter
jupyter-cache
matplotlib
numpy
pandas
Loading