Up to $140 off-Lowest  Prices of 2024(Nov.21-Dec.2)

My Computer Tips

Home | About | Categories | All Tips & Tutorials

How to create a basic Python3 script in Ubuntu and Ubuntu based distributions

ID: 158

Category: Python3

Added: 28th of April 2020

Updated On: Tutorial updated and rechecked on 25th of June 2022

Views: 3,215

Ubuntu and Ubuntu based distributions include Python3, so you can start programming without installing any additional packages.

In this example we will create a basic python script and run this through the terminal.

To start off, we can check what version of Python3 we have installed on our system, by issuing the following command in the terminal

python3 --version

As you can see from the screenshot, my system is running version


In order to create a Python script we need to find the path to the Python interpreter on our system, so issue the following command in the terminal
which python3



Next create a new file on your desktop and save as python.py
Next open the file and copy and paste the code below, noting the path to Python at the top of the script and save the file.
#!/usr/bin/python3
number1 = input("Enter your first number? ")
number2 = input("Enter your second number? ")
sum = int(number1) + int(number2)
print ("The answer is " +str(sum))

Now we need to make the file executable, so enter the following command in the terminal
chmod +x python.py

Now we can test the python script by entering the name of the file
./python.py

The script asks for two numbers via input, then adds the two numbers together.