Introduction to Python

Before learning about the Python programming language, let us understand what is a programming language and how it works.

An ordered set of instructions to be executed by a computer to carry out a specific task is called a program, and the language used to specify this set of instructions to the computer is called a programming language.

As we know that computers understand the language of 0s and 1s which is called machine language or low-level language. However, it is difficult for humans to write or comprehend instructions using 0s and 1s. This led to the advent of high-level programming languages like Python, C++, Visual Basic, PHP, Java that are easier to manage by humans but are not directly understood by the computer.

A program written in a high-level language is called source code. Language translators like compilers and interpreters are needed to translate the source code into machine language.

Python uses an interpreter to convert its instructions into machine language so that they can be understood by the computer.

Interpreter vs Compiler

An interpreter processes the program statements one by one, first translating and then executing. This process is continued until an error is encountered or the whole program is executed successfully. In both the cases, program execution will stop. On the contrary, a compiler translates the entire source code, as a whole, into the object code. After scanning the whole program, it generates error messages, if any.

Features of Python

  • Python is portable and platform-independent, which means it can run on various operating systems and hardware platforms.
  • Python is high level, free and open-source language.
  • It is an interpreted language, as programs written in Python are executed by an interpreter.
  • It is easy to understand programming language, as its syntax structure is very simple.
  • Python is case-sensitive, for example, ALPHA and alpha are not the same in Python.
  • It has a rich library of predefined functions.
  • It uses indentation for blocks and nested blocks.

Working with Python

To write and run (execute) a Python program, we need to have a Python interpreter installed on our computer or we can use any online Python interpreter. The interpreter is also called Python shell. A sample screen of Python interpreter is shown in figure below.

Python Interpreter/Shell

In the image above, the symbol >>> is the Python prompt, which indicates that the interpreter is ready to take instructions. We can type commands or statements on this prompt to execute them using a Python interpreter.

Execution Modes

We can use the Python interpreter in two ways:
a) Interactive Mode
b) Script Mode

A) Interactive Mode

To work in the interactive mode, we can simply type a Python statement on the >>> prompt directly. As soon as we press enter, the interpreter executes the statement and displays the result, as shown in the figure below.

Working in the interactive mode is convenient for testing a single line code for instant execution. But in the interactive mode, we cannot save the statements for future use and we have to retype the statements to run them again.

B) Script Mode

In the script mode, we can write a Python program in a file, save it and then use the interpreter to execute it. Python scripts are saved as files where the file name has the extension “.py”. By default, the Python scripts are saved in the Python installation folder. To execute a script, we can either:

a) Type the file name along with the path at the prompt. For example, if the name of the file is prog1.py, we type prog1.py. We can otherwise open the program directly from IDLE as shown in the figure below.

Fig. Python Source code file (prog1.py)

b) While working in the script mode, after saving the file, click [Run]->[Run Module] from the menu as shown in the figure below.

Fig. Execution of Python in Script mode using IDLE

c) The output appears on the shell as shown in the figure below.

Fig. Output of a program executed in script mode

Quiz

Results

-

#1. Python is a ____________ Programming Language.

#2. A program written in a high-level language is called __________.

#3. Language translators like ............................. is/are needed to translate the source code into machine language.

#4. Python uses .................. to convert its instructions into machine language.

#5. In the .................., we can write a Python program in a file, save it and then use the interpreter to execute it.

#6. The extension of Python file name is .............

Submit Answers