You are on page 1of 9

Lab 5 Introduction to Python and IDLE

Part 1 Getting Familiar with 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

Activity 1: Now let’s start IDLE by doing the following:

 If you are running Windows, open IDLE as follows:


Start → All Programs → Python 3.7 → IDLE (Python GUI)

If you are running MacOS, open IDLE as follows:


Finder → Applications → Python 3.7 → 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.

Python Interactive Mode

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.

After Activity 1, you should see a window labelled "Python


Shell". If you see a window labelled Untitlted, then from
the Run menu choose Run → Python Shell. Your session
window should look something like the figure on the right.

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.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)
=
=

and verify your manual calculation by typing the command in Python

>>> (2.0/4.0)**(1//2)-(13.0/14.0)**(5//2-7//3)

If your manual calculation doesn’t match Python’s calculation, why?

Using Python Scripts

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
#--------------------------------------------------------------------

#Initialize the Celsius degree:


deg_c = 32

# formula to convert C to F is: (degrees Celsius) times (9/5) plus (32)


deg_f = deg_c * (9 / 5) + 32

print(deg_c, " degrees Celsius is", deg_f, " degrees Fahrenheit.")


 Now, save the script to a location of your choice. Call it CtoF.py, or some other name you prefer, as
long as it includes the .py extension. IDLE will not provide you with the .py extension
automatically.
 Then, run the script. Go to the Run menu and select Run → Run Module. This will run the
module and should produce the output in the interpreter.

#Copy and paste the output here

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.

Part 2: Python Variables and Statements

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

For example, the Python line

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.

1. create a variable price and assign 8.99 to price.


2. create another variable items and assign 10 to items.
3. create the third variable total_amount, which stores the calculation of price * items.
4. display the value of total amount.

#1.create a variable price and assign 8.99 to price


price = 8.99

# copy and paste the rest of statements here


Save the change and run the program. Python executes the first line, then the second line, and so forth
until it reaches the last line.

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

We can use a table to keep track of the values as they change.

Statements Values after statement executes


first second third
first = 2 2
second = 3 2 3
third = first * second 2 3 6
second = third - first 2 34 6
first = first + second + third 2 12 4 6
third = second * first 12 4 6 48

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:

Statements Value of x after statement executes


x = 10
x=x+x
x=x-3
x=x*4

Activity 7: Write a short Python program named heads-shoulders-knees-toes.py, to count heads,


shoulders, knees, and toes at a party. Your code must define five variables, one called people, one
called heads, one called shoulders, one called knees, and one called toes, equal to the number of people,
heads, shoulders, knees, and toes in total at the party. Your program should calculate the number of heads,
shoulders, knees, and toes, according to the number of people. Write your program in IDLE, and test
your program. Once you complete the program, copy and paste your code in the box below.

#---------------------------------------------------------------------
# Purpose:
# Programmer: Your Name
# Last updated:
#--------------------------------------------------------------------

# There are 100 people in the party


people = 100
knees = people * 2 #each person has two knees

# continue to write the rest of the code here

Part 3: Type Conversions and User Input

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.

Activity 8: Let’s display the type of the following values,

 “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

print(type(12)) # <class 'int'>

When you mix a float with an int, Python converts the int to a float, and then works with the two floats.

Activity 9: Try the following statements:

x = 1.2
y=2
z=x*y
print(x, y, z)
print(type(x), type(y), type(z))

Run the program. What are displayed?

#copy and paste the result here

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.

Activity 10:, Run the following statements,

x = "3.4"
print(x-1)

What error message did you get?

#copy and paste the error message here


We observe that x has value “3.4”, which is a string. The operator “-” needs numeric operands. In order
to make the calculation work, we have to cast “3.4” to 3.4 by typecasting. Now please fix the error so that
the calculation will work.

#copy and paste the fixed statements here

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)

What are the values of x and 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")

# What is the value of x? copy and paste the value of x here

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")

#copy and paste the error message here

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.

x = input(“Enter a number”) # a piece of user input


print(x * x)

#copy and paste the error message here.

#copy and paste the fixed program here.

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

You might also like