chapter 7 programming
how do you find the lowest and highest values in a list?
you can use the build-in max and min functions
what will the following code display? numbers = list(range(1, 10, 2)) for n in numbers, print(n)
1 3 5 7 9
what will the following code display? numbers = [1, 2, 3, 4, 5], print(numbers([-2]))
4
assume the following statement appears in a program: names = [ ] which of the following statements would you use to add the string 'wendy' to the list at index 0? why would you select this statement instead of others? a. names [0]='wendy' b. names.append('wendy')
B. bc the element 0 doesn't exist so an error will occur
what will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[ :1] print(my_list)
[ 1 ]
what will the following code display? numbers = list(range(3)) print (numbers)
[0, 1, 2]
what will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[ : ] print(my_list)
[1, 2, 3, 4, 5]
what will the following code display? numbers1=[1, 2, 3] numbers2= [10, 20, 30] numbers2 += numbers1 print(numbers1) print(numbers2)
[1, 2, 3] [10, 20, 30, 1, 2, 3]
what will the following code display? numbers1=[1, 2, 3] numbers2= [10, 20, 30] numbers3 = numbers1 + numbers2 print(numbers1) print(numbers2), print(numbers3)
[1, 2, 3] [10, 20, 30] [1, 2, 3, 10, 20, 30]
what will the following code display? numbers = [ 1, 2, 3, 4, 5] numbers[2]=99 print(numbers)
[1, 2, 99, 4, 5]
what will the following code display? numbers = [10]*5 print(numbers)
[10, 10, 10, 10, 10,]
what will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[1: ] print(my_list)
[2, 3]
what will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[1:3] print(my_list)
[2, 3]
what will the following code display? numbers = [1, 2, 3, 4, 5] my_list = numbers[ -3 : ] print(my_list)
[3, 4, 5]
describe the insert method
inserts ab item into a list at a specific index
what will the following code display? names = [ 'Jim', 'Jill', 'John', 'Jasmine'] if 'jasmine' not in names print ('Cant find jasmine') else: print ('Jasmines family') print(names)
jasmines family: [ 'Jim', 'Jill', 'John', 'Jasmine']
describe the reverse method
reverses the order of the items in the list
describe the index method
searches for an item in the list and returns the index of the frist element containing that item
describe the sort method
sorts the items in the list to appear in ascending order
what is the difference between calling a lists remove method and using the del statement to remove an element?
the remove function searches for and removes an element containing a specific value. The del statement removes an element at a specific index
how do you find the number of elements in a list?
use the build-in len function