Category: Python3

Added: 28th of April 2020

Viewed: 467 times

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

The tutorial presumes you already have Python3 installed on your system. If you don't please visit my other tutorial, How to create a basic Python3 script in Ubuntu and Ubuntu based distributions

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.

Other Tips & Tutorials

Python3

Learning Python 3, check user input is an integer

Posted: 11th of December 2020

Views: 98

Python3

Learning Python3, Generate a random number using Python3

Posted: 8th of December 2020

Views: 106

Python3

Learning Python 3, getting basic user input and printing the results

Posted: 7th of December 2020

Views: 112

Python3

Learning Python3, System calls on Linux

Posted: 1st of December 2020

Views: 104

Python3

Create a new window using GTK and Python in Ubuntu and Ubuntu based distributions

Posted: 19th of November 2020

Views: 113

Python3

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

Posted: 28th of April 2020

Views: 467