Getting Started with Python in Linux

Getting started with Python in Linux

There is no need to install Python on Linux as it comes preinstalled. After opening the Terminal, the first thing to do is to create a folder that will contain all of the Python files.
To create a folder named Python, type ‘mkdir Python’.

mkdir Command

After creating the Directory, we are now ready to create our first “Hello World” program.
To get to the Directory that was created in the previous step, use the command “cd Python”

Change Directory Command

Once we have created and moved into the Directory, we are now ready to start our first program on Linux.
To create the our first file, we are going to use the ‘Touch’ command, and name our file something that is easy to remember for later on. In this instance we are going to call the file, “Hello.py”. We have the .py extension because it is a Python File.
When we use the ‘Touch’ Command in Linux. It doesn’t look like anything has happened. However, if you type ‘ls’ and press enter. This will list all files in the current directory, and as you can see from the screenshot below, there is currently only one file in the directory.

Demonstrating File Creation and Listing in the Terminal

It is now time to edit the file, and to do this we are going to use the VIM editor. Similar to before we are going to type ‘vim’ followed by the document name. e.g. vim Hello.py
It may be difficult to see in the image below. However, there are some key things to point out. Firstly, in the bottom left corner of the window, you can see the file name that we are currently working on.
Secondly, vim opens in a ‘read-only’ mode. For us to be able to edit it, we need to press ‘I’ on the keyboard, which will allow us to start editing the document. When you start editing the document, the file name will disappear from the bottom left of the screen and be replaced with the word ‘Insert’. This lets us know that we can start making changes.

Vim Editor

You can write anything you want for your first program using Python. Type the following code:
print(“A string of Text”)
Once you are happy with the code that you have written, we need to exit vim and go back to the Terminal window. To exit vim, enter the following command: Esc key :wq!
That command will convey to the editor, that you have finished typing and that you wish to write the changes to file and quit.
It is now time to run our Python Program. To do this type: python3 Hello.py
It will display the string of text in the Terminal window, as shown below.

Output of First Python Program in Linux

Congratulations, you have just learnt some basic navigation through the Linux Terminal and created your first program in Python using Vim.

Published by Daniel Wray

IT Support Engineer; Studying for CompTIA Network+. 100 Days of Python Challenge. Aspiring blogger. Passion for Learning and Teaching! https://ko-fi.com/danielwray4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: