You are on page 1of 4

MN404 Fundamentals of Operating System and Programming

Laboratory 4: Understand the basic data types and precedence of operators

Introduction

In each lab you will work through a series of exercises that will help you to reinforce your
learning by applying and using the knowledge gained in the lecture class.

Remember - you are not expected to remember everything from your memory – you may do
a little research / reading of materials from the MIT resources available. You have to prepare
yourself for the lab as much as possible before the lab session.

Submission Due: End of laboratory class, submit the file on Moodle at least 10 minutes
before the end of laboratory class even if you have not completed all the given exercises.

Description of the laboratory exercise:

In this exercise, you will learn to run code in the interactive shell and Understand the basic
data types and precedence of operators

Python is an interpreted language, and you can run simple Python expressions and
statements in an interactive programming environment called the shell. The easiest way
to open a Python shell is to launch the IDLE (Integrated DeveLopment Environment).
This is an integrated program development environment that comes with the Python
installation. When you do this, a window named python Shell opens. A shell window
running on a Windows system or a Linux system should look similar, if not identical, to
this one. Note that the version of Python appearing in this screenshot is as shown below
in fig 1.

A shell window contains an opening message followed by the special symbol >>>, called
a shell prompt. The cursor at the shell prompt waits for you to enter a Python
command. Note that you can get immediate help by entering help at the shell prompt or
selecting Help from the window’s drop-down menu. When you enter an expression or
statement, Python evaluates it and displays its result, if there is one, followed by a new
prompt.

The next few lines show the evaluation of several expressions and statements.

July 2020 Prepared by: Dr Nandini Sidnal Moderated by - Prof. Savitri


Bevinakoppa
MN404 Lab 5: Data types, operators, Input and output 2

>>> 3 + 4
7
>>> 3 3
>>> "Python is really cool!"
'Python is really cool!'
>>> name = "Ken Lambert"
>>> name
'Ken Lambert'
>>> "Hi there, " + name
'Hi there, Ken Lambert'
>>> print('Hi there')
Hi there
>>> print("Hi there,", name)
Hi there, Ken Lambert

Editing, Saving, and Running a Script While it is easy to try out short Python expressions
and statements interactively at a shell prompt, it is more convenient to compose, edit,
and save longer, more complex programs in files. We can then run these program files or
scripts either within IDLE or from the operating system’s command prompt without
opening IDLE. Script files are also the means by which Python programs are distributed
to others. Most important, as you know from writ- ing term papers, files allow you to
save, safely and permanently, many hours of work. To compose and execute programs
in this manner, you perform the following steps:
1. Select the option New Window from the File menu of the shell window.
2. In the new window, enter Python expressions or statements on separate lines, in
the order in which you want Python to execute them.

July 2020 Prepared by: Dr Nandini Sidnal Moderated by - Prof. Savitri


Bevinakoppa
MN404 Lab 5: Data types, operators, Input and output 3

3. At any point, you may save the file by selecting File/Save.


If you do this, you should use a . py extension. For example, your first program file might
be named myprogram.py.
4. To run this file of code as a Python script, select Run Module from the Run menu or
press the F5 key.
The command in Step 4 reads the code from the saved file and executes it. If Python
executes any print functions in the code, you will see the outputs as usual in the shell
window. If the code requests any inputs, the interpreter will pause to allow you to enter
them.
Otherwise, program execution continues invisibly behind the scenes. When the
interpreter has finished executing the last instruction, it quits and returns you to the
shell prompt.
Exercises:
Task 1: Open a Python shell, enter the following expressions, and observe the results:
a. 8
b. 8 * 2
c. 8 ** 2
d. 8/12
e. 8 // 12
f. 8/0 2.
Task 2: Write a script in Python that prints (displays) your name, address, and telephone
number.

Task 3: Represent the following math expressions in python

i.

ii.

iii.
iv.

July 2020 Prepared by: Dr Nandini Sidnal Moderated by - Prof. Savitri


Bevinakoppa
MN404 Lab 5: Data types, operators, Input and output 4

v. y = 12.3x4- 9.1x3+ 19.3x2 - 4.6x + 34.2

vi.

July 2020 Prepared by: Dr Nandini Sidnal Moderated by - Prof. Savitri


Bevinakoppa

You might also like