Changes

Jump to: navigation, search

OPS435 Python Lab 3

230 bytes added, 11:15, 3 June 2017
PART 3 - Iterating Over Lists
square(value)
</source>:#But this You should see the effectiveness of the previous demonstration. Although useful, the demonstration above only prints out each new valuethe squares as opposed to saving them for future use. Lets try making The next demonstration uses a new function that loops through lists, squares the values, and returns but saves the squares in a new list.<br><br>:#Issue the following:<source>
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
new_list_of_numbers = square_list(list_of_numbers)
new_list_of_numbers
</source>:#The above is just one example of quick, powerful, for loops mixed with lists. But be careful when passing lists into functions. When you give a function a listas an argument, it is the actual list reference and NOT a copy. This means a function can completely change the list without making a new list. While you do have to be careful this is also useful, a function can modify any given list, without have to return or store it.:#To demonstrate, issue the following:<source>
list_of_numbers = [ 1, 5, 2, 6, 8, 5, 10, 2 ]
def delete_numbers(numbers):
13,420
edits

Navigation menu