Getting started with Python
At the beginning, you can use an online Python interpreter, such as this one (it has simpler support for the modules you’ll need) or this one (if you want to try out the interactive mode), but you will soon need to have Python installed on your computer. You can also try out the interactive shell for small examples.
We will also use a static type checker called mypy, which can catch some type-related errors before running the code.
To get a complete setup, you will need to do the steps below. At the bottom of this page, you will find some videos that might be helpful during this process (feel free to search for other videos and tutorials or use AI tools to help you set up the environment).
Basic Python setup
Check that Python is installed correctly by opening a terminal (or command prompt) and running the following command:
You should see the version of Python you installed. If you see an error, you may need to add Python to your system’s PATH.
Familiarize yourself with the interactive mode. You can start the interactive mode by running
pythonin the terminal. You should see something like this:Python 3.14.7 (tags/v3.14.7:823f032, Aug 5 2026, 10:51:32) [MSC v.1944 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>Next, do the following:
- Type
42and press Enter. You should see 42 on the next line. - Type
4 * 5and press Enter. You should see 20 on the next line. - Type
name = "Bo"and press Enter. Then typenameand press Enter. You should see'Bo'on the next line.
As you might have noticed, this interactive mode is similar to the interactive pane in Pyret—it shows the value of each expression you type.
Type
exit()and press Enter to exit the interactive mode.- Type
Choose an editor for Python. For example, you can use VS Code (with the Python extension) or PyCharm.
Navigate to the directory where you want to create your Python files.
Create a new file with a
.pyextension. For example, createtest-python.pywith the following program:To run this program with the Python interpreter, navigate to the directory containing the file and open the terminal. Then, run the following command:
You should see the following output:
Bo 4Note that only calls to
printproduce output.If you want to run the program in the interactive mode, you can do so by running the following command:
You should see the following output:
Bo 4 >>>The
>>>prompt indicates that you are now in the interactive mode. Now, do the following:Type
nameand press Enter. You should see'Bo'on the next line.
Observe that the string value'Bo'stored in the variablenameis displayed in quotation marks, similar to how it would have been displayed in Pyret (Pyret uses double quotes for strings, while Python uses single and double quotes interchangeably). However, whennameis printed in the program usingprint(name), it is printed without quotation marks.Type
my_sqr(-5)and press Enter. You should see25on the next line.Type
my_sqrand press Enter. You should see something like<function my_sqr at 0x...>on the next line. This indicates thatmy_sqris a function, similar to how it would have been displayed in Pyret.Type
exit()and press Enter to exit the interactive mode.
You are now ready to learn the basics of Python and follow the beginning of 9.1 From Pyret to Python.
Setting up testing
Install pytest by, e.g., running:
Check that pytest is installed correctly by running the following command:
You should see the version of pytest you installed.
Modify your
test-python.pyfile to:Import pytest at the top of the file by adding the line
import pytestAdd a test function
test_my_sqr(note that a testing function has to start withtest_) that testsmy_sqrafter the function definition, as follows:Add the following line at the bottom of the file to run all the tests when the file is executed:
Run the program again with the same command as before:
You should see output similar to the following:
Bo 4 ============================ test session starts ============================ platform win32 -- Python 3.14.7, pytest-9.0.2, pluggy-1.6.0 rootdir: C:\Users\... collected 1 item test-python.py . [100%] ============================= 1 passed in 0.02s =============================Modify the
test_my_sqrfunction to include a failing test, for example:Run the program again with the same command as before. You should see output similar to the following:
Bo 4 ============================ test session starts ============================ platform win32 -- Python 3.14.7, pytest-9.0.2, pluggy-1.6.0 rootdir: C:\Users\... collected 1 item test-python.py F [100%] ================================= FAILURES ================================== def test_my_sqr(): assert my_sqr(0) == 0 assert my_sqr(3) == 9 assert my_sqr(-4) == 16 > assert my_sqr(5) == 20 E assert 25 == 20 E + where 25 = my_sqr(5)Fix the failing test and run the program again to make sure all tests pass.
You are now ready to start working through 9.1.3 Examples and Test Cases.
Note. Usually, instead of calling pytest.main from the program, tests in Python are run from the command line using the pytest command. In this course, to make it easier to run tests and avoid having to use multiple terminal commands, we will stick to the pytest.main approach. Make sure the call to pytest.main always stays at the bottom of the file, after all the test functions.
Setting up type checking
Install mypy by, e.g., running:
Check that mypy is installed correctly by running the following command:
You should see the version of mypy you installed.
Run the type checker on your
test-python.pyfile by running:or
You should see output similar to the following:
Success: no issues found in 1 source fileSee if your editor of choice has support for mypy. For example, there is a VS Code extension for mypy.
Add the following line to the file after
print(my_sqr(2)):Run the type checker again. You should see output similar to the following:
test-python.py:NN: error: Argument 1 to "my_sqr" has incompatible type "str"; expected "int" Found 1 error in 1 file (checked 1 source file)Now run the program. You should see output similar to the following:
Bo 4 Traceback (most recent call last): File "test-python.py", line NN, in <module> print(my_sqr("-7")) ~~~~~~~^^^^^ File "test-python.py", line NN, in my_sqr return x * x; ~~^~~ TypeError: can't multiply sequence by non-int of type 'str'Fix the type error by changing
"-7"to-7. Run the type checker and the program again to make sure there are no errors.
As you can see, the Python interpreter itself ignores type annotations (unlike Pyret), but the mypy type checker uses type annotations to catch some type-related errors without running the code.
Optional resources
- Short tutorial: How to Run a Python Script (From Terminal, IDE, or File)
Videos:
- How to Install Python on Windows 11 in 2026 (Full Beginner Guide) (3 minutes)
- How to Install Python on Windows 11 (DevOps DevSecOps Beginner Guide) (3 minutes)
- How to Setup Python & VS Code on Mac (DevOps DevSecOps Beginner Guide) 2026 (7 minutes)
- 4 Ways to Run Python Code (Terminal, IDLE & VS Code Explained) (10 minutes)
- Python Tutorial for Beginners 1: Install and Setup for Mac and Windows (16 minutes)
This is an older video, but it explains the difference betweenpythonandpython3(if you happen to run into that issue) and briefly shows how to use the terminal/command line.