Python
Offensive and Defensive Tool Construction
Table of Contents
EVALUATION:...............................................................................................................................3
Objectives......................................................................................................................................4
Background Reading.....................................................................................................................4
Important Information.....................................................................................................................4
Problem 1 (7pts)............................................................................................................................5
Introduction: Why Python?.........................................................................................................5
Problem 2 (7pts)............................................................................................................................6
Problem 3 (11pts)..........................................................................................................................6
Questions (show a screenshot for each of your answers if applicable):....................................7
Problem 4 (12pts)..........................................................................................................................8
Problem 5 (15pts)..........................................................................................................................9
EVALUATION:
1 Problem 1 7
2 Problem 2 7
3 Problem 3 11
4 Problem 4 12
5 Problem 5 15
TOTAL MARK 52
3 ICT: Offensive and Defensive Tool Construction
© 2017, Southern Alberta Institute of Technology
Offensive and Defensive Tool Construction
Python Programming I
Objectives
This lab focuses on the following objectives:
Explain the purpose of scripting languages and Python.
Explore the basic syntax of Python and compare it to C.
Use variables, expressions and statements in Python.
Explain function definitions and function calls (pure and with return value).
Background Reading
Read chapters 1–5 in How to Think Like a Computer Scientist: Learning with Python,
available at [Link]/thinkpython/[Link].
[Link]
Important Information
YOU MUST PRESENT IMAGES OF YOUR CODE BEING EXECUTED. DO NOT
SUBMIT YOUR ANSWERS IN THE DOCUMENT. CREATE A BLANK DOCUMENT
AND SUBMIT YOUR ANSWERS THERE.
YOU WILL LOSE MARKS FOR NOT FOLLOWING THE ABOVE
REQUIREMENTS.
All scripts must have the following elements:
1. File and Header comments, which follows the following format:
# Filename: m##[Link]
# Author: Taylor Swift
# Course: ITSC203
# Details: This program calculates the rate at which users allow themselves
# to be hacked.
# Resources: [Link]
2. Comments on lines where you used some unique computation that might be tricky to
comprehend a month later.
list1 = [x for x in range(20) if x % 4 == 1] # Using list comprehension to ….
4 ICT: Offensive and Defensive Tool Construction
© 2017, Southern Alberta Institute of Technology
Problem 1 (7pts)
Introduction: Why Python?
Both C and Python are high-level languages, and both need to be translated into machine code
before being executed.
In the table below, list 3 differences between the two languages. Also draw a simple diagram of
the path from source code to program execution: 5pts
C Program Python Program
C execution flowchart Python execution flowchart
You have learned how to program in C. Examine this simple program in C:
1. Write the equivalent program using Python. 2pts
5 ICT: Offensive and Defensive Tool Construction
© 2017, Southern Alberta Institute of Technology
Problem 2 (7pts)
Convert the following C program to the Python equivalent:
1. After converting the program, execute the code and provide screenshots of the program
running. 2pts
2. You will also provide your completed python code. 5pts
Problem 3 (11pts)
Open a terminal and follow the instructions below:
Note you may not have python3 installed if not you will need to install it:
sudo apt-get install python3
1. Type python3
2. Press Enter
a. The interface presented is called REPL (Read Evaluate Print Loop) interactive
environment. It is a single line interface that allows you to type python statements
in a similar way commands are entered in the Linux bash shell.
3. Take a screenshot of the information presented by the interpreter. 1pt
4. Type exit()
5. Type python3 -q
6. Press Enter
7. Take a screenshot of the information presented by the interpreter. 1pt
8. Type CTRL + D
6 ICT: Offensive and Defensive Tool Construction
© 2017, Southern Alberta Institute of Technology
9. Re-enter the python3 REPL interface
10. To enter the interactive help, Type help() and press the Enter key
a. Choose one of the following words (int, float, keyword, keyword, symbol,
topics) and enter it at the prompt. Observe the type of information shown.
b. Type q after you have finished reading the help
11. When you have returned to the REPL interface, Type help(tuple)
a. Read through the information provided. Take note of the different functions
available and press q to exit.
Questions (show a screenshot for each of your answers if applicable):
1. What version(s) of REPL were you able to launch in the steps above? 1pt
2. How can you return to the Linux terminal after finishing your testing in REPL? 1pt
3. How do you know that you are in the interactive shell and not at the Linux command
prompt? 1pt
4. How do you exit the interactive help? 1pt
5. Using help and your notes fill in the table below, by finding what LIST function performs
the actions in the table: 5pts
Functionality Required List function used
1. Get the number of times 12 is in the
list
2. Get the current position of the number
12.45
3. Reverse the order of the list
4. Add the Word “Property” to the
beginning of the list
5. Add the Word “Property” to the end of
the list.
6. Insert the word “Property” into the
middle of the list.
7. A. Remove the word “Really” from the
list
B. Remove all content from the list
6. Can the above actions be performed on a Tuple object?
7. Can the above actions be performed on a Set object?
7 ICT: Offensive and Defensive Tool Construction
© 2017, Southern Alberta Institute of Technology
Problem 4 (12pts)
HINT: You don’t need to go fancy here. It is sufficient to manually count the
length of each string. We will learn how to get the length programmatically later.
The exercise is more about printing, so focus on how to print and center strings
within a field.
Write a Python program named [Link] that prints the table shown below.
You have been given the following list that contains the user’s full name and the home directory
The table must be created such that the width of each column is only 2 spaces wider than the
longest string. Please pay attention to a few other items:
1. Notice that the names in the output table have to be titlecased.
2. Notice that the folder names capitalize the first letter of each name and also only use the
first initial of the first name.
3. You must use list as shown above and then generate the table above.
4. You must also use list methods to programmatically manipulate the text. Don’t
manually manipulate the strings.
5. Submit your code and an image showing the table output.
8 ICT: Offensive and Defensive Tool Construction
© 2017, Southern Alberta Institute of Technology
9 ICT: Offensive and Defensive Tool Construction
© 2017, Southern Alberta Institute of Technology
Problem 5 (15pts)
Analyze the following code and complete the following steps:
1. Find and fix any errors in syntax or logic 10pts
a. You can also remove lines that will not work, but you must explain why you
removed that line
2. Document what you did to make the program work: 3pts
a. Explain what was wrong.
b. Explain what you did to fix the issue.
3. Submit the corrected, commented code along with a screenshot of the program’s output.
2pts
10 ICT: Offensive and Defensive Tool Construction
© 2017, Southern Alberta Institute of Technology
© 2017, Southern Alberta Institute of Technology. All rights reserved.
This publication and materials herein are protected by applicable intellectual property laws.
Unauthorized reproduction and distribution of this publication in whole or part is prohibited.
For more information, contact:
Director, Centre for Instructional Technology and Development
Southern Alberta Institute of Technology
1301 16 Ave. N.W., Calgary, AB T2M 0L4