0% found this document useful (0 votes)
98 views30 pages

Prelims Lab Exercise #9 - M2U3

The document describes exercises from a programming lab on installing and using Python programming tools. It includes steps to download and install Python, explore a simple Python program, use the debugger to step through a program, and add and call a function. The lab exercises introduce basic Python programming concepts like variables, strings, functions, conditionals, loops, and debugging tools.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views30 pages

Prelims Lab Exercise #9 - M2U3

The document describes exercises from a programming lab on installing and using Python programming tools. It includes steps to download and install Python, explore a simple Python program, use the debugger to step through a program, and add and call a function. The lab exercises introduce basic Python programming concepts like variables, strings, functions, conditionals, loops, and debugging tools.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Nimuel Vincent F.

Reyes
BSIT 101I

Prelims Lab Exercise #9- M2U3

Lab 9 / Using Programming Tools

Exercise 1 / Installing Programming Tool

1. Open a web browser and navigate to www.python.org/downloads

2. Click the Download Python button to download the latest version of Python for Windows.
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 is 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 Python02 files, and then copy them to
Documents folder.
Exercise 2 /’ 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.
 Line 1 declares a variable (user_name) and sets it to the string value “World”.
 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 concatenate
(join together) string 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.
 In line 3, we use the input() function to set the variable to a value entered by the user.
 Line 4 sets up a loop using the for statement. A variable named “counter” is used to
track progress through the stop. To determine the loop duration, the range() function
sets an initial value of 1, an exit value of 3, and a step value of 1. The intention is to run
the loop three times, but as we shall see the current code might not accomplish that.
 Note that 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 logic components, but will need to learn the specific
syntax of different development languages.
 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.
 Also in line 5, note the use of the str() function to convert the integer variable “counter” to
a string data type.
 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-clicks 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, select Debug > Debugger. A Debug Control window open 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 Over” processes the statement without pausing for each
part of any functions 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
exits the loop and processes line 7. Can you explain why the loop only executes twice?

think because it is set as that or that is the default.

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:
 Go—continue execution normally (unless a breakpoint is hit).
 Step—advance through statements and functions step-by-step. This is often also
referred to as “Step Into.”
 Over—execute the next statement without “following” any function calls.
 Out—complete any remaining parts within a function and pause at the next statement in
the main 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 languages are more likely to use brackets to delimit the statements belonging within a
function.
The previous code has been assigned to the function main(). The last line of code calls this
main() function if the script is executed directly. This structure allows the file containing
these functions 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 form.

4. In line 16, note the way that the ordinal_suffix() function is called. The function expects an
integer as input (an argument). The main() function is passing the value of the integer
variable “counter” to the 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. Look 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 blocks in line 4-9 and make sure you understand what they do:
 In the first if 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 I 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 value from 0 to
99 to determine the appropriate suffix.
 The second if statement check whether i 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.
 The else statement returns the correct suffix in any other case. It does this by using the
mode operator (%). Mod returns the remainder after division. For example, if i is 2 then
the remainder after division by 10 is also 2. If i 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