Page 1
5.1 Introduct Ion to Python We have written 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 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. 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 instructions
into machine language, so that it can be understood
by the computer. 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.
“Computer programming
is an art, because it applies
accumulated knowledge
to the world, because it
requires skill and ingenuity,
and especially because
it produces objects of
beauty. A programmer who
subconsciously views himself
as an artist will enjoy what he
does and will do it better.”
– Donald Knuth
Chapter 5
Getting Started with
Python
In this chapter
» Introduction to
Python
» Python Keywords
» Identifiers
» Comments
» Data Types
» Operators
» Expressions
» Statement
» Input and Output
» Type Conversion
» Debugging
Ch 5.indd 87 08-Apr-19 12:35:10 PM
2024-25
Page 2
5.1 Introduct Ion to Python We have written 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 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. 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 instructions
into machine language, so that it can be understood
by the computer. 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.
“Computer programming
is an art, because it applies
accumulated knowledge
to the world, because it
requires skill and ingenuity,
and especially because
it produces objects of
beauty. A programmer who
subconsciously views himself
as an artist will enjoy what he
does and will do it better.”
– Donald Knuth
Chapter 5
Getting Started with
Python
In this chapter
» Introduction to
Python
» Python Keywords
» Identifiers
» Comments
» Data Types
» Operators
» Expressions
» Statement
» Input and Output
» Type Conversion
» Debugging
Ch 5.indd 87 08-Apr-19 12:35:10 PM
2024-25
Computer SCien Ce – Cla SS xi 88
5.1.1 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 relatively
simple structure.
• Python is case-sensitive. For example, NUMBER
and number are not same in Python.
• Python is portable and platform independent,
means it can run on various operating systems and
hardware platforms.
• Python has a rich library of predefined functions.
• Python is also helpful in web development. Many
popular web services and applications are built
using Python.
• Python uses indentation for blocks and
nested blocks.
5.1.2 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 5.1:
Downloading Python
The latest version of
Python 3 is available on
the official website:
https://www.python.org/
Figure 5.1: Python interpreter or shell
In the above screen, 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.
Ch 5.indd 88 08-Apr-19 12:35:10 PM
2024-25
Page 3
5.1 Introduct Ion to Python We have written 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 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. 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 instructions
into machine language, so that it can be understood
by the computer. 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.
“Computer programming
is an art, because it applies
accumulated knowledge
to the world, because it
requires skill and ingenuity,
and especially because
it produces objects of
beauty. A programmer who
subconsciously views himself
as an artist will enjoy what he
does and will do it better.”
– Donald Knuth
Chapter 5
Getting Started with
Python
In this chapter
» Introduction to
Python
» Python Keywords
» Identifiers
» Comments
» Data Types
» Operators
» Expressions
» Statement
» Input and Output
» Type Conversion
» Debugging
Ch 5.indd 87 08-Apr-19 12:35:10 PM
2024-25
Computer SCien Ce – Cla SS xi 88
5.1.1 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 relatively
simple structure.
• Python is case-sensitive. For example, NUMBER
and number are not same in Python.
• Python is portable and platform independent,
means it can run on various operating systems and
hardware platforms.
• Python has a rich library of predefined functions.
• Python is also helpful in web development. Many
popular web services and applications are built
using Python.
• Python uses indentation for blocks and
nested blocks.
5.1.2 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 5.1:
Downloading Python
The latest version of
Python 3 is available on
the official website:
https://www.python.org/
Figure 5.1: Python interpreter or shell
In the above screen, 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.
Ch 5.indd 88 08-Apr-19 12:35:10 PM
2024-25
Gettin G Started with Python 89
5.1.3 Execution Modes
There are two ways to use the Python interpreter:
a) Interactive mode
b) Script mode
Interactive mode allows execution of individual
statement instantaneously. Whereas, Script mode
allows us to write more than one instruction in a file
called Python source code file that can be executed.
(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(s), as shown in Figure 5.2.
Figure 5.2: Python interpreter in interactive mode
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 file name has
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
prog5-1.py, we type prog5-1.py. We can otherwise
open the program directly from IDLE as shown in
Figure 5.3.
b) While working in the script mode, after saving the
file, click [Run]->[Run Module] from the menu as
shown in Figure 5.4.
Ch 5.indd 89 08-Apr-19 12:35:11 PM
2024-25
Page 4
5.1 Introduct Ion to Python We have written 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 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. 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 instructions
into machine language, so that it can be understood
by the computer. 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.
“Computer programming
is an art, because it applies
accumulated knowledge
to the world, because it
requires skill and ingenuity,
and especially because
it produces objects of
beauty. A programmer who
subconsciously views himself
as an artist will enjoy what he
does and will do it better.”
– Donald Knuth
Chapter 5
Getting Started with
Python
In this chapter
» Introduction to
Python
» Python Keywords
» Identifiers
» Comments
» Data Types
» Operators
» Expressions
» Statement
» Input and Output
» Type Conversion
» Debugging
Ch 5.indd 87 08-Apr-19 12:35:10 PM
2024-25
Computer SCien Ce – Cla SS xi 88
5.1.1 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 relatively
simple structure.
• Python is case-sensitive. For example, NUMBER
and number are not same in Python.
• Python is portable and platform independent,
means it can run on various operating systems and
hardware platforms.
• Python has a rich library of predefined functions.
• Python is also helpful in web development. Many
popular web services and applications are built
using Python.
• Python uses indentation for blocks and
nested blocks.
5.1.2 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 5.1:
Downloading Python
The latest version of
Python 3 is available on
the official website:
https://www.python.org/
Figure 5.1: Python interpreter or shell
In the above screen, 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.
Ch 5.indd 88 08-Apr-19 12:35:10 PM
2024-25
Gettin G Started with Python 89
5.1.3 Execution Modes
There are two ways to use the Python interpreter:
a) Interactive mode
b) Script mode
Interactive mode allows execution of individual
statement instantaneously. Whereas, Script mode
allows us to write more than one instruction in a file
called Python source code file that can be executed.
(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(s), as shown in Figure 5.2.
Figure 5.2: Python interpreter in interactive mode
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 file name has
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
prog5-1.py, we type prog5-1.py. We can otherwise
open the program directly from IDLE as shown in
Figure 5.3.
b) While working in the script mode, after saving the
file, click [Run]->[Run Module] from the menu as
shown in Figure 5.4.
Ch 5.indd 89 08-Apr-19 12:35:11 PM
2024-25
Computer SCien Ce – Cla SS xi 90
Program 5-1 Write a program to show print statement
in script mode.
Figure 5.3: Python source code file (prog5-1.py)
5.2 Python Keywords Keywords are reserved words. 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 5.1.
Table 5.1 Python keywords
False class finally is return
None continue for lambda try
c) The output appears on shell as shown in
Figure 5.5.
Figure 5.4: Execution of Python in Script mode using IDLE
Figure 5.5: Output of a program executed in script mode
Ch 5.indd 90 21-May-19 11:57:33 AM
2024-25
Page 5
5.1 Introduct Ion to Python We have written 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 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. 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 instructions
into machine language, so that it can be understood
by the computer. 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.
“Computer programming
is an art, because it applies
accumulated knowledge
to the world, because it
requires skill and ingenuity,
and especially because
it produces objects of
beauty. A programmer who
subconsciously views himself
as an artist will enjoy what he
does and will do it better.”
– Donald Knuth
Chapter 5
Getting Started with
Python
In this chapter
» Introduction to
Python
» Python Keywords
» Identifiers
» Comments
» Data Types
» Operators
» Expressions
» Statement
» Input and Output
» Type Conversion
» Debugging
Ch 5.indd 87 08-Apr-19 12:35:10 PM
2024-25
Computer SCien Ce – Cla SS xi 88
5.1.1 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 relatively
simple structure.
• Python is case-sensitive. For example, NUMBER
and number are not same in Python.
• Python is portable and platform independent,
means it can run on various operating systems and
hardware platforms.
• Python has a rich library of predefined functions.
• Python is also helpful in web development. Many
popular web services and applications are built
using Python.
• Python uses indentation for blocks and
nested blocks.
5.1.2 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 5.1:
Downloading Python
The latest version of
Python 3 is available on
the official website:
https://www.python.org/
Figure 5.1: Python interpreter or shell
In the above screen, 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.
Ch 5.indd 88 08-Apr-19 12:35:10 PM
2024-25
Gettin G Started with Python 89
5.1.3 Execution Modes
There are two ways to use the Python interpreter:
a) Interactive mode
b) Script mode
Interactive mode allows execution of individual
statement instantaneously. Whereas, Script mode
allows us to write more than one instruction in a file
called Python source code file that can be executed.
(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(s), as shown in Figure 5.2.
Figure 5.2: Python interpreter in interactive mode
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 file name has
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
prog5-1.py, we type prog5-1.py. We can otherwise
open the program directly from IDLE as shown in
Figure 5.3.
b) While working in the script mode, after saving the
file, click [Run]->[Run Module] from the menu as
shown in Figure 5.4.
Ch 5.indd 89 08-Apr-19 12:35:11 PM
2024-25
Computer SCien Ce – Cla SS xi 90
Program 5-1 Write a program to show print statement
in script mode.
Figure 5.3: Python source code file (prog5-1.py)
5.2 Python Keywords Keywords are reserved words. 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 5.1.
Table 5.1 Python keywords
False class finally is return
None continue for lambda try
c) The output appears on shell as shown in
Figure 5.5.
Figure 5.4: Execution of Python in Script mode using IDLE
Figure 5.5: Output of a program executed in script mode
Ch 5.indd 90 21-May-19 11:57:33 AM
2024-25
Gettin G Started with Python 91
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
5.3 Ident If Iers In programming languages, identifiers are names used
to identify a variable, function, or other entities in a
program. The rules for naming an identifier in Python
are as follows:
• The name should begin with an uppercase or a
lowercase alphabet or an underscore sign (_). This
may be followed by any combination of characters
a–z, A–Z, 0–9 or underscore (_). Thus, an identifier
cannot start with a digit.
• It can be of any length. (However, it is preferred to
keep it short and meaningful).
• It should not be a keyword or reserved word given
in Table 5.1.
• We cannot use special symbols like !, @, #, $, %,
etc., in identifiers.
For example, to find the average of marks obtained
by a student in three subjects, we can choose the
identifiers as marks1, marks2, marks3 and avg rather
than a, b, c, or A, B, C.
avg = (marks1 + marks2 + marks3)/3
Similarly, to calculate the area of a rectangle, we can
use identifier names, such as area, length, breadth
instead of single alphabets as identifiers for clarity and
more readability.
area = length * breadth
5.4 Var Iables A variable in a program is uniquely identified by a name
(identifier). Variable in Python refers to an object — an
item or element that is stored in the memory. Value
of a variable can be a string (e.g., ‘b’, ‘Global Citizen’),
numeric (e.g., 345) or any combination of alphanumeric
characters (CD67). In Python we can use an assignment
statement to create new variables and assign specific
values to them.
n otes Ch 5.indd 91 08-Apr-19 12:35:11 PM
2024-25
Read More