This week is all about lists, in particular Python's built-in functions and methods. Of course, we also get more practice with loops and choices.
Create a directory on your USB device for this lab, say, lab11, and launch IDLE.
This week, you will not submit your shell window at the end of the session. Do not panic if you close it accidentally along the way.
You will submit a responses.txt file this week. Download this template file and use it to record any answers or predictions asked for in the exercises.
Study this code:
A good way to learn a language and how it works is to implement some of its built-in functionality.
Create a new Python file named task02.py.
>>> length( ['alice', 20, True] ) 3
>>> total( [1, 100, 3, 4, 5, 6] ) 119
>>> contains( ['alice', 20, True], 20 ) True >>> contains( ['alice', 20, True], 'eugene' ) False
>>> find( ['alice', 20, True], True ) 2 >>> find( ['alice', 20, True], 42 ) -1
Python has lots of cool built-in functions, but people who program with lists often need more than the built-in functionality.
Create a new Python file named list_utils.py.
>>> all_to_int( '1,2,3,4,5'.split(',') ) [1, 2, 3, 4, 5]
>>> find_bigger( [12, 45, 36, 4, 106, 43, 45, 7, 9, 15], 12 ) [45, 36, 106, 43, 45, 15] >>> find_bigger( [12, 45, 36, 4, 106, 43, 45, 7, 9, 15], 102 ) [106]
>>> add( [1,2,3], [4,5,6] ) [5, 7, 9]Bonus. If the lists are of different lengths, only process to the end of the shorter list:
>>> add( [1,2,3], [4,5,6,7,8] ) [5, 7, 9]
With list processing, we can let our user give us any number of inputs on the same line!
>>> Enter as many numbers as you like, separated by spaces. Your numbers: 42 34 56 1 3 ... add up to 136 ... multiply to 239904 ... contain 0 zeros >>> Enter as many numbers as you like, separated by spaces. Your numbers: 0 12 0 34 56 1 3 0 0 42 15 0 ... add up to 163 ... multiply to 0 ... contain 5 zeros
You may use Python's built-in functions and methods for lists.
Make sure that your program files are complete and saved. Save your responses.txt file.
Submit your files for grading on the electronic submission system, at lab11 -- Lists and Methods and Loops.
As always, make sure you see the verification screen that says The files listed above were uploaded.
If you need any help, let me know.