CHAPTER -5
GETTING STARTED WITH PYTHON
Introduc on to python
We have wri en algorithms for different problems in Chapter 4. Let us now move a step further
and create programs using any version of Python 3. But before learning about Python programming
language, let us understand what is a programming language and how it works.
An ordered set of instruc ons 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 instruc ons 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- l e v e l language. However, it is difficult for humans to write or comprehend instruc ons
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 wri en in a high-level language is called source code. Recall from Chapter 1
that language translators like compilers and interpreters are needed to translate the source
code into machine language. Python uses an interpreter to convert its instruc ons into
machine language, so that it can be understood by the computer.
Features of Python
Python is a high level language. It is a free and open source language.
It is an interpreted language, as Python programs are executed by an interpreter.
Python programs are easy to understand as they have a clearly defined syntax and
rela vely simple structure.
Python is case-sensi ve. For example, NUMBER and number are not same in Python.
Python is portable and pla orm independent, means it can run on various opera ng
systems and hardware pla orms.
Python has a rich library of predefined functions.
Python is also helpful in web development. Many popular web services and applica ons are
built using Python.
Python uses indenta on 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.
,In the above screen, the symbol >>> is the Python prompt, which indicates that the interpreter
is ready to take instruc ons. We can type commands or statements on this prompt to execute
them using a Python interpreter.
Execu on Modes
There are two ways to use the Python interpreter:
Interac ve mode
Script mode
Interac ve Mode: Interac ve mode allows execu on of individual statement instantaneously.
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(s), as shown in Figure
Working in the interac ve mode is convenient for tes ng a single line code for instant execu on.
But in the interac ve mode, we cannot save the statements for future use and we have to retype
the statements to run them again.
Script mode: Script mode allows us to write more than one instruc on in a file called Python
source code file that can be executed.
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 file name has extension “.py”.
By default, the Python scripts are saved in the Python installa on folder.
While working in the script mode, a er saving the file, click [Run]->[Run Module] from the
menu as shown in Figure.
Python source code file
, Execu on of Python in Script mode using IDLE
Output of a program executed in script mode
Python Keywords:
Keywords are reserved words. Also known as pre-defined word.
Each keyword has a specific meaning to the Python interpreter, and we can use a keyword in our
program only for the purpose for which it has been defined.
As Python is case sensitive, keywords must be written exactly as given in Table.
False class finally is return
None continue for lambda try
True def global not with
and del if or yield
as elif from nonlocal while
assert else import pass
break except in raise
Identifiers:
Name given to programming element known as Identifier.
In programming languages, identifiers are names used to identify a variable, function, or other
entities in a program.
GETTING STARTED WITH PYTHON
Introduc on to python
We have wri en algorithms for different problems in Chapter 4. Let us now move a step further
and create programs using any version of Python 3. But before learning about Python programming
language, let us understand what is a programming language and how it works.
An ordered set of instruc ons 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 instruc ons 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- l e v e l language. However, it is difficult for humans to write or comprehend instruc ons
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 wri en in a high-level language is called source code. Recall from Chapter 1
that language translators like compilers and interpreters are needed to translate the source
code into machine language. Python uses an interpreter to convert its instruc ons into
machine language, so that it can be understood by the computer.
Features of Python
Python is a high level language. It is a free and open source language.
It is an interpreted language, as Python programs are executed by an interpreter.
Python programs are easy to understand as they have a clearly defined syntax and
rela vely simple structure.
Python is case-sensi ve. For example, NUMBER and number are not same in Python.
Python is portable and pla orm independent, means it can run on various opera ng
systems and hardware pla orms.
Python has a rich library of predefined functions.
Python is also helpful in web development. Many popular web services and applica ons are
built using Python.
Python uses indenta on 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.
,In the above screen, the symbol >>> is the Python prompt, which indicates that the interpreter
is ready to take instruc ons. We can type commands or statements on this prompt to execute
them using a Python interpreter.
Execu on Modes
There are two ways to use the Python interpreter:
Interac ve mode
Script mode
Interac ve Mode: Interac ve mode allows execu on of individual statement instantaneously.
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(s), as shown in Figure
Working in the interac ve mode is convenient for tes ng a single line code for instant execu on.
But in the interac ve mode, we cannot save the statements for future use and we have to retype
the statements to run them again.
Script mode: Script mode allows us to write more than one instruc on in a file called Python
source code file that can be executed.
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 file name has extension “.py”.
By default, the Python scripts are saved in the Python installa on folder.
While working in the script mode, a er saving the file, click [Run]->[Run Module] from the
menu as shown in Figure.
Python source code file
, Execu on of Python in Script mode using IDLE
Output of a program executed in script mode
Python Keywords:
Keywords are reserved words. Also known as pre-defined word.
Each keyword has a specific meaning to the Python interpreter, and we can use a keyword in our
program only for the purpose for which it has been defined.
As Python is case sensitive, keywords must be written exactly as given in Table.
False class finally is return
None continue for lambda try
True def global not with
and del if or yield
as elif from nonlocal while
assert else import pass
break except in raise
Identifiers:
Name given to programming element known as Identifier.
In programming languages, identifiers are names used to identify a variable, function, or other
entities in a program.