In this lab, you will learn how to use Visual Studio Code (VS Code) for Python development. We will begin by exploring the VS Code interface, familiarizing ourselves with its key components like the File Explorer, Editor Area, and Terminal.
Next, you will practice using Python in interactive mode directly within the VS Code terminal, which is a convenient way to test code snippets. Finally, you will create and run a Python script within VS Code, solidifying your understanding of the development workflow.
Reading won’t teach your fingers to code.
Explore the VS Code Interface
In this step, we will familiarize ourselves with the VS Code interface, which is the primary development environment we will use in this course. VS Code is a powerful and versatile code editor that supports many programming languages, including Python.
When you open the LabEx environment, you will see the VS Code interface. Let's take a look at the main areas:
File Explorer: Located on the left side, this area shows the structure of your project. You can create new files and folders here by right-clicking.
Editor Area: This is where you will open and edit your files. Each open file appears as a tab. Changes are usually saved automatically.
Terminal: Located at the bottom, this is a command-line interface. In the LabEx environment, this is a Linux terminal (specifically, zsh). You can run Linux commands here, and also launch the Python interactive mode or run Python scripts.
Take a moment to look at these areas in your LabEx environment. The default directory in the terminal is /home/labex/project.
You can also explore the menu bar at the top left of the VS Code window to see the various options available.
Use Python in Interactive Mode in VS Code Terminal
In this step, we will explore the Python interactive mode directly within the VS Code terminal. The interactive mode is useful for quickly testing small snippets of code and experimenting with Python features.
To enter the Python interactive mode, open the terminal (Menu bar -> Terminal -> New Terminal) in VS Code and type the following command:
python
You should see output similar to this, indicating that you are now in the Python interactive shell:
Python 3.10.x (main, ...)
[GCC ...] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> prompt indicates that the Python interpreter is ready to accept commands.
Now, let's try running a simple Python command. Type the following at the >>> prompt and press Enter:
print("Hello, Interactive Mode!")
You should see the output Hello, Interactive Mode! printed below your command.
To exit the Python interactive mode, you can type exit() or quit() and press Enter, or use the keyboard shortcut Ctrl + D. Let's exit the interactive mode now:
exit()
You will return to the regular terminal prompt (labex:project/ $).
Tutorials are better when they're interactive.
Create and Run a Python Script in VS Code
In this step, we will create a simple Python script file and run it using the VS Code terminal. This is the standard way to write and execute larger Python programs.
First, let's create a new file named helloworld.py in the /home/labex/project directory. You can do this by right-clicking in the File Explorer area and selecting "New File", or by using the terminal. Let's use the terminal for practice:
touch /home/labex/project/helloworld.py
After running the command, you should see helloworld.py appear in the File Explorer on the left. Click on helloworld.py in the File Explorer to open it in the Editor Area.
Now, type the following Python code into the helloworld.py file:
print("Hello, World from script!")
VS Code automatically saves your changes.
Next, we will run this script from the terminal. Make sure your terminal is in the /home/labex/project directory (it should be by default). Then, use the python command followed by the script name:
python helloworld.py
You should see the output of your script printed in the terminal:
Hello, World from script!
This demonstrates how to write Python code in a file and execute it using the Python interpreter.
Summary
In this lab, we began by exploring the fundamental components of the VS Code interface, including the File Explorer for project navigation, the Editor Area for writing and modifying code, and the integrated Terminal for executing commands and interacting with the operating system. We familiarized ourselves with the layout and key functionalities of this development environment.
Following the interface exploration, we learned how to utilize the Python interactive mode directly within the VS Code terminal. This involved launching the Python interpreter using the python command and understanding the >>> prompt as the indicator for interactive input. This step demonstrated a quick way to test Python code snippets and experiment with the language's features.
"
This was incredible. Exactly type of practical learning I can take beyond this lab, I immediately was able to implement on my personal system and see reason for cause to action and results. Awesome"