Experiment 2:
Strings, Lists, Tuples, and Dictionaries
CPE106L (Software Design Laboratory)
Group No.:
Section:
PreLab
Readings, Insights, and Reflection
METIS book: Chapter 4 and Chapter 5
Fundamentals of Python: First Programs
Kenneth A. Lambert
Edition 2
ISBN: 9781337671019
Cengage Learning US
2019
Pg. 102-166
Websites: https://docs.python.org/3/contents.html
Answers to Questions
1. B. 20
2. B. [20,30]
3. A. 1
4. B. [10,20,30,40,50]
5. B. [10,5,30]
6. C. [10,15,20,30]
7. B. ["name", "age"]
8. B. None
9. B. pop
10. B. strings and tuples
11.
InLab
• Objectives
Create Create a program for the given problems
Debug the programs using VS code
Discuss the structure of the program
• Tools Used
Anaconda Environment
Spyder
• Procedure.
• median.py
Figure 1.1 Source code of median.py
In figure 1.1, it displays the source code which is necessary for the user to input the file name, as
well as the code that will compute the median of the given text file.
Figure 1.2 Code for computation
Figure 1.2 displays the necessary code to compute the median of the numbers given in the text
file.
Figure 1.3 Median output of txt file
Figure 1.3 shows the output median of the sample text file used which yielded 31.
• mode.py
Figure 2.1 Source code of mode.py
Figure 2.1 shows the source code for computing the mode and letting the user input the file
name and obtaining the files frequency and saving it.
Figure 2.2 Code for the computation of mode
Figure 2.2 displays the computation required in acquiring the mode of the samples provided.
Figure 2.3 Mode output
• computersquare.py
Figure 3.1 Source code for computesquare.py
Figure 3.1 illustrates the source code for the problem.
Figure 3.2 Where the code is executed
Figure 3. shows the entry point for program execution.
Figure 3.3 Output of the code
Figure 3.3 displays the output of the code using the sample number that as entered.
PostLab
Programming Problems
1. Filename: stats.py
A group of statisticians at a local college has asked you to create a set of functions that
compute the median and mode of a set of numbers, as defined in the below sample
programs:
• mode.py
• median.py
Define these functions in a module named stats.py. Also include a function named mean,
which computes the average of a set of numbers. Each function should expect a list of
numbers as an argument and return a single number. Each function should return 0 if
the list is empty. Include a main function that tests the three statistical functions with a
given list.
Figure 1. Median Function
Figure 2. Mode function
Figure 3. Mean function
Figure 4. Sample values
Figure 5. Output of the code using the sample values
2. Filename: LR2_2.py
Write a program that allows the user to navigate the lines of text in a file. The program should
prompt the user for a filename and input the lines of text into a list. The program then enters a
loop in which it prints the number of lines in the file and prompts the user for a line number.
Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program
quits. Otherwise, the program prints the line associated with that number.
Figure 6. Sample text values
Figure 7. Source code of the program
Figure 8. Output of code using the sample text file
3. Filename: generator_modified.py
Modify the sentence-generator program of Case Study 5.3:
• METIS book: 9781337671019, page 150.
• Python source code: generator.py
so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt,
verbs.txt, articles.txt, and prepositions.txt. (Hint: Define a single new function, getWords. This
function should expect a filename as an argument. The function should open an input file with
this name, define a temporary list, read words from the file, and add them to the list. The
function should then convert the list to a tuple and return this tuple. Call the function with an
actual filename to initialize each of the four variables for the vocabulary.)
Figure 9. Source code of generator_modified.py
Figure 10. Sample text files
Figure 11. Output of the program