Python
2021/3/25 7.14 Programming Lab 4
https://ccsf.instructure.com/courses/41466/assignments/740094?module_item_id=2101965 1/4
7.14 Programming Lab 4
Due Thursday by 11:59pm Points 60 Submitting a file upload File Types py Available after Mar 17 at 10am
Submit Assignment
Optional Reading
Here are some external website pages that review different examples of using slicing. These pages are optional reading and give similar examples of slicing to those presented in prior module reading.
Python string slicing (Link to an external site.) (https://www.pythoncentral.io/how-to-get-a-substring- from-a-string-in-python-slicing-strings/)
Python list and tuple slicing (Link to an external site.) (https://www.pythoncentral.io/how-to-slice- listsarrays-and-tuples-in-python/)
Exercise
Please read and perform the programming exercise given below. You are to write two Python programs for this exercise.
Make sure your Python programming code blocks and algorithm(s) have comment lines where you write comments that explain how your program logic is working. You must write enough comments to fully explain your code blocks and algorithm(s) which your programming logic is performing.
Also make sure when you submit your programs for this exercise that you write your programs as individual Python .py files for each program, and provide these files as your submission. (The Canvas Student Guide provides instructions for submitting an online Assignment (https://guides.instructure.com/m/4212/l/41972-how-do-i-submit-an-online-assignment) Please examine these instructions in case you wish to see how to upload many files in your submission. Most of what you need to know is mentioned under the section labeled "Submit A File Upload").
Programming Exercise 4: Using a Python Slicing And Python Functions Program 1 Purpose. The purpose of this program is for you to practice using slicing on a Python string, a Python list, and a Python tuple. Requirements. Write a program named slicingStringListAndTuple.py that contains programming logic to perform the following 6 tasks:
2021/3/25 7.14 Programming Lab 4
https://ccsf.instructure.com/courses/41466/assignments/740094?module_item_id=2101965 2/4
1. Place the following string, list, and tuple:
str = ‘Python Slicing’ l = [‘P’, ‘y’, ’t’, ‘h’, ‘o’, ’n’, ’S’, ‘l’, ‘i’, ‘c’, ‘i’, ’n’, ‘g’] t = (‘P’, ‘y’, ’t’, ‘h’, ‘o’, ’n’, ’S’, ‘l’, ‘i’, ‘c’, ‘i’, ’n’, ‘g’)
into your program.
2. Print the string ‘Slice’ by taking the str string, extracting ’Slic’ from str using slicing, and printing the ‘Slic’ extracted, and the character ‘e’ after it.
3. Use slicing to get the list [’S’,"i",'n','g’] from the list l and store the list [’S’,"i",'n','g’] in a variable named song. Use slicing to get the tuple (‘o’,’n’) from the tuple t and store the tuple (‘o’,’n’) in a variable named notOff.
4. Using the input() function, have a user input how many characters they would like to extract. The input the user gives should be an integer, and should be converted using the int() function. After the input has been converted using int(), use the integer to print the following slices: a) print a slice starting at the beginning of the str string that has a length equal to the integer from the input b) print a slice starting at the beginning of the l list that has a length equal to the integer from the input c) print a slice starting at the beginning of the t tuple that has a length equal to the integer from the input
5. Use negative numbers in slicing to print out the following: a) the last 3 characters of the string str b) the last 3 characters of the list l c) the last 3 characters of the tuple t
6. Use a single, negative number step value and performing slicing to print the string str, the list l, and the tuple t backwards.
Program I/O. Input: one number from the console keyboard. Output: Echo the input value and print the result of the calculation to the console screen.
Example. For example, with user input in bold:
Slice How many characters would you like to extract: 3 Pyt ['P', 'y', 't'] ('P', 'y', 't') ing ['i', 'n', 'g'] ('i', 'n', 'g') gnicilS nohtyP ['g', 'n', 'i', 'c', 'i', 'l', 'S', 'n', 'o', 'h', 't', 'y', 'P'] ('g', 'n', 'i', 'c', 'i', 'l', 'S', 'n', 'o', 'h', 't', 'y', 'P')
2021/3/25 7.14 Programming Lab 4
https://ccsf.instructure.com/courses/41466/assignments/740094?module_item_id=2101965 3/4
Program 2 Purpose. The purpose of this program is for you to practice writing and using a Python function. Requirements. Write a program named myThreeFunctions.py that contains three functions. The functions are to be named myAvgfun, slicingStringListAndTuple and myOwnFunction. These three functions will be called in the main program part of the programming logic. The three functions should do the following: 1. myAvgfun calculates the average value of 5 parameters given to it. The function should take 5 parameters (assume that the parameters will always be numbers), add all of the values of the parameters together, and then divide the addition by 5.
Make sure the myAvgfun function is a value returning function that uses the return command to return the average as the value for the function.
2. slicingStringListAndTuple uses the programming logic that was written in program slicingStringListAndTuple.py for the Program 1 part of this programming lab. The slicingStringListAndTuple function should take 3 parameters for the values that were sliced in slicingStringListAndTuple.py. Those 3 parameters should be used for the string, the list and the tuple given in the Program 1 description. Those 3 parameters can have any names you choose to provide, but they must be able to take a string, a list, and a tuple as values passed in by a main program.
3. write myOwnFunction to take as many parameters as you like (even 0 parameters is appropriate) and either performs a tasks that you choose or returns a value that you choose
4. write a main program that:
• calls the myAvgfun with the values 1, 2, 3, 4, and 5 and prints the value returned by myAvgfun
• calls the slicingStringListAndTuple with the string, the list and the tuple given in the Program 1 description, and prints & takes the input similarly to the slicingStringListAndTuple.py program
• calls your myOwnFunction and performs the programming logic you placed inside it
Program I/O. Input: one number from the console keyboard. Output: Echo the input value and print the result of the calculation to the console screen.
Example. For example, with user input in bold:
Average: 3
Slice
How many characters would you like to extract: 2 Py
2021/3/25 7.14 Programming Lab 4
https://ccsf.instructure.com/courses/41466/assignments/740094?module_item_id=2101965 4/4
['P', 'y'] ('P', 'y') ng ['n', 'g'] ('n', 'g') gnicilS nohtyP ['g', 'n', 'i', 'c', 'i', 'l', 'S', 'n', 'o', 'h', 't', 'y', 'P'] ('g', 'n', 'i', 'c', 'i', 'l', 'S', 'n', 'o', 'h', 't', 'y', 'P')
My function just prints this sentence.
Examples Here are some sample programs you can use to see how functions are defined and are called in a main program: paramList.py (https://ccsf.instructure.com/courses/41466/files/5218748/download?download_frd=1) passingGradeFun.py (https://ccsf.instructure.com/courses/41466/files/5218749/download? download_frd=1) name.py (https://ccsf.instructure.com/courses/41466/files/5218750/download? download_frd=1) yesNo.py (https://ccsf.instructure.com/courses/41466/files/5218751/download? download_frd=1)