Introduction to Python in Linux

Introduction to Python in Linux

Python is a very powerful and useful programming language, gaining popularity around the world due to it’s simplicity and great ability to run on all systems and platforms. Via simple packages it is possible to do pretty much everything you can imagine with it, starting with mathematical analysis, artificial intelligence, machine learning, front and back-end development (user interface applications and servers), and much more!

Firstly, open up your Linux terminal.  It will automatically open up to your working directory. To view the files and folders in this directory just type I or ls (for list) to get an output. I will assume that you won’t see much in yours just yet, so let’s type the command mkdir test. This command creates a new folder for you (make directory). If you want to change into this new folder, simply type cd test. Again you can view all files and folder within this folder by typing I or ls and hitting Enter. Let’s create our first file by typing sudo nano test.txt. Sudo is the Linux way of saying that you are admin and that you own the rights to do so. Nano is the name of a shell-based Text Editor, it’s the Linux way of saying Microsoft Word. The last bit of the command is the name of your file, test, with the extension .txt, making it a text file. An empty window should pop up; this is your new blank text document. Into this document you can now type anything you want, just like you would in word, let’s type “My first document”. Now to save this press CTRL X. A prompt will appear to ask you if you are sure, you will then say yes with a “y” and continue with hitting Enter. This will bring you back to your “test” folder. Type ls to see what’s in this folder, our new document “test.txt” should appear. As you are a big fan of backups, let’s create a quick backup of or document by typing cp test.txt backp.txt. This command consists of the command cp (copy), the file we want to copy and the new filename. I just saw – we misspelled our backup file, that’s not good, so let’s rename it! Type mv backp.txt backup.txt. Mv (move) will not copy but replace the new file with the old file, making it the same effect of renaming it. Now we want to delete our test.txt file, because we only want to keep the backup, this is done with the command rm test.txt – (remove). If it prompts you to confirm your wish to delete it, type yes.

If you wish to return to your home directory, the command cd will always get you there, if you only want to move up one directory have cp followed by a space and two dots (cp ..).

I would recommend you to try this for a couple of minutes to get comfortable with this way of navigating.

 

Example Program hello_me.py

Go into your home directory (cd) and create a new file (sudo nano …). Replace the (…) by hello_me.py to create your first python program. Again, the file editor should pop up with an empty document. Let’s insert the following program code:

1       #This is a comment, it won’t be executed
2
3       name = input(“Please tell me your name: “)
4       print(“Hello %s, how are you?” % name)

Return to the folder with CTRL X, Y and Enter and execute your program by typing -> python hello_me.py

Output:

Please tell me your name: Mats
Hello Mats, how are you?

 

Explanation:

Line 1 consists of a comment, everything behind a hashtag will be ignored

Line 2 empty lines are no problem, they will be ignored

Line 3 – we will prompt the user for an input after the text “Please tell me your name: “.

The input will be saved to the variable name.

Line 4 – we will print out some text, which is always found in between “text“. The %s is telling our program that there is a variable input needed, which is found behind the next %-sign. The ‘s’ in this command refers to string, if you want to pass a number use %d.

 

Lists in Python:

Now we are going to work in the live python mode. In any directory type python. This will start your python 2.x. To test your program working type print(“I am working”). The output should look as follows:

>>> print("I am working")
I am working

Once this is complete, let’s create our first two lists, first empty, second already filled. Type:

>>> emptylist = []
>>> fulllist = [1,2,3,4,5]
>>> emptylist
[]
>>> fulllist
[1,2,3,4,5]

But I hate empty lists, so let’s add the values 9 and 10 to the list:

>>> emptylist.append(9)
>>> emptylist.append(10)
>>> emptylist
[9, 10]

Let’s get back to the fulllist and learn to find ojects in a list.

Find the first and second object:

>>> fulllist[0]
1
>>> fulllist[1]
2

But now, how to find the last object? There is a simple and a complicated way to do so. We know that the last digit is at the same place as the list is long minus 1. To find the length of our list type

>>> len(fulllist)
5

To be able to to know find the last and second to last place of this list you could type

>>> fulllist[len(fulllist) – 1]
5
>>> fulllist[len(fulllist) – 2]
4

But as I said before, there is an easier way to do this, let me introduce browsing lists by range. With this feature you can find all different placements of a list. The following examples will show you the first 3, the last one, the last three and the second to third.

>>> fulllist[:3]
[1, 2, 3]
>>> fulllist[-1:]
[5]
>>> fulllist[-3:]
[3, 4, 5]
>>> fulllist[1:3]
[2, 3]

Next up we want to output the last number and remove it from the list, the command for this is called pop.

>>> fulllist.pop()
5
>>> fulllist
[1, 2, 3, 4]

Now the first item from the list

>>> fulllist.pop(0)
1
>>> fulllist
[2, 3, 4]

For now, I want to leave it at that – the rest we will bring up as we go – I am sure you love to get right into it?

 

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s