Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks

Thursday, 13 October 2016

Introduction to Python Programming on Linux

Hello readers! We are happy to announce a new tutorial series on Python- 'Python on Terminal', and this is the first article of the series. In this tutorial series, we will learn the basic building blocks in Python Programming and Python Scripting. Python is a really very simple language, that anyone with or without any knowledge of coding can grasp it quickly. So, in short, it will be great fun learning Python. Here we begin!


What are we going to cover in this article?

  • Introduction to Python
  • Origin of Python
  • Where do we use Python?
  • Why should everybody learn Python?
  • Some basic Python code sheet standards and code execution model
You must be wondering, especially if you are an IT guy, that everyone around you is talking about Python. And if your quest to find out what exactly it is, then you have come to the right place. You don’t need any special prerequisites to try Python, anybody can take hold of it pretty quickly, may it be experienced professionals or newbies. Python syntax is so clear and simple, that anyone working on it gets a feel of reading English. I just said 'try python', because some day, you will realize that, in order to accomplish something, where it needs a several pages of code in other languages, Python can do it in very minimal code and less time. As a result, your interest in Python will grow, day by day, making Python an integral part of your life.
       Enough of praise, before it seems like an exaggeration. We will start our discussion on Python straight away.

What is Python?

  1. This is a programming language as well as a scripting language, that supports OOP (object oriented programming) concept. In brief, we can call it as object oriented scripting-cum-programming language. OOP in Python- Don’t worry! We will be learning everything about it in the subsequent articles in this series.
  2. It is very easy to learn and use, improves developer productivity, provides quality codes.
  3. This is considered among the top 5 widely used programming languages in the world.
  4. This is open source language so does not need licensing.
  5. Two versions of Python are widely used in industry - Python2 and Python3.

Why should I learn Python?

  1. Every software engineer wants to enhance his/her skills, in order to maintain a continuous progress in his/her career, and basically wants to work on renowned technologies or ideas that are widely accepted all around the globe.
  2. So to lure you as to why you should learn python, here is some motivation.
    • Google, YouTube, Dropbox, Instagram - all of them use python extensively.
    • Intel, Cisco, HP, IBM use Python for hardware testing.
    • NASA use it for scientific programming tasks.
    • And used in lot many areas which I have not mentioned here. Feeling motivated?
  3. You can use python in Machine learning, Gaming, Image Processing, Robotics, Graphic designing, Web development, Search engines, etc..

Why is it named as Python?

Despite being all the 'python' images over all the books and materials of Python, the fact is, it is named after the British comedy group "Monty Python", who are the makers of 1970's BBC comedy series "Monty Python’s Flying Circus". Guido van Rossum is the creator of Python.

There are lot of technical differences between Python and other rivals that I have not mentioned here. These differences obviously makes Python the Winner in every bit of a specification. If you have a background in Software development life-cycle and want to know more distinguishing features of python then just Google it, you will get to know bunch of them. But in this tutorial series, we will unveil the Python features step by step, so that anyone new to programming world will find it easy to understand the concepts. Before starting the basics, we must know some background of how the Python code gets executed internally.

Our first Python code

Before we jump into anything, let's check which version of Python we are using. To do this, simply open your Linux terminal and type python -V or python --version, in my case it's 'Python 2.7.5'.

$ python -V
Python 2.7.5

$ python --version
Python 2.7.5

A Python script is a simple text file containing all of our code. But on Linux, we should include the first line in our file as #!/usr/bin/python, and this is called as "Shebang" or "Hashbang". With this line, Linux shell will come to know that, the interpreter to be used to execute the code below it, is Python interpreter located at /usr/bin/python. Normally, this path will be common for most of us, but you can check the path to python interpreter by running which python command on your Linux terminal.

$ which python

So, for our first python code, we will use print command, that takes a parameter that will be printed on the terminal screen. Here is how the file contents look like-

$ cat

print("I ride on a road with a code")

Then, we need to make this file executable using chmod command in Linux as below-

$ chmod +x

Now, time to run our Python script. We can do this in two ways, as below-

$ ./


$ python

And we should get the following output-

I  ride on a road with a code

Bamm! The print statement will display the contents included in the parenthesis.

Code execution overview

Python is not just a programming language or not only a scripting language, but also an interpreter.

What is Interpreter?

This is the software level sandwiched between the processor and the user. It understands the type of the program you want to execute. It reads the instruction from the program and execute it. As pointed out earlier, Python script is just a plain text file with the properly indented code and should be saved with extension .py. Indentation is a very crucial aspect in Python programming, if your code is not properly indented, Python will give out errors. I repeat -

Remember : Whitespace indentation has a very important role to play in Python. If not used properly, it may throw syntax errors or even change the meaning of the code and the program might misbehave.

Now, there is lot more happening in background with the execution of a Python code. When you run a Python code, it will be converted to something called Bytecode. Bytecode is platform-independent code which is not human readable, but machine understandable. This bytecode file is automatically saved onto the system by name with extension .pyc. So as you have guessed correctly our file is first converted to the myFirstCode.pyc internally and saved in the same directory as of the main file. This is the intermediate steps toward complete python code execution. Now this .pyc code (byte-code) is being run by something called PVM (Python virtual machine).

PVM is just a big code which loops in the byte code to run through it, understands it and carries out the actual processing. Every time you run your python program with the source (.py file) unchanged, then the compilation step will be excluded from the processing and the Python uses byte-code directly saving the resources. To identify the source code change, python maintains the timestamp for last code changes. Python very well knows when to compile the source code to generate the byte code. And there are two instances when this happens, first- when the source code changes and the second- when the python version changes.

That's all for the scope of this article. In the next article, we will learn how to install Python on Linux and create our first 'Hello World' program.

This article is originally published at - Introduction to Python


Post a Comment