Python is an interpreting language, which means Python Programs are executed by an interpreter. Python interpreter can be started simply by typing python at the Linux Terminal/console, which gives you the python prompt. The python prompt >>> gives you an interface to write and run your code directly from the prompt. For the sake of simplicity, treat >>> as python prompt and $ as Unix/Linux Prompt, for this post and the rest of the post to follow on Python series. Python (and all other interpreter, in general) runs code line by line. We will now dive into details of python programming, but before that, make sure you have gone through Learning Python: Introduction to Python Programming Language.

Learning Python: Write your first program in python and know the underlying concept

Start Python Interpreter

Sample Output

Start Python Interpreter

You can start writing your codes just after the python prompt. You can exit/quit the python prompt simply by typing exit() or quit() on a new line in python interpreter.

Type exit() to leave interpreter

exit in python

Type quit() to leave interpreter

quit in python

Write your First Program in Python

As per the standard practice lets write a simple “Hello, world!” Program. Write the below line of code in Python prompt and hit return key.

Sample Output

Hello World Program

You can write code in a file and ask python to execute it. Writing codes to a file is recommended as it gives you flexibility, especially when your code spans over several lines. Also, you can come later to edit and execute it. Not only that, but also you can reuse those codes in another program.

How to write and run python programs from a file?

The python program is a normal text file with a .py extension. Following are the steps to write and run a python program from a file.

    1. open an Atom text editor

Atom Text Editor

    1. open a new file (File > New File) to edit and save (File > Save As) it as Remember python works best with a .py extension.
    2. Start with python shebang line. Shebang commonly referred to as hashbang, hashpling, pound bang or crunchbang refers to ‘#!’. They are the interpreter directive as the first line of the text file. In the Unix/Linux OS presence of the above characters indicates that it is a script. The program is then interpreted as a script specified by the rest of the first line of the code. Python shebang is

Though a lot of people use the below shebang, but that is less flexible and hence not recommended.

Also, if you have several versions of python installed, /usr/bin/env will ensure that python interpreter uses this on environment’s $PATH.

So now you know what python shebang is, you must always include it in your first line.

    1. A lot of python programmer gets the error about ASCII encoding. Especially when you are from a country where Unicode UTF-8 is not the standard. Even if you have Unicode UTF-8 in default, adding the below line won’t harm. Instead, this will make your program portable across Unix systems irrespective of the locale/country you belong. Here is the line you should have on second line of python program.

    1. Write the code, you want python to interpret for you, starting from third line. For “Hello, world!” program, write

    1. save the file (File > save).
    2. Execute the python script, simply as

Sample Output

Hello World Python Program from file

Comments in Python

Comments are very important in any program. Use of suitable comments makes your program readable. In large programs, comments make it possible to edit/change/understand a block or overall program. Python interprets anything and everything after ‘#’, a comment. You can put a # followed by text in English, about what it does. Also, if you want to disable a block, you may just put a ‘#’ before the code. Remember, you need to put a ‘#’ on all the lines of a block, which you want to disable temporarily so that Python interpreter consider that as comments and ignore. Also, comments can be used on the same line as code, but anything that lies right of ‘#’ is ignored.

Example of comments in Python

Here “# This is a comment line” and “# Again a comment line” are comments. It makes no difference to the output of the program, other than making it readable. Execute this yourself and see what you gets.

Don’t consider that ‘#’ on the first and second line of the above program as comments. Although the interpreter still ignores it but you may consider it as a hack or workaround that it serves the purpose.

Quotes in Python

As you have already seen in the above example that print statement prints everything that is enclosed in double quotes(“”). Python supports a varied range of quotes implementation and mostly they are interchangeable. The only rule is they have to be enclosed in matching quotes.

Here is an example to demonstrate the use of quotes in python.

save the above code as and execute. It is pretty easy to understand the use of quotes if you remember the one and only rule i.e., always enclose in matching quotes.

Sample output

quotes in python

That’s all for now. I’ll come here with next post of python series soon. Till then stay healthy and tuned to JoinTux. Do let us know in the comments, if you liked this post. Like and share us and help us get spread.


Please enter your comment!
Please enter your name here