Python and VsCode Setup

Ready to embark on an exhilarating programming journey? Fantastic! Let's lay a strong foundation to ensure your success. In this initial chapter of the course, we will install two essential software programs: Python, the programming language itself, and an Integrated Development Environment (IDE).

Writing code directly through the console or terminal can be quite tedious, which is why we will install the popular VsCode IDE. It is widely used and open-source πŸ˜‰. VsCode offers customization options and useful extensions that enhance programming efficiency. It is not limited to Python either, as it supports other popular programming languages like JavaScript, C, and CSS. This makes it a valuable investment for beginners and professionals programmers alike.

By setting up these key components, we create an environment that promotes efficient coding practices and unlocks your programming potential. Throughout this tutorial, we guide you through the installation process. While we focus on Windows installations, the principles we discuss apply to Mac and Linux systems as well.

Let’s embark on this programming adventure together! πŸš€

Python installation

Installing Python is a breeze. Let us walk through the steps together. First, head over to the official website at https://www.python.org/downloads/. There, you will find a bright yellow button that says Download Python (check out the picture below). Click on it, and the download will start automatically.

pff0-1.png

Once the download is complete, go ahead and run the file you just downloaded.

⚠️ Important: Make sure to follow the steps after opening the installation file to avoid any issues. ⚠️

In the first window that appears, pay close attention to the checkbox that says Add Python 3.X to PATH (see picture below). It must be ticked. This step is crucial as it allows Windows to recognize Python automatically by adding it to the environment variables. The "X" in 3.X represents the digits of the available Python 3 version on the website, usually indicating the latest release. Keep in mind that the version you see may differ from what we show in this tutorial because Python gets updated over time. But worry not, the validity of this tutorial and the following steps remains intact πŸ™‚.

pff0-2.png

Moving on, let us choose the Customize installation option (as shown in the picture above). In the Optional Features section, we recommend keeping all the checkboxes checked (see picture below). This ensures you have access to all the handy features.

pff0-3.png

Finally, in the advanced options, make sure to tick the install for all users box (see picture below). If you are installing Python version 3.11.X, consider changing the installation path to C:\Python311. This will help avoid any permission hiccups that sometimes occur on Windows.

pff0-4.png

Ready? It is time to click on Install 😁. Once the installation is complete, you can close the installer (see picture below).

pff0-5.png

Now, let us check whether Python has been installed and configured correctly. Open a command prompt by typing cmd in the taskbar search on Windows and pressing enter (see picture below).

pff0-7.png

In the command prompt, type the following command: python --version (with two hyphens) and press enter.

pff0-6.png

This step should display your Python version (see picture above). If you encounter an error message indicating that Python is not recognized, try using the command python3 --version instead. Do not worry if you still face an error. A simple solution could be restarting your PC. If the problem persists after that, uninstall Python and then reinstall it, making sure you follow all the previous steps, especially the ticking of the Add Python to PATH box at the beginning of the installation process. Let’s also check that pip was installed correctly. Same as previously, try pip --version or pip3 --version. Pip is the python package manager, if you want to install any python package, you will have to use it. We will get to do that in a future chapter of this course.

You can now begin your first Python commands by typing python in a command prompt and then pressing the enter key. This will take you to a Python terminal (see picture below).

pff0-8.png

Note the three β€œ>>>” at the start of the new line, this tells you that you have indeed entered a Python terminal. From there, you can type the command print("Hello World") and press enter to display the text "Hello World" (see picture below). You can also perform operations like 2+3, which will give you the result 5.

pff0-9.png

Well done! You might have not realized it, but you have just run your first Python program! πŸŽ‰

VsCode installation

Let's head over to the official website at this link: https://code.visualstudio.com/download. From there, you can download the software that corresponds to your operating system.

Click on the downloaded file and proceed by clicking Next until you reach the window shown below.

pff0-10.png

Here, I would suggest checking all the boxes, as this is important for simplifying the software's usability later on. Then, click Next and Install.

Now that VsCode is installed, you can close the opened window.

Your First Python Program with VsCode

Let's create a folder, for example, on your Desktop, and name it python_training. Open this folder and right-click inside it. You should see an option Open with Code (see picture below), click on that. If you're using Windows 11, you can find this option in the More options menu.

pff0-11.png

Click on Yes, I trust the authors in the window that comes up, and then you can close the welcome tab (see picture below).

pff0-12.png

In the top left corner, click on the button to create a new file (see picture below). Name this file hello.py (.py is the file extension for executable Python files).

pff0-14.png

When you open this file, you might see a message popping up at the bottom right of the VsCode window prompting you to install the Python extension for VsCode. Accept as it enables VsCode to support the Python programming language. If you don't see this message, you can go to the extensions menu on the left and search for the Python extension, then install it (see picture below).

pff0-15.png

Next, return to your hello.py file, and start editing it. Fill it with the following code:

print("Hello World")

You should have a β€œplay” button in the top right corner to run your hello.py file (see picture below (1)).

pff0-16.png

Clicking on it should open a terminal automatically at the bottom of the VsCode window, executing the command to run your Python program. As a result, you will see "Hello World" displayed in the terminal (see picture above (2)).

Congratulations πŸŽ‰, you have just run your first Python program in VsCode πŸ˜‰!

Creating a Notebook

When you want to create a program that aims to automate an action for example, you will almost always write your program in a .py file, as we just saw. However, there is another type of file that greatly facilitates initial testing, data exploration, and analysis. These files are called Jupyter Notebooks. They work in the form of cells that can be executed one after another, making them also great pedagogical and presentation tools. We will be using these files extensively in this training.

To create a Jupyter notebook, simply create a new file as we did before. This time, name it hello.ipynb (.ipynb is the extension for Jupyter notebooks).

VsCode should also prompt you to install an extension to support this type of file. If it doesn't, you can search for and install the Jupyter extension (see picture below).

pff0-17.png

Now, go back to your hello.ipynb file and fill the first cell with the same command as before print("Hello World").

pff0-18.png

This time, the arrow to execute the cell is located on the left side (see picture above). Clicking on it should display Hello World. You can also use the shortcut Ctrl + Enter to execute the currently edited cell. If you receive any update notifications from VsCode, such as prompts to update your Python kernel, make sure to proceed with the updates.

To add a new cell below, hover your cursor at the bottom of the current cell and a + Code icon will appear, alternatively you can also click on the + Code (see picture below). Click on it and write print(2+3) in this new cell, execute it, and it should display 5.

pff0-19.png

Other useful VsCode extensions

To further enhance your coding endeavours, we recommend installing the Python Extension Pack (see picture below). This package gathers several extensions that will help you write code more efficiently and clearer.

pff0-20.png

Conclusion

Well done! You have successfully installed Python, set up VsCode, and run your first Python program. Whether you are working with Python files or Jupyter Notebooks, you now have the tools to explore the exciting world of programming.

In the next session, we will delve into variables, which form the foundation of every computer program. Get ready for more learning and discovery!