Lab 5 Introduction To Python and IDLE
Lab 5 Introduction To Python and IDLE
Python IDLE ("Integrated DeveLopment Environment ") is widely used by Python programmers. We
will use it often in our class too. Part 1 of this lab will guide you through the basics of using Python with
IDLE.
By now, IDLE should have been installed on your computer, which is automatically installed when you
install Python. If this is not the case, please follow Python Installation instructions in the Python Basics of
Week 4 Module and come back to this lab once you've installed the packages.
Starting IDLE
IDLE has two main window types, the Shell window and the Editor window. By default, the Python
Shell window will be opened when you start IDLE unless you have changed the setting to make Editor
window open when you start IDLE.
A "shell" is a computer program that lets you interact with a program or an operating system. We have
used Python shell from the command line last week. IDLE lets
you open a Python shell. In this section, we will review the
Python Interactive Mode from IDLE shell window.
Activity 2: As you did in the Lab 4, Python interactive mode can be used as a calculator; you can scroll
up and down in the shell session to look at the calculations you've done. Let’s do the following
calculation. Compare the results of the following quotients. Type them one at a time. Type the response in
the purple box under each command:
>>> 5.0/2.0
>>> 5/2
>>> 5//2
>>> 5.0//2.0
>>> 5 % 2
>>> (3.0**2)/(1.5**0.5)
What type of values are quotient and the remainder of two floating point numbers?
What type of values are the quotient and remainder of two integers?
What type of value are the quotient and remainder of a floating point number and an integer?
Activity 3: Show your work step by step when you do the following calculation manually
(2.0/4.0)**(1//2)-(13.0/14.0)**(5//2-7//3)
=
=
>>> (2.0/4.0)**(1//2)-(13.0/14.0)**(5//2-7//3)
We have seen that Python interactive mode is convenient for quick answers and calculations. But if you
need to chain a sequence commands together, it is much more efficient to save them to a file and use
IDLE to run the file. A set of Python commands in a file is called a script. The Python script tells the
Python interpreter what to do and in what order to do it.
In Python, comments are lines of text that are ignored by the Python interpreter but are included to help
explain your code to others and to remind yourself what your code is intended to do. Good code writing
requires good comments. Comments in Python start with a # sign. Any text following the # sign is
ignored by the interpreter.
Activity 4: Now we will write some commands to a file and run the file as a Python script.
First, open a new file from within IDLE, by going to the file menu and pointing to File → New.
In the script window, type the following commands:
#---------------------------------------------------------------------
# Purpose: Convert a Celsius degree to a Fahrenheit degree
# Programmer: John Smith
# Last updated: 09/10/18
#--------------------------------------------------------------------
When you selected Run Module, several things happened. Run Module is a command that asks IDLE
to translate the code into a form the computer can understand, load the translated code into the computer
memory, and run the commands in the script one at a time in the order they are written.
Variables are labeled storages for data in a program. Each variable has a unique name in the program. To
store information in a variable, we write a command using an equal sign in the following way:
variable_name = value
num = 5
stores the value 5 in the variable num. Then, anywhere you write the variable name num again, Python
retrieves the stored value.
Activity 5: Start IDLE and open the Edit window (File New). Create a new file and name it as
lab5_variables.py. Implement the following in Python.
Variable Tracing
You can simulate the memory storage of a computer with paper and pencil, by keeping track of the values
in a table.
first = 2
second = 3
third = first * second
second = third - first
first = first + second + third
third = second * first
At the end of the program, the value of first is 12, the value of second is 4, and the value of third is 48.
Drawing a table like this on pencil and paper is always a good idea and helpful when understanding or
fixing code.
Activity 6: Now, let’s trace the value of x after these commands execute. Please fill in the table below:
#---------------------------------------------------------------------
# Purpose:
# Programmer: Your Name
# Last updated:
#--------------------------------------------------------------------
In Python, values are classified into different types, such as string, int, float, and so on. If we are not sure
what class a value falls into, Python’s type() function can tell us. For example, type(12) returns <class
'int'>, indicating that 12 is an integer.
Now, let’s start Python IDLE. You may get the answers for the following activities from either Python
interactive mode or script mode.
“Hello World!”
23
12.343
Please copy and paste your statements in the colored box below and show the type as comments.
#Enter your statements here
#For example
When you mix a float with an int, Python converts the int to a float, and then works with the two floats.
x = 1.2
y=2
z=x*y
print(x, y, z)
print(type(x), type(y), type(z))
It is often necessary to change data from one type to another type by using a typecast function. You write
the name of the desired new type in the same way as a function call.
x = "3.4"
print(x-1)
Activity 11: Converting a float to an int loses the information after the decimal point, test the following
statements:
x = int(1.234)
y = int(-34.7)
print(x, y)
#x
#y
Activity 12: Converting a str to an int causes an error if the string is not formatted exactly like an integer.
Test the following statement:
x = int("1.234")
Activity 13: Converting a str to a float causes an error if the string is not a number, test the following
statements:
x = float("sandwich")
A common use of typecasting is to convert user input, which is always a string, to numerical form.
Activity 14: The following program is trying to calculate the square of a number that the user entered.
Test it in script mode and fix the error.
Once you completed this lab, please save it as Lab 5 firstname lastname.docx or Lab 5 firstname
lastname.pdf and submit it to the Blackboard