CMPS-224 Lab 4 Sets in Python

Eddie Rangel
Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

Introduction

Goals: Understand how sets work in Python 3.

Resources: Python 3 Documentation Documentation Python Lab 3 Materials

Files Needed:

You use the open function in Python to open a file. The open function creates a file object and associates it with a file on the disk. Here is the general format of how the open function is used:

    
        file_variable = open(filename, mode)
    
    

As an example, suppose the file customers.txt contains customer data, and we want to open it for reading. Here is an example of how we would call the open function:

    
        customer_file = open('customers.txt', 'r')
    
    

Suppose we want to create a file named sales.txt and write data to it. Here is an example of how we would call the open function:

    
        sales_file = open('sales.txt', 'w')
    
    

Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.

    
    input_file = open(input_name, 'r')
    text = input_file.read()
    words = text.split()
    
    

What to turn in

Show the programs running to me or our TA.