Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

141 bytes added, 09:46, 29 January 2018
PART 1 - Navigating Items in Lists
#!/usr/bin/env python3
# Create the list called "my_list" below this command (here, not within any function defined below).# That makes it a global variable. We'll talk about that in another lab.
def give_list():
# Does not accept any arguments
# Returns all of the entire list global variable my_list unchanged
def give_first_item():
# Does not accept any arguments
# Returns a single string that is the first item in the listglobal my_list
def give_first_and_last_item():
# Does not accept any arguments
# Returns a list that includes the first and last items in the listglobal my_list
def give_second_and_third_item():
# Does not accept any arguments
# Returns a list that includes the second and third items in the listglobal my_list
if __name__ == '__main__': # This section also referred to as a "boiler platemain code"
print(give_list())
print(give_first_item())
:::'''Sample Run 1:'''<source>
run ./ lab3e.py
[100, 200, 300, 'six hundred']
100
[200, 300]
</source>
:::'''Sample Run 2 (with importfrom another script):'''<source>
import lab3e
lab3e.give_list()
# Will print [100, 200, 300, 'six hundred']
lab3e.give_first_item()
# Will print 100
lab3e.give_first_and_last_item()
# Will print [100, 'six hundred']
lab3e.give_second_and_third_item()
# Will print [200, 300]
</source>
:::3. Exit the ipython3 shell, download Download the checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab3/
pwd #confirm that you are in the right directory
python3 ./CheckLab3.py -f -v lab3e
</source>
:::4. Before proceeding, make certain that you identify any and all errors in lab3e.py. When the checking script tells you everything is OK before proceeding - proceed to the next step.
== PART 2 - Manipulating Items in Lists ==