About Py-CRAFT
Py-CRAFT is a free, browser-based place to learn Python and NumPy by writing real code. Every program runs directly in your web browser, with nothing to install, and every error is explained in plain English: what went wrong, why Python behaves that way, which line caused it, and how to fix it.
What you can do
- Learn 25 topics, each with a short lesson, worked examples, automatically checked exercises and a quiz.
- See your code run with Step through, which shows every line and every variable changing.
- Build 5 mini-projects step by step, from a report card maker to a weather analysis with NumPy.
- Draw with turtle graphics to practise loops in a visual way.
- Test yourself with timed practice, like an exam.
- Keep your progress in your browser or, where accounts are enabled, in your account on any device, and earn a certificate of completion.
- Study classic programs such as prime numbers, Fibonacci and palindromes in the Python programs section, each with real output.
- Share code as a link, and use the error guide and cheat sheets as quick references.
- Optional AI help with your own API key, or by connecting a free OpenRouter account.
For teachers
Create classes, share a class code, set assignments that every student receives identically, follow progress live and download results for Excel on the teacher dashboard. Students without accounts can send progress and submission files instead. Worksheets for any topic can be printed with or without an answer key.
How the content is checked
Every example and answer in Py-CRAFT is tested before it is published.
- Each quiz question whose answer depends on running code was run in real Python, and the marked answer must match the real output exactly.
- Each cheat-sheet example shows the output that Python really printed. Each error-guide example was run to confirm that it raises the error it claims to, and that the suggested fix works.
- Each exercise has a model solution that must pass the same automatic checks that learners face, on several sets of input.
- The same programs are then run again in the Python engine used inside the browser, to make sure learners see the same results as in real Python.
For NumPy, Py-CRAFT uses a small teaching version that covers the commonly used functions. It behaves like NumPy for those, but it is not the full library.
What you will learn
Python basics
Python is a programming language: a precise way of giving instructions to a computer. It was created by Guido van Rossum and first released in 1991. It is popular because its code reads almost like English, and it is used for data science, artificial intelligence, websites, automation and teaching.
The traditional first program in any language shows the words Hello, World! on the screen. In Python it is one line.
A variable is a name that refers to a value. You create one with =, which means "store the value on the right under the name on the left". It does not mean "equals" as in maths.
Every value in Python has a type, which decides what you can do with it. The four basic types are:
Arithmetic operators work on numbers:
input() pauses the program and waits for the user to type something. Whatever they type is returned as a string. You can show a message, called a prompt, inside the brackets.
A string is a sequence of characters. Each character has a position, called an index, starting from 0. Negative indexes count from the end, so -1 is the last character.
An if statement runs a block of code only when a condition is True. The line ends with a colon, and the block below is indented by 4 spaces.
A for loop repeats a block once for every item in a sequence. With range(), you can repeat a fixed number of times.
A while loop repeats as long as its condition stays true. Use it when you do not know in advance how many rounds you need.
A list stores many values in order, inside square brackets. Like strings, lists are indexed from 0.
A tuple is like a list that cannot be changed. It is written with round brackets. Tuples are useful for values that belong together, such as coordinates or a date.
A dictionary stores pairs of keys and values, like a real dictionary stores words and meanings. You look up a value by its key, not by position.
A function is a named block of code that you can run whenever you need it. You define it with def, give it parameters in brackets, and send a result back with return.
A module is a file of ready-made code that you can use in your programs. Python comes with many, and you load one with import.
When Python meets a problem it cannot handle, it raises an exception and the program stops. The explanation panel in this app tells you what each one means. Common ones are ValueError, ZeroDivisionError, IndexError, KeyError and TypeError.
NumPy
NumPy, short for Numerical Python, is the library that nearly every scientific and machine-learning tool in Python is built on, including pandas, scikit-learn, TensorFlow and PyTorch. Its central object is the array, or ndarray: a grid of values that all share one type.
You rarely type every value by hand. NumPy has functions that build arrays for you.
A 1D array is indexed just like a list: a[0] is the first element, a[-1] the last, and a[1:4] a slice.
Arithmetic between two arrays of the same shape works element by element: the first element with the first, the second with the second, and so on.
Aggregation functions summarise an array in a single value. Each one exists both as a method and as a function, so a.sum() and np.sum(a) are the same.
Comparing an array with a value gives an array of True and False, one for each element. This is called a boolean mask.
reshape gives the same values a new shape, reading them row by row. The total number of elements must not change: 12 values can become (3, 4), (2, 6) or (12, 1), but not (5, 3). Put -1 in one position to let NumPy work out that size.
A 1D array can represent a vector and a 2D array a matrix. Be careful to choose the right kind of multiplication:
Fun with Python
The turtle module lets you draw pictures with code. Imagine a small turtle holding a pen, sitting in the middle of the screen and facing right. You give it instructions, and it draws a line wherever it walks.
Projects
- Report card maker: Read marks, work out the total, average and grade, and print a neat report card.
- Number guessing game: Build a guessing game step by step: compare guesses, loop until correct, and count the tries.
- Shopping bill with GST: Read items and prices, add them up, apply a discount and GST, and print a bill.
- Word statistics: Analyse a sentence: count words, find the longest word and the most common word.
- Weekly weather with NumPy: Store a week of temperatures in a NumPy array and find averages, extremes and hot days.
About the author
Credits and licences
- Python code runs inside your browser using Brython, an open-source Python interpreter for the web, released under the BSD licence.
- The pages use the open-source fonts Atkinson Hyperlegible and IBM Plex Mono, released under the SIL Open Font License and served by Google Fonts when you are online.
- Sign-in and class features use Google Firebase. Advertising, where shown, is provided by Google AdSense.
- Python and NumPy are trademarks of their respective owners. Py-CRAFT is an independent project and is not affiliated with or endorsed by them.
