Write Python. Understand every error.

Open the editor

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.

Start learning

What you can do

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.

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

About Python

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.

Hello, World! and print()

The traditional first program in any language shows the words Hello, World! on the screen. In Python it is one line.

Variables

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.

Data types

Every value in Python has a type, which decides what you can do with it. The four basic types are:

Operators

Arithmetic operators work on numbers:

Input and output

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.

Strings

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.

Conditions: if, elif, else

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.

for loops and range()

A for loop repeats a block once for every item in a sequence. With range(), you can repeat a fixed number of times.

while loops, break and continue

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.

Lists

A list stores many values in order, inside square brackets. Like strings, lists are indexed from 0.

Tuples and sets

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.

Dictionaries

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.

Functions

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.

Modules: math and friends

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.

Errors and exceptions

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 arrays

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.

Creating arrays

You rarely type every value by hand. NumPy has functions that build arrays for you.

Indexing and slicing

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 and broadcasting

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.

Statistics and aggregation

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.

Boolean masks and filtering

Comparing an array with a value gives an array of True and False, one for each element. This is called a boolean mask.

Reshaping and combining

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.

Matrices and linear algebra

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

Drawing with turtle

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

About the author

Portrait of Dr. Saptarshi Das

Dr. Saptarshi Das is an AI engineer and machine-learning security specialist based in Kolkata, India, with a Ph.D. from IIT Kharagpur. He creates and maintains Py-CRAFT as an independent project.

Read the full profile · Contact

Credits and licences