002 – Python installation and first program

I am a Linux user and Python is installed in all Linux distributions. As I know, it is installed on Mac computers as well. Some of the Windows laptops come with Python installed. First, you have to check if you have Python installed. Open the terminal and run the command python3.

ddnro@ddnro-Linux-desktop:~$ python
Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3
ddnro@ddnro-Linux-desktop:~$ python3
Python 3.10.6 (main, Nov 2 2022, 18:53:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Notice what happens if you type in the terminal just python. In the past you would get the following:

python on Linux

Once Python is started you can type code directly in terminal. That would be just fine for now.

>>> 2+3
5
>>> 3>1
True
>>> 8>9
False
>>> 23-4
19
>>> (2+3)-1
4
>>> 5/2
2.5
>>> 9/4*2
4.5
>>>

As you can see, Python performs different tasks immediately after they are typed. The code you type in the terminal is not saved anywhere. The terminal is useful for testing purposes.

To write an entire program you have to use an editor. There is an editor that comes with Ubuntu (the Linux distribution I use) which is called gedit. You can use whatever text editor you want, the result will be the same.

So, open the editor and type the following line of code.

print('hello there')

Python first program

This is how my editor with the code looks like. You have to save it in the desired location and give the file the extension .py. I saved the file as hello.py in the Documents directory on my computer. Now, you can execute the file via terminal.

First, I changed the directory where my file is by running the command cd Documents. Once the directory is changed you can execute the Python program via terminal by running the command python hello.py. Below is the result.

Execute a Python program via Linux terminal

This is pretty much it. That’s how you can create a simple Python program.

Leave a Reply