Imagine you want to learn computational physics and Python as an advanced high-school student or undergraduate. Where to start? Assuming no prior programming experience, a quick introduction to programming concepts wonât go amiss. To get to that point, though, youâll need to prepare some tools first.
In this post, I describe how to install Python and Visual Studio Code, two tools we need before we can start programming and learning computational physics. I then describe how to configure VS Code to use Python, finishing up by writing a toy Python program and running it within VS Code.
A high school student that I tutor asked me a while ago if one can learn physics by programming in Python. I answered: âOf course!â Since then, Iâve had all kinds of ideas about how to introduce computational physics and numerical methods to advanced high-school or early-level university students.
This is the first post in a (long-term) series focusing on numerical methods and computational physics. The intent is to collect enough topics and examples to create a course in computational physics using Python.
A rough plan
My rough plan is to discuss concepts already taught at school, for which we know the analytical solution. This way, we focus on learning the numerical methods, secure in the knowledge that we have a firm understanding of the mathematics and physical concepts.
Then we implement the numerical solution in Python and compare it with the analytical one to see how well we did. This reduces cognitive load: weâre not introducing new physics and maths concepts. The only things that are new to us are programming and the relevant numerical methods.
One long-term goal is to build sufficient understanding of the numerical methods so that we can move out of our comfort zone and learn to solve physical problems where an analytical solution is either unknown or difficult to calculate.
Before we can really get going, we need to set up our working environment.
Getting started
To run Python programs, we need a working Python development environment. And to create Python programs, we need software in which we can write code. Thus, we need to do some boring administrative work before we can start doing interesting physics and programming stuff.
This post will focus on getting a Python environment up and running within Visual Studio Code. The following article will introduce some basic programming concepts and show a simple example of using the NumPy and Matplotlib libraries. This foundation will enable us to focus on computational physics topics later.
The installation guide described here assumes a Windows environment. I made this decision because I assume that most students either use Windows or have access to a Windows computer.
Some students will have macOS. Unfortunately, I donât have access to a macOS system. Hence, I canât describe the installation process in that environment.
Other students likely use Linux. From personal experience, Linux users are often in the minority. So, even though I would like to describe the process from a Linux perspective, I chose Windows because it is relevant to most students.1
We now focus on installing Python and the Visual Studio Code integrated development environment (known simply as VS Code). As you will see below, thereâs a lot of setup required before we can get to the fun stuff and start programming. Fortunately, we only have to do this setup once, so itâs worth taking our time to do it properly.
Installing Python
According to the Visual Studio Code tutorial on installing Python, itâs recommended to install Python first, so weâll start there. This ensures that Python is available for VS Code to find when we install it later.
There is also good documentation on the VS Code website for writing and running Python programs. You can find more information under the âPythonâ link on that site.
Before installing Python, we have to download it. The best place to get it for Windows is from the Python website. Using a browser, navigate to the Python downloads page:
Now click on the âDownload Pythonâ button to download the most recent version of Python (note that the version number you see will be different to that shown in the screenshot above).2
After the download has completed, open your Downloads folder and search for the installer application. Itâll be called something like python-3.15.5-amd64.exe (where 3.15.5 is the version number and is likely different in your case). You should see an icon which looks like this:
Double-click on the installer icon. You should then see the Python for Windows installer startup dialog box:
This window has two unchecked options at the bottom. One mentions using admin privileges when installing Python (which we donât need), and the other mentions adding python.exe to your PATH. Your PATH tells your computer where to look when it wants to find the program to run Python. Itâs therefore a good idea to check this option.
Now weâre ready to install Python. Click on the âInstall Nowâ option in the middle of the window. This will install Python and any supporting software it needs.
Once the installation has completed, youâll see the âSetup was successfulâ dialog box appear.
Click on the âCloseâ button to continue.
Now that Python is installed, weâre ready to install Visual Studio Code.
Installing Visual Studio Code
One thing to note first: Visual Studio Code is not Visual Studio; theyâre different things. Yes, Visual Studio is also an IDE.3 Yes, itâs a product from the same company. Yes, this is confusing. Sorry about that.
Go to the Visual Studio Code download page and choose an installer appropriate for your operating system.
Because weâre using Windows, click on the âWindowsâ button to download the installer application. This will download a file called VSCodeUserSetup-x64-1.101.2.exe (the 1.101.2 part is the version number; itâll be different for you).
Open your Downloads folder and double-click on the .exe fileâs icon:
This will start the VS Code installer:
To continue, we have to click on âI accept the agreementâ. This way, the âNextâ button becomes active.
Click on the âNextâ button. Youâll see a window asking you to choose a destination location.
The default option should be good enough, so click on âNextâ in this window. A dialog box will appear, asking you if you want a Start Menu folder entry for Visual Studio Code.
This is handy to have, so click on âNextâ to continue.
The following dialog box asks which additional tasks the installer should perform.
One option you might want to select, which isnât one of the default options, is to add a desktop icon. If you want that, go for it, and click on the âCreate a desktop iconâ checkbox. In this case, I chose not to do this and simply clicked on âNextâ.
Youâre now presented with an overview of what will be installed.
Click on âInstallâ to install Visual Studio Code. The installation will now start.
You should see that the installation completed successfully.
The dialog box has a checkbox to ask if you want to launch Visual Studio Code. This is a good idea, so click on âFinishâ to complete the installation and start the IDE.3
The VS Code start page will then appear.
You could take the walkthrough setup of VS Code at this stage, if you want. Weâll skip that, though, and start installing the extensions we need to get Python up and running within VS Code.
Installing the Python VS Code extension
The next task is to install the Python VS Code extension. This allows us to develop and run Python programs within the VS Code IDE.
To see a list of available extensions, you could either click on the extensions icon from the toolbar on the left-hand side of the page:
or click on the settings icon (the cog symbol at the bottom left of the page) and select the Extensions menu option.
Either way, youâll see a list of extensions populated in the left-hand frame in VS Code.
Fortunately for us, Python appears as one of the recommended extensions at the top of the list. If it werenât, we could have searched for it in the search box above the list of extensions.
To install the Python extension, click on its âInstallâ button. If you look carefully, youâll see that VS Code also installs the Pylance and Python Debugger extensions.
Once the Python extension has been installed, youâll see a help page for getting started with Python development:
Now weâre ready to set up a project, which will contain the computational physics topics weâre going to investigate as part of this course.
Setting up the project folder
Our next goal is to write a simple Python program. Again, thereâs some setup we have to do before we can do that. Such is life when starting at the very beginning. Donât worry, weâll soon have enough foundation that we wonât have to spend any time on setup.
In software development, which is part of what weâre doing here, one often collects related files together into a project. This is an organisational structure that keeps related things together; things that share a similar topic or theme. In theory, you could put all files from all projects into your user directory. And yes, Iâve seen people who work like that. However, youâll find that over time, as you write more and more code, things get hard to find. Such chaos slows you down and makes your life harder. Hence, itâs a good idea to bundle code into projects andâas necessaryâinto subfolders to keep things organised.
The top-level organisational unit we have is the overarching topic of computational physics. Thus, weâll create a project folder in VS Code for this topic. Subtopics live in their own folders within the main project folder. The main project folder will also contain the environment we need to run Python programs.
There are two ways to create a project folder. We can either click on the âFileâ menu and choose the âOpen FolderâŠâ option, or we can click on the âOpen Project Folderâ button from the âGet Started with Python Developmentâ page. For example:
Weâll choose the second option, because itâs a single click. Clicking on the âOpen Project Folderâ button in the middle of the page will open the âOpen Folderâ system dialog.
The issue right now is that we donât have a prior project folder we want to open: weâve got to create a new one. To do this, click on the âNew folderâ button at the top left of the âOpen Folderâ dialog box.
This created a new folder in the current directory. You should see that the folderâs name (âNew folderâ) is highlighted and thus ready for us to rename. Letâs call this folder ComputationalPhysics.
Click on the âSelect Folderâ button at the bottom right to select it as our project folder for use in VS Code.
VS Code will now open the project folder. But first, it will ask you if you trust the authors of the files in this folder. Since you yourself are the author, you can click on the âYes, I trust the authorsâ button. Assuming, of course, that you trust yourself. đ
Creating a Python virtual environment
Theoretically, weâre ready to create a simple Python program and start playing. However, later we want to install some external Python libraries (i.e. libraries that arenât distributed as part of Python itself) so that we can define and plot data. Thus, we need to set up a virtual environment which will contain these extra libraries.
Setting up a virtual environment is a good idea when writing Python programs. It allows you to keep the extra libraries you install separate from any other Python software you might have, thus avoiding conflicts between libraries with different versions.
Conflicts between different library versions can arise surprisingly easily. Hence, itâs useful to isolate software libraries within their own project context. Our computational physics project is one instance of such a context. Were we to install a library in a global context (i.e. where the library could be used across multiple projects), itâs possible for different projects to need different library versions.
For instance, letâs say program blah needs version 5.2 of a library called buzz, but program foo needs version 3.7 of that same library. Itâs not possible to satisfy both requirements had we installed buzz at a global level. But using a virtual environment allows us to keep things isolated. Thus, if programs blah and foo each use their own virtual environment, we can install the appropriate buzz version for each program and avoid conflict.
There are a few ways to set up a virtual environment in VS Code.
One way to do this is to go to the âViewâ menu and select the âCommand PaletteâŠâ option:
You will see a list of items in a drop-down menu at the top of the screen.
You can either type âPython: Create Environmentâ into the box at the top:
or scroll down the (very) long list of options until you see the âPython: Create EnvironmentâŠâ option. Either way, click on this option now to create your Python environment.
Another way to do this is to click in the box at the top middle of the screen containing a magnifying glass icon and the name of our project folder, ComputationalPhysics. Clicking here will bring up a menu including the option âShow and Run Commandsâ.
Assuming that the âGet Started with Python Developmentâ window is still available, click on the âSelect or create a Python environmentâ option and then click on the âCreate Environmentâ button.
VS Code will ask us what type of environment we want to create.
Since we arenât using Anaconda, we only want to use the standard Python virtual environment, so click on the âVenvâ option. This will create a folder within our project folder with the name .venv.
VS Code now asks us which Python installation we want to use in our virtual environment. If youâve installed multiple versions of Python, youâll see several options in the drop-down list that now appears. In our case, weâve only installed the one Python version, so we only see one version appear:
Selecting the Python installation will create the Python environment. Youâll see an info box in the bottom right-hand part of the window telling you about this.
Creating a new Python file
To make sure that everything works as expected, letâs create a simple Python file to check. Usually, you create new files by going to the âFileâ menu and selecting the âNew Text Fileâ option.
However, because weâre just getting started, we click on the âCreate a Python fileâ step from the âGet Started with Python Developmentâ window:
Doing so will open a Python file called Untitled-1 in a tab next to the âWelcomeâ tab containing the âGet Started with Python Developmentâ stuff.
Note that if you open a new file via the âFileâ menu, youâll only see the new file in a tab and the âWelcomeâ tab will be hidden.
A personal statistics program
Weâre now going to create a small program which asks you some questions and prints back that information in a slightly different form. Itâs not very interesting, but it allows us to see that the Python environment is working, and we can start some basic programming.
Click in the tab named Untitled-1 and enter the following text:
name = input("What's your name? ")
age = int(input("What's your age (in years)? "))
height = float(input("What's your height (in m)? "))
print(f"Hello {name}! You are {age} years old and {height} m tall.")
You need to save this file before you can run it. You save files by either selecting the âFileâ menu and then the âSaveâ option:
or you could use the key combination Ctrl+S. This means you press the control key (labelled Ctrl on your keyboard) together with the s key.
Either way, youâll see a dialog box to save the file as a certain name.
In the âFile name:â field at the bottom, where the text âUntitled-1â is highlighted, enter the name print-personal-stats.py. Then click on the âSaveâ button in the bottom right-hand corner.
Now weâre ready to run the file. At the top right of the VS Code tab with our Python code in it, thereâs a small triangle which looks like the symbol for âPlayâ. If you hover your mouse over this triangle, youâll see the text âRun Python Fileâ.
Click on the triangle to run the file.
You will see a new window appear in VS Code. This is your terminal window where your program runs.
Youâll also notice that your program has started, and itâs already asking you a question. To enter text into the terminal, you have to click in its area at the bottom of your VS Code window to focus it. This will allow you to interact with your running program. A cursor (a small white rectangle) will appear where you can now start entering information.
As an example, enter the following information:
- When asked for the name, enter:
Alice - When asked for the age, enter:
42 - When asked for the height, enter:
1.57
After typing each piece of information, press the âEnterâ key to pass it to the program. You should see output like this:
Congratulations! Youâve run your first Python program in VS Code!
Weâve fulfilled a large number of the first steps listed in the âGet Started with Python Developmentâ welcome tab, and weâre able to continue with confidence. We can close this tab and free up some room in the VS Code window.
Close the âWelcomeâ tab by clicking on the small âxâ next to the word âWelcomeâ.
That gave us a lot more room to see our code. Nice!
More next time
That will do for this time. Weâve come quite far in getting everything we need up and running. Next time, weâll discuss the Python code in detail, talking about programming concepts such as variables, assignment, and data types.
As a long-time Unix and Linux user, this does break my heart a bit. However, I have to realise that Windows is the most common environment for students in the wild and adapt to that. â©
Note that you could go to the Python for Windows downloads page. But it turns out to be easier to click on the âDownload Pythonâ button on the main downloads page. If youâre on Windows, it will detect that and will provide you with a direct link to download Python. â©
IDE stands for Integrated Development Environment. This is a term for a very powerful code editor packed with lots of functionality that makes editing and running code very easy. You could use something simpler, such as vim orEmacs, to write Python code, and many people do. IDEs such as VS Code are very prevalent in software development circles. â© â©2










































Top comments (0)