Apr 28, 2026 | 561 words | 6 min read
13.2.1. Task 1#
Learning Objectives#
Create and execute simple scripts comprised of basic Python concepts. Apply course code standard in development of Python scripts. Modularize and comment code for readability and reusability.
Task Instructions#
Reflection#
Compare your results for the pre-task assignment with your team members (Task 0). Run each member’s code and compare results with the given solutions. Discuss the differences and similarities of your approaches, highlighting strengths of each.
Choose one team member’s pre-class activity working code file as the basis for the following reflection questions.
What similarities did you notice between the team’s codes? What differences?
For each team member, rank your comfort level in the following skills from 1 (not comfortable) to 5 (most comfortable).
Creating
forloops in PythonCreating
whileloops in Python
What is a real world problem that you can solve using
forloops and/orwhileloops in a program?Which working code did the team decide to submit and why? What are the strengths of this approach?
Save your answers to the reflection as py3_team_1_teamnumber.pdf and turn it in with the remainder of the team task assignment files in Gradescope.
Code Modification#
Modify the chosen team member’s code (in a new python file) as described below:
Similar to the existing UDF
build_matrix, create a new UDF namedbuild_numpy_matrixthat uses theNumPylibrary to create and return a 2DNumPyarray of the specified dimensions containing numbers in numerical order.Create a new UDF named
exponentiate_with_for. It should take in a standard python array,X, and a power,pas inputs. It will then use nestedforloops (like you did intraverse_with_for) to raise each element of the array to the power ofpand return the resulting array.Create a new UDF named
exponentiate_with_numpy. It should take in aNumPyarray,X, and a power,pas inputs. It will then useNumPy’s built-in method to raise each element of the array to the power ofpand return the resulting array.In your main function, ask for user input for the number of rows and columns to build both types of matrices and the power to which to raise each element. Then:
Build both types of matrices (Standard and NumPy) using the respective UDFs (
build_matrixandbuild_numpy_matrix).Use both exponentiation UDFs to raise each element of the respective matrices to the user specified power.
Print the resulting matrices to the console.
Note
As you can tell, using NumPy makes matrix operations like exponentiation much
simpler and more efficient compared to using nested for loops. This is one of
the key advantages of using libraries like NumPy for numerical computations in
Python.
Another important aspect to consider is NumPy’s performance advantage. You may
want to measure and compare the execution time of both methods for larger matrices to
see the efficiency difference.
You can use the time module to measure the execution time of each method. Here
is a simple way to do it (do not need to include this in your final code submission,
it’s just for your understanding):
import time
def compare_run_times():
rows, cols, power = 2000, 2000, 3
regular_matrix = build_matrix(rows, cols)
numpy_matrix = build_numpy_matrix(rows, cols)
start_time = time.time()
exponentiate_with_for(regular_matrix, power)
for_time = time.time() - start_time
print(f"Time taken for standard matrix exponentiation with FOR loops: {for_time:.6f} seconds")
start_time = time.time()
exponentiate_with_numpy(numpy_matrix, power)
numpy_time = time.time() - start_time
print(f"Time taken for NumPy matrix exponentiation: {numpy_time:.6f} seconds")
If you call this function in your main, it will give you an idea of the performance difference between the two methods.
Sample Output#
Use the values in Table 13.5 below to test your program.
Case |
Rows |
Columns |
Power |
|---|---|---|---|
1 |
3 |
3 |
3 |
2 |
2 |
5 |
4 |
3 |
4 |
4 |
0 |
Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.
Case 1 Sample Output
$ python3 py3_team_1_teamnumber.py Enter Matrix Dimensions Enter rows: 3 Enter columns: 3 Enter exponent power: 3 Standard Matrix Exponentiation with FOR loops: [[1, 8, 27], [64, 125, 216], [343, 512, 729]] NumPy Matrix Exponentiation: [[ 1 8 27] [ 64 125 216] [343 512 729]]
Case 2 Sample Output
$ python3 py3_team_1_teamnumber.py Enter Matrix Dimensions Enter rows: 2 Enter columns: 5 Enter exponent power: 4 Standard Matrix Exponentiation with FOR loops: [[1, 16, 81, 256, 625], [1296, 2401, 4096, 6561, 10000]] NumPy Matrix Exponentiation: [[ 1 16 81 256 625] [ 1296 2401 4096 6561 10000]]
Case 3 Sample Output
$ python3 py3_team_1_teamnumber.py Enter Matrix Dimensions Enter rows: 4 Enter columns: 4 Enter exponent power: 0 Standard Matrix Exponentiation with FOR loops: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] NumPy Matrix Exponentiation: [[1 1 1 1] [1 1 1 1] [1 1 1 1] [1 1 1 1]]
Deliverables |
Description |
|---|---|
py3_team_1_teamnumber.py |
The modified Python code file. |
py3_team_1_teamnumber.pdf |
The completed team reflection document. |