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

  1. Install Python (you can download the installer from here).

  2. Check that Python is installed correctly by opening a terminal (or command prompt) and running the following command:

    python --version

    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.

  3. Familiarize yourself with the interactive mode. You can start the interactive mode by running python in 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 42 and press Enter. You should see 42 on the next line.
    • Type 4 * 5 and press Enter. You should see 20 on the next line.
    • Type name = "Bo" and press Enter. Then type name and 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.

  4. Choose an editor for Python. For example, you can use VS Code (with the Python extension) or PyCharm.

  5. Navigate to the directory where you want to create your Python files.

  6. Create a new file with a .py extension. For example, create test-python.py with the following program:

    42
    
    name = "Bo"
    print(name)
    
    def my_sqr(x: int) -> int:
        return x * x
    
    my_sqr(1)
    print(my_sqr(2))
  7. To run this program with the Python interpreter, navigate to the directory containing the file and open the terminal. Then, run the following command:

    python test-python.py

    You should see the following output:

    Bo
    4

    Note that only calls to print produce output.

  8. If you want to run the program in the interactive mode, you can do so by running the following command:

    python -i test-python.py

    You should see the following output:

    Bo
    4
    >>>

    The >>> prompt indicates that you are now in the interactive mode. Now, do the following:

    • Type name and press Enter. You should see 'Bo' on the next line.
      Observe that the string value 'Bo' stored in the variable name is 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, when name is printed in the program using print(name), it is printed without quotation marks.

    • Type my_sqr(-5) and press Enter. You should see 25 on the next line.

    • Type my_sqr and press Enter. You should see something like <function my_sqr at 0x...> on the next line. This indicates that my_sqr is 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

  1. Install pytest by, e.g., running:

    python -m pip install -U pytest
  2. Check that pytest is installed correctly by running the following command:

    python -m pytest --version

    You should see the version of pytest you installed.

  3. Modify your test-python.py file to:

    • Import pytest at the top of the file by adding the line import pytest

    • Add a test function test_my_sqr (note that a testing function has to start with test_) that tests my_sqr after the function definition, as follows:

      def test_my_sqr():
          assert my_sqr(0) == 0
          assert my_sqr(3) == 9
          assert my_sqr(-4) == 16
    • Add the following line at the bottom of the file to run all the tests when the file is executed:

      pytest.main([__file__])
  4. Run the program again with the same command as before:

    python test-python.py

    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 =============================
  5. Modify the test_my_sqr function to include a failing test, for example:

    assert my_sqr(5) == 20
  6. 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)
  7. 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

  1. Install mypy by, e.g., running:

    python -m pip install -U mypy
  2. Check that mypy is installed correctly by running the following command:

    python -m mypy --version

    You should see the version of mypy you installed.

  3. Run the type checker on your test-python.py file by running:

    mypy test-python.py

    or

    python -m mypy test-python.py

    You should see output similar to the following:

    Success: no issues found in 1 source file
  4. See if your editor of choice has support for mypy. For example, there is a VS Code extension for mypy.

  5. Add the following line to the file after print(my_sqr(2)):

    print(my_sqr("-7"))
  6. 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)
  7. 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'
  8. 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

Videos: