Writing your first Python3 Script in Linux


To begin with this script, please make sure you have python3 installed.

To check if you have python3 installed, press Ctrl+Alt+T in linux.

The terminal will open up. Then type

python --version

If python is installed on your computer you make get the results as:

Now lets begin writing the script, type

python #probably opens up python2
#or to use python3 type

With this you should be in the python console.

Now try writing any python code.

print('My first Python Code') #prints My first Python Code

The python code will work but if you make any mistake here, it's hard to correct it this way. So, we will create a file, write all the code in that file then execute that.

So, lets begin

#if you are still in the console
exit() #this will close python conole

Let's create a file

touch script.py

This will create a file script.py in the existing directory

To edit this file you can use any code editor of your choice. I will use nano here.

nano script.py

Now that I am in the console, I can write the code here.

To make the script autonomous you can add a shebang like

#!/usr/bin/env python3 

This should be your first line in the code because it will tell the operating system which interpreter to use when executing it.

print('Hello World!')

Now the file will be like

#!/usr/bin/env python3
python('Hello World')

Now save the file by pressing Ctrl+X then Y

Before we can execute it, we need to change the file permission

chmod +x script.py

Notice, you may need admin priviledges, so use sudo command.

Now that we are ready to execute, so we type


This should give you the output in the terminal. If you didn't get it, it's okay , keep trying and repeat the procedure.


Peace out

Post a Comment

Post a Comment (0)

Previous Post Next Post