\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{{\kern-4mu}\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{\kern-4mu}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} \newcommand\pound{\unit{lbs}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{\kern-4mu}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{\kern-4mu}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Capacitance \newcommand\farad{\unit{F}} \newcommand\F{\farad} \newcommand\microfarad{\micro\farad} \newcommand\muF{\micro\farad} % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

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.

  1. What similarities did you notice between the team’s codes? What differences?

  2. For each team member, rank your comfort level in the following skills from 1 (not comfortable) to 5 (most comfortable).

    1. Creating for loops in Python

    2. Creating while loops in Python

  3. What is a real world problem that you can solve using for loops and/or while loops in a program?

  4. 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:

  1. Similar to the existing UDF build_matrix, create a new UDF named build_numpy_matrix that uses the NumPy library to create and return a 2D NumPy array of the specified dimensions containing numbers in numerical order.

  2. Create a new UDF named exponentiate_with_for. It should take in a standard python array, X, and a power, p as inputs. It will then use nested for loops (like you did in traverse_with_for) to raise each element of the array to the power of p and return the resulting array.

  3. Create a new UDF named exponentiate_with_numpy. It should take in a NumPy array, X, and a power, p as inputs. It will then use NumPy’s built-in method to raise each element of the array to the power of p and return the resulting array.

  4. 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:

    1. Build both types of matrices (Standard and NumPy) using the respective UDFs (build_matrix and build_numpy_matrix).

    2. Use both exponentiation UDFs to raise each element of the respective matrices to the user specified power.

    3. 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.

Table 13.5 Test Cases#

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

Table 13.6 Deliverables#

Deliverables

Description

py3_team_1_teamnumber.py

The modified Python code file.

py3_team_1_teamnumber.pdf

The completed team reflection document.