Lab 3 - Introduction of Python and IDLE
Lab 3 - Introduction of Python and IDLE
Introduction
This lab is designed to develop basic understanding with python and its
development environment IDLE.
Objectives
The objective of this exercise is to become familiar with the Python IDE for
version 3.x while introducing basic mathematical operations, variable types, and
printing options.
Tools/Software Requirement
Python IDLE
Description
Follow the lab manual step by step. As you proceed, you will be asked to add
screenshots/snaps of your results inside the provided output windows within this
manual for grading purposes.
Introduction to Python:
Python is a widely used high-level programming language used for general-
purpose programming, created by Dutch programmer Guido van Rossum and first
released in 1991. Its syntax allows programmers to express concepts in fewer
lines of code than possible in languages such as C++ or Java.
Before opening IDLE, it is worth recalling that there are three basic types of
simple variables in Python: integers (whole numbers, commonly used as an
index), floats (that is, numbers with a decimal point, AKA real numbers), and
strings (collections of alphanumeric characters such as names, sentences, or
numbers that are not manipulated mathematically such as a part number or zip
code). A legal variable name must start with a letter. It is then optionally followed
a=2.3
name=“Joe”
It is best to think of the equal sign as “gets”. That is, think of the first example as
“the variable a gets the floating point value 2.3” and the second as “the variable
name gets the string Joe”. An assignment command such as these literally
reserves space in the computer’s memory for the variable and tags it with the
variable name. Then, it stores the appropriate value at that location for future use.
Lab Work :
Open IDLE by selecting Python from the Start menu and then choosing the option
to open IDLE (Python GUI). Do NOT open the command line. Alternately, open
the IDLE (Python GUI) icon on the desktop. A simple text window known as the
Python shell will open. It should have a white background with a text message at
the top and immediately below that, a cursor prompt >>>
The shell serves two functions. First, it can serve as a sort of scratch pad to try
snippets of code (shown in the steps below). Second, it can serve as the text
output window for larger programs. Do not try to use this window for the creation
of complete programs that you wish to save. We shall use this window to create a
few variables and perform some basic manipulations on them.
a=5
The >>> should reappear. This command defines a variable called a and copies
the integer value 5 into it. In similar manner, type in the following commands:
b=13 x=5.0 y=13.0 m=“Mary” n=“Nancy” It is very important that the “.0”
portions be included. This is how integers and floats are distinguished: floats
always have a decimal point, integers don’t. Also, it is possible to define the
In any case, the computer’s memory now looks something like this:
The trick now, of course, is to access these values, manipulate them, and see the
results. An important command for this process makes use of the print function.
The print function will print whatever is passed into it, either variables or
expressions, on to the output window. Note that like all built-in commands and
functions in Python, this function is all lower case. Capitalizing it will generate an
error. Also, note that commands will be color coded as orange-red.
Now type:
print( a )
[1 Mark]
Output of print(a)
Add snap here.
Now type:
print( a, x, m, n )
[1 Mark]
Output of print(a, x, m, n)
Add snap here.
print( a + b )
[1 Mark]
Output of print(a+b)
Add snap here.
This results in the value 18. This line retrieves the values of a and b from
memory, adds them together, and prints the result on the output window. Neither
a nor b are altered in the process. Alternately, we could have created a brand new
variable and printed it. The result will be the same. Enter the following two lines
to verify this:
c=a+b
print( c )
[1 Mark]
Output of print(c)
Add snap here.
The only difference is that this version adds a new “slot” called c to the memory
map above. It is worth noting that once a variable is created, its value may be
recomputed over and over if desired. For example, type the following:
c = 20 + a
print( c )
[1 Mark]
The result should be 25. The first line computes a new value which then
overwrites the prior value of 18.
a=(b+c)*d
Remember, think of the equal sign as “gets” as in “a gets the value computed by
the expression”. It is an assignment, not a true mathematical relation. That is, if at
some point in the future the value of b was to change, a will not automatically be
altered to reflect that change. This allows you to do the following:
c=c+1
Type this in. What do you think the result will be?
What the above statement says is, “Retrieve the current value of c, add one to it,
and store the result back in c (overwriting the original value). Print out the value
of c. You should get 26 (the prior value of 25 plus one). Continuing with the
other math operators, type:
print( y/x )
[1 Mark]
Output of print(y/x)
Add snap here.
print( b/a )
The result is also 2.6 even though both variables are integers (an integer, of
course, can’t contain a fractional portion). In essence, Python promotes the
variables to floats in order to maintain precision, producing a floating point
answer. Now try:
print( b/x )
[1 Mark]
Output of print(b/x)
Add snap here.
In this case the answer is again 2.6. This is because in a mixed calculation
between a float and an integer, the integer is again promoted to a float in the
calculation in order to maintain the precision of the floating point variable.
Python can be run in one of two modes. It can either be used interactively as
explained above, via an interpreter, or it can be called from the command
line/Scripted Mode to execute a script.
1. Open IDLE
OR explicitly define the type of file by writing .py extension at the end of the
name
6. See the name of the file has been changed with the .py extension
7. Now Go to Run >> Run Module OR enter F5 on your keyboard to run the code in
Scripted Mode
Write programs for the following problems. Make use of the best
programming practices (documentation/variable naming conventions e.t.c)
discussed in the class to gain maximum marks.
[3+1=4 marks]
1. Write a program in the scripted mode that adds three numbers (of your choice)
together. Use variables to store the values in your program and consider the
following rules.
Condition: You cannot add more than two numbers in a single line i.e.
a+b is fine.
[3+1=4 marks]
3. Mr. Shahwaiz has provided you with the electricity bills for past three months.
The bills for the last three months have been $67, $48 and $94. Write a
program that computes average monthly electricity bill over the three month
period. Using variables write an appropriate expression to calculate the mean,
and use print( ) to display the result.
Source Code( average_electricity_bill.py)+ Output Snap
Add your source code here.
Grade Criteria
This lab is graded. Min marks: 0. Max marks: 20.