2nd Semester Intro to Computer Science

What you will learn

Reading

Do Now

1. In your Console

Type the following code

for i in range(0, 10):
    print(i)

In your Notebook

Respond to the following

Write down what the range function does.

2. In your Console

Type the following

Use the range and len functions to make a for loop that loops through a.

a = ['apples', 'oranges', 'pears', 'grapes']

len_a = len(a)
range(0, len(a))

Continue In your Notebook

  1. Write down what range(0, len(a)) does.
  2. What is the return value of the range function?

3. In your Console

Use the range and len functions to make a for loop. (Remember that for loops iterate over lists)

Lab

Part 1

Write a function pluralize_words that takes in a list of words and updates the values of the list to make each one plural. It returns nothing. Making plurals in english has a number of special cases, but for this lab we’ll use a simple rule: if the word ends in a ‘y’ remove the ‘y’ and add ‘ies’; otherwise add an ‘s’.

We’ll exercise the function on a list of words.

  1. Create the function contract for pluralize_words.
  2. Provide a few examples that confirm pluralize_words works as expected:

    • Include examples with ‘berry’
    • What if the list is empty?
    • What happens if a word ends in ‘s’?

Example 1

# contract goes here
def pluralize_words(word_list):
    # your code goes here

word_list = ['apple', 'berry', 'melon']
print("Singular words: " + str(word_list))
pluralize_words(word_list)
print("No longer singular words: " + str(word_list))
    # more examples go here
Here is what it should look like when you run your code
Singular words: ['apple', 'berry', 'melon']
No longer singular words: ['apples', 'berries', 'melons']

Hint

Remember that you can index into the string and get the length of a string. Use that to get the last letter of each word.

Part 2

Create a function my_reverse, which will return a reversed string.

  1. Create the function contract for my_reverse.
  2. Provide a few examples to confirm that my_reverse works:

    • An empty string
    • A string of even length
    • A string of odd length greater than 1
    • A string of length 1

Example 2

# contract goes here
def my_reverse(string_to_reverse):
    # your code goes here
reversed = my_reverse("apples")
print(reversed)
# examples go here

Here is what example 2 should look like when you run your code

>>> python3 my_reverse_lab.py
selppa

Hint 2

To get the last element:(len(my_list) -1) - 0 To get the second to last element: (len(my_list)-1 ) - 1 To get the third to last element: (len(my_list)-1) - 2

Bonus

Create a function reverse_strings_in_list. This function will input a list of strings you want to reverse. The function will reverse the strings in the list by calling the my_reverse function in a loop.

Check your understanding

Pick an answer for each question. You can try again until you get it right.

1. What does range(0, 4) produce?

  • 1, 2, 3, 4
  • 0, 1, 2, 3, 4
  • 0, 1, 2, 3
  • 4

range starts at the first number and stops before the second.

2. A list a has 5 items. What values does i take in for i in range(0, len(a)):?

  • 0, 1, 2, 3, 4
  • 1, 2, 3, 4, 5
  • 0, 1, 2, 3, 4, 5
  • Only 5

len(a) is 5, so i runs from 0 up to 4, the valid indexes.

3. Why loop over indexes with range and len when you want to update the items in a list?

  • Loops over items are not allowed
  • It is the only way to print a list
  • Index loops run backwards
  • You can assign to a[i] to change the list itself, not just a copy of an item

Assigning to the loop variable of an item loop does not change the list, but assigning to an index does.

Downloads