Skip to content

IPP-DEV-02-005: Simple Sequences #62

@labbenchstudios

Description

@labbenchstudios

Description

  • Using instructions from the in-class lecture, create a module to test some simple sequence operations using list, tuple, and range.

Review the README

  • Please see README.md for further information on, and use of, this content.
  • License for embedded documentation and source codes: IPP-DOC-LIC

Estimated effort may vary greatly

  • The estimated level of effort for this exercise shown in the 'Estimate' section below is a very rough approximation. The actual level of effort may vary greatly depending on your development and test environment, experience with the requisite technologies, and many other factors.

Actions

Step 1: Make Sure Your System is Setup for Python and This Course

Step 2: REMINDER: Make Sure PYTHONPATH is Set Correctly

Whether running Python tests within your IDE or from the command line, you must set the PYTHONPATH environment variable in every execution environment (e.g., every terminal you launch) when attempting to run any of your scripts and their tests or the IPP test app from the command line. The IPP source and test paths will be as follows:

  • {your IPP source code path}
  • {your IPP source code path}/tests

See IPP-DEV-01-001 for details.

Step 3: Create your own module to test simple numeric calculations

  • Using your IDE, or from within a terminal, create a new Python file - a module - named SimpleSequences.py within the labmodule02 path (or package)
  • Open the module, and create the indicated test cases:

Test 1 (string iteration)

  • Create a comment at the beginning of the file:
    • For example:
    • # Test 1: string iteration
  • Declare the name variable as a string. Set the value to whatever name you'd like.
  • Iterate over each character in name using a for iteration call. Here's an example:
    • NOTE: Be sure to use 4 spaces to indent the print line after the for statement.
for c in name:
    print(c)
  • Print a newline (empty) to separate this test from the next one.

Test 2 (list iteration)

  • Add a blank line and then another comment for this next test:
    • For example:
    • # Test 2: list iteration and manipulation
  • Declare the class_items variable as a list. Set the contents of the list to the following:
    • ["notebook", "pen", "power supply"]
  • Append an item to class_items. Perhaps "jacket" or something else related to items you'd need for a class.
  • Iterate over each item in class_items using a for iteration call. It should include all your original items plus the one you just appended. Here's an example:
    • NOTE: Be sure to use 4 spaces to indent the print line after the for statement.
for item in class_items:
    print(f"Item: {item}")
  • Print a newline (empty) to separate this test from the next one.

Test 3 (tuple iteration)

  • Add a blank line and then another comment for this next test:
    • For example:
    • # Test 3: tuple iteration (and failed manipulation)
  • Declare the immutable_class_items variable as a tuple. Set the contents of the tuple to the following:
    • ("notebook", "pen", "power supply")
  • Iterate over each item in immutable_class_items using a for iteration call. It should include all your original items plus the one you just appended. Here's an example:
    • NOTE: Be sure to use 4 spaces to indent the print line after the for statement.
for item in immutable_class_items:
    print(f"Item: {item}")
  • Try to append an item to immutable_class_items. Perhaps "jacket" or something else related to items you'd need for a class.
    • This will fail when you attempt to execute your code. You can handle the exception by adding the following code in place of the append call:
try:
    immutable_class_items.append("jacket")
except:
    print(f"Tuples are immutable! Can't append.")
  • Print every item in the list again to show that you did not successfully append to the tuple (which, again, is immutable).
  • Print a newline (empty) to separate this test from the next one.

Test 4 (range iteration)

  • Add a blank line and then another comment for this next test:
    • For example:
    • # Test 4: rangeiteration
  • Create three new range iterations, as follows:
    • simple_range, which will create a range() instance with a single parameter (stop): Suggestion: try (10)
    • bounded_range, which will create a range() instance with two parameters (start and stop). Suggestion: try (1, 11)
    • stepwise_range, which will create a range() instance with three parameters (start, stop, and step). Suggestion: try (0, 30, 5)
  • Create an iteration for each range, printing the value of each using the same for statement described in the previous tests.
    • NOTE: Be sure to use 4 spaces to indent the print line after the for statement.

Estimate

  • Small

Tests

  • From within your IDE

    • Right click on your newly created module SimpleSequences.py and click your IDE's run icon
    • You should see output similar to that discussed in class
  • From the command line

    • Open a terminal and cd to your IPP_HOME path
    • Start your virtual environment (if not already running)
    • Be sure your PYTHONPATH is set correctly
    • Run the module
      • python ./labmodule02/SimpleSequences.py
    • You should see output similar to that discussed in class

Sample output (yours may differ slightly)

T
h
o
r
 
O
d
i
n
s
o
n

Item: notebook
Item: pen
Item: power supply
Item: hoodie

Item: notebook
Item: pen
Item: power supply
Item: hoodie
Tuples are immutable! Can't append.
Item: notebook
Item: pen
Item: power supply

Simple range val: 0
Simple range val: 1
Simple range val: 2
Simple range val: 3
Simple range val: 4
Simple range val: 5
Simple range val: 6
Simple range val: 7
Simple range val: 8
Simple range val: 9

Bounded range val: 1
Bounded range val: 2
Bounded range val: 3
Bounded range val: 4
Bounded range val: 5
Bounded range val: 6
Bounded range val: 7
Bounded range val: 8
Bounded range val: 9
Bounded range val: 10

Stepwise range val: 0
Stepwise range val: 5
Stepwise range val: 10
Stepwise range val: 15
Stepwise range val: 20
Stepwise range val: 25

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Lab Module 02 - Syntax

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions