CS 115 Lab 8: Setup and Practice

[Back to lab instructions]

Refer to the Week 9 reading or other resources as necessary to complete this assignment.


Setup

  1. If the machine was on when you started using it, reboot it to MacOS.
  2. Follow these procedures to mount your blue home-directory on the local machine and create a PyCharm project (call it Lab08). You will use PyCharm in the next part, but, it is important that you set up your project now so that if you run into any issue, we can resolve them quickly.

Practice Part 1: String Methods

In this part, you'll use the the online Python 3 tutor to practice splitting strings into lists.

  1. For the following program...
    x = 'I am a string'
    z = x.split()
    print(z[0])
    print(x)
        
    Predict what this code will do and answer Questions 1 and 2 in your writeup.
  2. For the following program...
    x = 'I am a string'
    for w in x.split():
        print(w, end="")
    print()
        
    Predict what this code will do and answer Question 3 in your writeup.
  3. For the following program...
    x = 'I am a string'
    y = x.split()
    print(len(y))
        
    Predict what this code will do and answer Question 4 in your writeup.

Practice Part 2: Populating Lists

For this part of the lab, you'll use the online Python 3 tutor to build lists using append.

  1. Enter the following code into the tutor:
    L = []
    for i in range(3):
       L.append(i)
    
    Answer Question 5 in your Moodle writeup.
  2. Enter the following code into the tutor:
    L = []
    for j in range(5):
       L.append(j ** 2)
    
    Answer Question 6 in your Moodle writeup.
  3. Enter the following code into the tutor:
    A = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec']
    B = []
    for month in A:
        B.append(month)
    
    Answer Question 7 in your Moodle writeup. Note that there are more efficient ways to do what this code does -- this is just an example that uses append.

Practice Part 3: Functions

  1. Enter the following code into the tutor:
    import math
    
    
    def PrintHello():
        print("Hello!")  # A
    
    
    def PrintGoodbye():
        print("Goodbye!")  # B
    
    
    def HiFive():
        print("HiFive!")  # C
        return 5
    
    
    def main():
        print("Calling PrintHello")  # D
        PrintHello()
        print("Calling HiFive")  # E
        value1 = HiFive()
        print("Result of HiFive:", value1)  # F
    
    
    main()
  2. Answer Question 8 in your Moodle writeup.