2Foundational Skills 2.1Python Basics
2.1.1 Python Fundamentals
Learn the core of Python with one free course, and see the handful of ideas you will use in every IOAI task.
Why Python?
Every IOAI task comes as a Python notebook, and every library you will use, from pandas to scikit-learn to PyTorch, is a Python library. So before any machine learning, you need to be comfortable reading and writing basic Python.
The good news: you do not need to become a software engineer. IOAI code is usually short. You load some data, loop over it, call a few library functions and print the results. The contest even gives you a small LLM for help with code (see the LLM usage policy). But the LLM cannot think for you. You still have to read its code, spot what is wrong, and change it. That is exactly what this module prepares you for.
If you have not installed Python yet, do Setting Up Python first. Or skip installing for now: the main course below runs in your browser.
What you need to know
By the end of this module, you should be comfortable with these ideas:
| Idea | In one line |
|---|---|
| Variables and types | Store a value under a name; numbers, text and True/False behave differently. |
| Lists | Keep many values in order and pick them out by position. |
| Loops | Repeat the same steps for every item in a list. |
| Conditions | Do one thing or another depending on a test. |
| Functions | Give a piece of code a name so you can reuse it. |
| Strings | Work with text: join it, split it, format numbers inside it. |
| Dictionaries | Look a value up by a key, such as a word or a label. |
| Libraries | Use code other people wrote, with import. |
| Error messages | Read what Python tells you when something breaks. |
That list is shorter than it looks, and every course below covers all of it.
Pick one main course
There are hundreds of Python courses online. Do not collect them. Pick one, finish it, and do its exercises. Here are the ones we recommend, and who each suits.
Our pick: Kaggle Learn Python
Kaggle Learn Python has seven short lessons. Each lesson is a notebook to read, followed by an exercise notebook where you write the code yourself and check it instantly. Everything runs in your browser; you only need a free Kaggle account. It is also written by data scientists, so the examples already feel like the work you will do at IOAI.
Do the lessons in order. The table shows where to read more if a lesson feels too fast.
| Kaggle lesson | What it covers | If you want more |
|---|---|---|
| 1. Hello, Python | Variables, numbers, print |
CS50P week 0 |
| 2. Functions and Getting Help | Calling and writing functions, help() |
CS50P week 0 |
| 3. Booleans and Conditionals | True/False, if, elif, else |
CS50P week 1 |
| 4. Lists | Indexing, slicing, changing lists | Python tutorial, section 5.1 |
| 5. Loops and List Comprehensions | for, while, list comprehensions |
CS50P week 2 |
| 6. Strings and Dictionaries | Text methods, f-strings, dictionaries | Python tutorial, section 5.5 |
| 7. Working with External Libraries | import, using other people's code |
CS50P week 4 |
For more depth: CS50P
Harvard's CS50P is a full university course, free online. Each week has a lecture video, written notes and a problem set. It goes slower and deeper than Kaggle, and it is a great choice if you have never programmed before or if you like learning from lectures.
For this module, weeks 0 to 4 are enough: Functions and Variables, Conditionals, Loops, Exceptions, and Libraries. Week 6 (File I/O) is useful later, when you start reading data files. Week 8 (Object-Oriented Programming) belongs to the next module.
If you prefer something else
- A book: Automate the Boring Stuff with Python is free to read online. Chapters 1 to 8 cover this module, and Chapter 5 is a friendly introduction to debugging.
- Lots of small exercises: the University of Helsinki's Python Programming MOOC checks your answers automatically. Parts 1 to 4 are plenty for now.
- One long video: freeCodeCamp's Learn Python: Full Course for Beginners gives you a quick overview in one sitting. Follow it with one of the courses above, because watching is not the same as writing code.
A quick tour
Here is what the ideas look like in code. You do not need to understand every line yet; come back after your course and check that you can. Try typing the examples into a notebook and changing them.
Variables and types
name = "Asha"
age = 16
accuracy = 0.87
passed = True
print(type(age), type(accuracy), type(name), type(passed))
# <class 'int'> <class 'float'> <class 'str'> <class 'bool'>
print(f"{name} is {age} and scored {accuracy:.0%}")
# Asha is 16 and scored 87%
The f before the quotes makes an f-string: anything inside {} is replaced by its value. :.0% shows a number as a percentage with no decimals.
Lists and loops
scores = [72, 85, 90, 64]
total = 0
for score in scores:
total = total + score
print(len(scores), total, total / len(scores))
# 4 311 77.75
print(scores[0], scores[-1], scores[1:3])
# 72 64 [85, 90]
Positions start at 0, so scores[0] is the first item. scores[-1] is the last one, and scores[1:3] is a slice: items 1 and 2, but not 3.
A list comprehension builds a new list in one line. This one keeps the scores of 70 or more:
high_scores = [s for s in scores if s >= 70]
print(high_scores)
# [72, 85, 90]
Conditions and functions
def grade(score):
if score >= 85:
return "A"
elif score >= 70:
return "B"
else:
return "C"
for s in scores:
print(s, grade(s))
# 72 B
# 85 A
# 90 A
# 64 C
Python uses indentation (the spaces at the start of a line) to know which lines belong to the def, the if or the for. Keep it consistent: four spaces is the usual choice.
Dictionaries
A dictionary stores key: value pairs. Here it counts how often each label appears, which you will do all the time with real datasets:
labels = ["cat", "dog", "cat", "bird", "cat", "dog"]
counts = {}
for label in labels:
counts[label] = counts.get(label, 0) + 1
print(counts)
# {'cat': 3, 'dog': 2, 'bird': 1}
print(counts["cat"])
# 3
counts.get(label, 0) means "the value for this label, or 0 if it is not there yet".
Libraries
import math
import statistics
print(math.sqrt(16))
# 4.0
print(statistics.mean(scores))
# 77.75
print(statistics.median(scores))
# 78.5
math and statistics come with Python. Later you will import bigger libraries in exactly the same way, such as import pandas as pd in Pandas.
Reading error messages
You will see errors every day. Everyone does, including the people who wrote this guide. The skill is not avoiding them; it is reading them calmly. When Python stops, it prints a traceback. Here is the one you get for asking a four-item list for scores[4]:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[1], line 2
1 scores = [72, 85, 90, 64]
----> 2 print(scores[4])
IndexError: list index out of range
Read it from the bottom up. The last line says what went wrong: position 4 does not exist, because a list of four items only has positions 0 to 3. The lines above it say where: Cell In[1], line 2, with an arrow pointing at the line that failed. This is how an error looks in a notebook; in a .py file the layout differs a little, but the last line still tells you what happened.
These are the errors you will meet most often at the start:
| Error | What it usually means | Example |
|---|---|---|
SyntaxError |
Python cannot read the line: a missing bracket, quote or colon. | print("hi" |
IndentationError |
The spaces at the start of a line are wrong. | a line under if that is not indented |
NameError |
You used a name Python does not know, often a typo. | print(scroes) |
TypeError |
You mixed types that do not work together. | "Score: " + 85 |
IndexError |
You asked for a position that does not exist. | scores[4] in a list of four |
KeyError |
You asked a dictionary for a key it does not have. | counts["fish"] |
For more, see Errors and Exceptions in the official tutorial and CS50P week 3.
How to practise
- Type the code yourself. Copy-pasting skips the part where your fingers and brain learn the syntax.
- Change the examples. What happens with an empty list? A negative score? Breaking code on purpose teaches you a lot.
- A little every day beats a lot once a week. Thirty minutes a day for two weeks is better than one eight-hour Sunday.
- Ask for help the right way. Try
help(len)in Python, search the official documentation, or ask in the SOTA Discord. If you ask an LLM, ask it to explain rather than to write the code for you; the prompt in How to study works well.
Check yourself
Try each one before opening the answer.
If these felt easy, you are ready to move on. If not, no problem: go back to the matching Kaggle lesson and redo its exercise.
What comes next
- Object-Oriented Programming, Classes, and Inheritance: the
classkeyword you will see in PyTorch code. - Simple Python Problems: more practice with small problems.
- Jupyter Notebook Interface and Basics: the notebook format every IOAI task uses.
Resources
| Source | Title | Why read it |
|---|---|---|
| Kaggle Learn | Python | Seven short lessons with exercises, run in your browser. Our recommended main course. |
| Harvard | CS50's Introduction to Programming with Python (CS50P) | Free lectures, notes and problem sets. Weeks 0–4 cover this module in more depth. |
| Python.org | The Python Tutorial | The official tutorial. Best for looking something up once you know the basics. |
| Python Tutor | Visualize code step by step | Paste a few lines and watch what every variable holds after each step. |
| Al Sweigart | Automate the Boring Stuff with Python, 3rd edition | A free book. Chapters 1–8 cover everything here, for readers who prefer text. |
| University of Helsinki | Python Programming MOOC 2026 | A free university course with many small exercises checked automatically. |
| freeCodeCamp | Learn Python: Full Course for Beginners | One long video, if you want a quick overview before a proper course. |
Finished this module? Mark it done.
Comments
Stuck on something, spotted a mistake, or found a resource that helped you? Tell us below.
Loading comments