B Sc Computer Science – VI Semester
Elective Papers - CS6PET01: Python and LateX
Module I - Introduction to Python
The Python Programming Language, Variables, Basic expressions and statements,
Arithmetic Operators, Data types - Type conversion, Numbers, Floats, String
operations
___________________________________________________________________
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.
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
Execution Modes
There are two ways to use the Python interpreter:
a) Interactive mode
VIJAYA GOPINATH M
,B Sc CS CS6PET01: Python and LateX Page |2
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
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
b) While working in the script mode, after saving the file, click [Run]->[Run Module] from the
menu as shown in Figure.
VIJAYA GOPINATH M
, B Sc CS CS6PET01: Python and LateX Page |3
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 below :
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
IDENTIFIERS
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 the above Table.
• We cannot use special symbols like !, @, #, $, %, etc., in identifiers.
For example,
1. 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
2. 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
VARIABLES
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., ‘a’, ‘Hello World’), 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.
Example : gender = 'M'
message = "Keep Smiling"
price = 987.9
VIJAYA GOPINATH M
Elective Papers - CS6PET01: Python and LateX
Module I - Introduction to Python
The Python Programming Language, Variables, Basic expressions and statements,
Arithmetic Operators, Data types - Type conversion, Numbers, Floats, String
operations
___________________________________________________________________
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.
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
Execution Modes
There are two ways to use the Python interpreter:
a) Interactive mode
VIJAYA GOPINATH M
,B Sc CS CS6PET01: Python and LateX Page |2
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
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
b) While working in the script mode, after saving the file, click [Run]->[Run Module] from the
menu as shown in Figure.
VIJAYA GOPINATH M
, B Sc CS CS6PET01: Python and LateX Page |3
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 below :
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
IDENTIFIERS
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 the above Table.
• We cannot use special symbols like !, @, #, $, %, etc., in identifiers.
For example,
1. 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
2. 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
VARIABLES
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., ‘a’, ‘Hello World’), 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.
Example : gender = 'M'
message = "Keep Smiling"
price = 987.9
VIJAYA GOPINATH M