Professional Documents
Culture Documents
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.
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. The latest version of Python can be downloaded from the following web:
https://www.python.org/downloads/
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.
>>> 3 + 4
7
>>> 3 3
>>> "Python is really cool!"
'Python is really cool!'
>>> name = "Ken Lambert"
March 2022 Prepared by: Dr Nandini Sidnal, Updated by Dr Leijia Wu,
Moderated by Prof. Savitri Bevinakoppa
MN4 Lab 5: Data types, operators, Input and 2
>>> 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 File 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.
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.
i.
ii.
iii.
iv.
vi.