Typically, learning a programming language begins with writing and displaying the text “Hello World”. This simple program serves as an introduction to the basic functions of the Python language and allows you to become familiar with the programming environment you are using, such as VSCodium.
The “Print” function
The “print” function in Python is used to print data to standard output (the console or terminal by default). This can be a single string, a number, a variable, or the result of an operation. It is extremely useful for debugging code, allowing you to check the state of variables during program execution. It is also often used to display the results of a program operation.
Example of using the “print” function:
print("Hello World")
The above command will display the text “Hello World” on the standard output.
So much theory, now it’s time for some practice.
Creating the first “Hello World” program
“Hello World” in the Python interpreter
We start the terminal and go to the directory we created for the study.
By default, the terminal in macOS, as in Ubuntu, starts in the user’s home directory. That’s where I created my directory, which I navigate to with the command:
cd LearnPython
Then we activate the virtual environment we created:
source my_venv/bin/activate
Where my_venv is the name of my virtual environment.
We run the Python interpreter in a virtual environment:
python
Note that in a virtual environment, we can use the python command instead of python3 if that’s what we prefer. Outside the virtual environment, we use only the python3 command.
Then, in the Python interpreter, we type and execute the following command (by pressing “Enter”):
print("Hello World")
If we did everything right, we should see “Hello World” on the screen.
This is the simplest program that can be written in Python, and is often used to check that the Python environment is configured correctly.
As you can see, the interpreter works on the principle that the command is executed immediately and we see the output immediately. This mode is one of the most useful features, because after typing any valid function, we will immediately see the result.
We can also use it as a calculator:
Note: the variable “_” in the interpreter (and only here) stores the result of the last operation.
The Python interpreter can be quit as follows:
by entering the command: quit()
or in Linux or macOS by pressing Ctrl+D
“Hello World” in the VSCodium editor
We launch VSCodium.
We open the folder:
We select the menu: File > Open Folder
or in Linux we press: Ctrl+O
or in macOS we press: Command+O
We select the folder containing our project, in my case it will be the folder created in the previous post called “LearnPython”.
We start the terminal:
We select the menu: Terminal > New Terminal
or in Linux we press: Ctrl+Shift+`
or in macOS we press: Ctrl+`
A terminal will open in the location of our folder. Now we can activate the previously created virtual environment by typing the following command:
source my_venv/bin/activate
Where “my_venv” is the name of our virtual environment.
We open the Python interpreter:
We select the menu: View > Command Palette...
or in Linux we press: Ctrl+Shift+P
or in macOS we press: Shift+Command+P
and select “Python: Select Interpreter”. Then, in the “Select Interpreter” window that opens, we select the Python interpreter that is installed in our enabled virtual environment (venv).
We open a new, empty Python file:
We select the menu: File > New File
In the new file, we write the following code:
print("Hello World")
We save the file:
We select the menu: File > Save As
and type the file name with the .py extension, for example “hello_world.py”.
Now we can run the program in the terminal by typing:
python hello_world.py
and pressing “Enter”.
We can also run the program by clicking on the “Run Python File” icon in the upper right corner.
If we did everything right, we should see “Hello World” in the terminal.
Congratulations, we have just written and run our first program in Python using VSCodium.