Changes

Jump to: navigation, search

OPS435 Python Lab 3

1,803 bytes removed, 11:03, 29 January 2018
PART 2 - Manipulating Items in Lists
:'''Perform the Following Steps:'''
:#Start the ipython3 shell:<source>ipython3</source>:#Let's perform a simple change to a list element. Issue Try the following in the ipython shellcode:<source lang="python">
courses = [ 'uli101', 'ops235', 'ops335', 'ops435', 'ops535', 'ops635' ]
print(courses[0])
courses[0] = 'eac150'
print(courses[0])print(courses)</source>It might be useful to change a list element-by-element, but there are other more efficient methods of changing a list (for example: using functions). You will now use the '''dir()''' and '''help()''' functions to see what functions and attributes are available for manipulating lists. The '''help()''' function will also give us tips on how to use those functions. For example, you can use issue help(list-name) in order to see what functions are available to use for that list specific list.<br><br>:#Issue the following:<source lang="python">dir(courses)help(courses)</source>Below are some examples of using built-in functions to '''manipulate''' lists. Take your time to see how each function can be a useful tool for making changes to existing lists.<br><br>:#Issue the following:<source lang="python">help(courses.append)
courses.append('ops235') # Add a new item to the end of the list
print(courses)
help(courses.insert)
courses.insert(0, 'hwd101') # Add a new item to the specified index location
print(courses)
help(courses.remove)
courses.remove('ops335') # Remove first occurrence of value
print(courses)
help(courses.sort)
sorted_courses = courses.copy() # Create a copy of the courses list
sorted_courses.sort() # Sort the new list
print(courses)print(sorted_courses)
</source>:#In addition to using functions to manipulate lists, there are functions that are useful to provide '''information''' regarding the list such as number of elements in a list, the smallest value and largest value in a list.<br><br> :#Issue the following:<source lang="python">
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
lengthOfList = len(list_of_numbers) # Returns the length of the listsmallestInList = min(list_of_numbers) # Returns the smallest value in the listlargestInList = max(list_of_numbers) # Returns the largest value in the list</source>In addition to manipulating and obtaining characteristics of a list, it # Notice how the long line below is also useful wrapped to be able to '''perform searches''' for values within lists and obtain the location of values for elements contained within a list. The '''indexfit on one screen:print()''' function allows searching inside a list for a value, it will return the index number of the first occurence. <source lang="pythonList length is ">+ lengthOfList + number = 10help(list_of_numbers.index)list_of_numbers.index(number) # Return index of the number searched for</source>One common annoyance that can occur when performing searches are '''error messages''' when performing a search for an ", smallest element that happens NOT to exist in the list. A good way to prevent those type of errors is to use an '''if''' statement to check to see if the value for an element is in a list, then the appropriate search can be performed for that existing element value.<br><br>" + smallestInList +:#Issue the following:<source lang="python ">list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]number = 7if number in list_of_numbers: # Returns True if value in list, returns False if item not largest element in the list number_index = list_of_numbers.index(number) print('index is: ' " + str(number_index))else: # If the statement is False, the else will run print(str(number) + ' is not in list_of_numbers'largestInList)
</source>

Navigation menu