Professional Documents
Culture Documents
Laboratory 04 - Data Types and Expressions
Laboratory 04 - Data Types and Expressions
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. 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.
>>> 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.
i.
ii.
iii.
iv.
vi.