You are on page 1of 22

PT1: Prelims Lab Exercise #8- M2U3 

 
 
Exercise 1 / Installing Programming tools 
 
1) Open a web browser and navigate to www.python.org/downloads

  
 
2) Click the Download Python button to download version of Python for Windows. Note that the
exact version of Python available may vary over time.

 
 

3) When prompted, in the download bar at the bottom of the browser window, click the Save
button. 

 
 
4) When the file has downloaded and the security scan has complete, in the download bar, click the
Run button.

 
 
5) In the Python Setup window, click Install Now.

 
 
6) In the user Account Control window, click Yes. 
 
7) Setup will continue for a minute or two. When the “Setup was Successful” message is shown,
click Close.

  
 
8) In the Browser, open the following URL:  
     https://s3.amazonaws.com/comptia-learning/python.zip 
 
9) When prompted, in the download bar at the bottom of the browser window, click the Save
button. 

 
10) When the file has downloaded and the security scan is complete, in the download bar, click the Open
button.  
 
11) Click-and-drag to select the Python01 and the Python02 files, and then copy them to the
Documents folder.
  
 
Exercise / Exploring a Simple Program  
 
1) Click in the Instant Search box and type idle. From the search results, click IDLE (Python 3.x 32-
bit)

  
 
2) Type the following function. As you type, note how tooltips appear to help you complete the statement
and how color-coding is used to validate your syntax (purple for the function name and green for the
string input).   
Print (‘Hello World’) 
 
 
3) Press ENTER to execute the statement. The string “Hello World” is “Printed” as output to the
shell.

  
 
4) From the File menu, select Open. Click the Documents object then double-click the Python01
file.  The program code is opened in an editor window.
 

 
5) Take a few moments to identify what each line of this program does. The line position of the cursor is
shown in the status bar or you can press ALT+G to select a particular line:  
 
o Line 1 declares a variable (user_name) and sets it to the string value “World”. 
 
o Line 2 uses the print () function we saw earlier but the output is constructed from both static
strings and the variable we declared. Note the use of the + sign to concentrate (join together) strings
literals and variables. Note also that we use double quotes to delimit the literal strings. This allows us
to use the single quote (‘) as an apostrophe within the string. 
 
o In line 3, we use the input () function to set the variable to a value entered by the user. 
 
o Line 4 sets up a loop using the for statement. A variable named “counter” is used to track
progress through loop. To determine the loop duration, the range () function sets as an initial value of
1, an exit value of 3, and a set up value of 1. The intention is to run the loop three times, but as we
shall see the current code might not accomplish that. 
 
o Note the use of a colon to complete the line containing the for statement. While “For” type
constructs in general are common to all programming languages, this statement syntax is specific to
Python. It will often be the case that you may understand the general operation of logic components,
but will need to learn the specific syntax of different development languages.  
 
o Lines 5 and 6 are executed during the loop. Note that these lines are indented. In Python,
indentation is used to structure the code and is critical to compilation and execution. In many other
programming languages, indentation is used for clarity but whitespace is ignored by the interpreter or
compiler. 
 
o Also, in line 5, note the use of the str () function to convert the integer variable “counter” to a
string data type. 
 
o Line 7 is executed once the loop has completed and is also the final statement in the procedure. 
 
6) Press F5 to execute the program. 

 
7) In the shell window, respond to the prompts by typing any names you wish and pressing
Enter. 

 
8) Count how many times the loop executes – is this a surprise?

 
 
9) Leave all the Python windows open. 
 
Exercise 3 / Using the Debugger 
1) In the editor window, right-click line 5 (the first statement in the for loop) and select Set
Breakpoint.

 
 
2) In the editor window, right-click line 7 (the last line) and select Set Breakpoint.

 
 
3) In the shell window, select Debug > Debugger. A Debug Control window opens and the
shell shows the message “DEBUG ON”. 
 
4) In the editor window, press F5 to run the program.

 
 
5) In the Debug Control window, click the Step button.

 
6) Click the Step button.

 
 
7) Click the Out button. Using “Step Out” completes processing of the function and
advances execution to the next statement (line 3). 
 
8) Click the Over button. Using “Step Out” processes the statement without pausing for
each part of any function it contains. 

 
9) Switch to the shell window, click at the prompt, type any name, and press ENTER. 
 
10) In the Debug Control window, note the new value of the user_name variable. Click the
Go button.

 
 
11) Click the Go button. 
 
12) Switch to the shell window, click at the prompt, type any name, and press Enter.
 
 
13) In the Debug Control window, Click the Out button.

 
 
14) Switch to the shell window, click at the prompt, type any name, and press Enter. 

 
15) In the Debug Control window, click the Out button. Note the value of counter as the program
exist th loop and the process line 7. Can you explain why the loop only executes twice? 

 
16) In the editor window, change the line to read as follows: 
      for counter in range (1, 3+1, 1) : 
 
17)Save the file then press F5 to run it again.

 
18) Step through the program to confirm that the loop executes three times. 

 
19) Take a moment to review the use of the debug tools: 
 
o Go – continue execution normally (unless a breakpoint is hit). 
 
o Step – advance through statements and functions step-by-step.  This is often also reffered to as
“Step Into” 
 
o Over – execute the next statement without “following” any function calls. 
 
o Out – complete any remaining parts within a function and pause at the next statement in the
program flow.

 
 
  20) Close the Python editor window. 
 
  21) In the shell window, select Debug > Debugger. The debugger is disabled. 
 
Exercise 4 / Using a Function 
 
1) From the File menu, select Open. Click the Documents object then double-click the
Python02 file. The program code is opened in an editor window. 

 
2) You can see the new function has been added in the first ten lines. Note the use of “def” to declare the
function and the use of indents to show which statements belong to the function. Other languanges are
more likely to use brackets to delimit the statements belonging within a function. 
The previous code has been assigned to the main function (). The last line of code calls the main ()
function if the script is executed directly. This structure allows the file containing this function to be
imported by another module without necessarily running the main () code. 
 
3)Note that line 1 contains a comment (#) with attribution for the source code that this function is derived
from. 

4) In line 16, note the way that the ordinal_suffix () function is called. The function expects
an integer as an input (an argument). The main () function is passing the value of the
integer variable “counter” ordinal_suffix () function.
 
 
5)In line 2 we use an array-like construction to store the list of ordinal suffixes associated with 1,2 and 3.
This type of Python list is called a dictionary. Note that the list is declared outside of any function
definition, making it available to any code in the same module.

 
 
6) Lok at line 3. The function assigns its own variable (i) to the integer value. Note that ordinal_suffix ()
does not manipulate the value of the “counter” variable. 
 
7)Look at the if conditional block in lines 4-9 and make sure you understand what they do:

  
 
o In the first statement, we check whether the integer is over two digits long. If it is, we truncate it,
using functions to convert between integer and string and back. For example, if the value of it is 112,
this statement will convert it to 12. The code snippet [-2:] is a slice, which is the method used in
Python to return part of a string (in this case the last two characters). We’re doing this because we
only need to evaluate the values from 0 to 99 to determine the appropriate suffix. 
 
o Second if the statement checks whether it has the value of 11, 12, or 13 because in these special
cases, the suffix is “th” not “11st” or “12nd”. The code uses the comparison operator less than (<) to
accomplish this. 
 
o The else statement returns the correct suffix in any other case. It does this by using the mod
operator (%). Mod returns the remainder after division. For example, if it is 2 then the remainder after
division by 10 is also 2. If is 22, the remainder is still 2. The get () function is used to lookup this
remainder value in the SUFFIX_DICT list. If the value is not found (if it is zero or four for instance),
“th” is returned by default. 

 
 
     8) Use F5 to run the new program and test that it returns correctly formatted suffixes in the output. 
 
9)Change the value of range (1,3+1,1) to test different integers. For example, try range
(109,124,1). 
 
     10) Close all open windows and apps.

You might also like