Professional Documents
Culture Documents
I
TUTORIAL 5
LISTS
print(numbers)
print(mixedlist)
newlist = [ numbers, vocabulary ]
print(newlist)
List length -> len()
print([0] * 4)
print([1, 2, ["hello", "goodbye"]] * 2)
list-6-1: What is printed by the following
statements?
alist = [1, 3, 5]
blist = [2, 4, 6]
print(alist + blist)
(A) 6
(B) [1, 2, 3, 4, 5, 6]
(C) [1, 3, 5, 2, 4, 6]
(D) [3, 7, 11]
list-6-2: What is printed by the following
statements?
alist = [1, 3, 5]
print(alist * 3)
(A) 9
(B) [1, 1, 1, 3, 3, 3, 5, 5, 5]
(C) [1, 3, 5, 1, 3, 5, 1, 3, 5]
(D) [3, 9, 15]
List Slices
fruit[0] = "pear"
fruit[-1] = "orange"
print(fruit)
alist = ['a', 'b', 'c', 'd', 'e', 'f']
alist[1:3] = ['x', 'y']
print(alist)
List Deletion – del
Objects and references
Aliasing
alist = [4, 2, 8, 6, 5]
blist = alist
blist[3] = 999
print(alist)
(A) [4, 2, 8, 6, 5]
(B) [4, 2, 8, 999, 5]
Cloning
list-13-1: What is printed by the following
statements?
alist = [4, 2, 8, 6, 5]
blist = alist * 2
blist[3] = 999
print(alist)
alist = [4, 2, 8, 6, 5]
alist = alist + 999
print(alist)
(A) [4, 2, 8, 6, 5, 999]
(B) Error, you cannot concatenate a list with an integer.
FOR loop
alist = [4, 2, 8, 6, 5]
blist = [ ]
for item in alist:
blist.append(item+5)
print(blist)
(A) [4, 2, 8, 6, 5]
(B) [4, 2, 8, 6, 5, 5]
(C) [9, 7, 13, 11, 10]
(D) Error, you cannot concatenate inside an append.
Lists as parameters
Pure functions
Assume you already have a function is_prime(x)
that can test if x is prime. Now, write a function to
return a list of all prime numbers less than n:
def primes_upto(n):
""" Return a list of all prime numbers less than n. """
result = []
for i in range(2, n):
if is_prime(i):
result.append(i)
return result
List comprehension
list-22-1: What is printed by the following statements?
alist = [4,2,8,6,5]
blist = [num*2 for num in alist if num%2==1]
print(blist)
(A) [4,2,8,6,5]
(B) [8,4,16,12,10]
(C) 10
(D) [10]
Nested lists
A tuple, like a list, is a sequence of items of any type. Unlike lists, however,
tuples are immutable. Syntactically, a tuple is a comma-separated
sequence of values. Although it is not necessary, it is conventional to
enclose tuples in parentheses:
myList.append("apple") #a
myList.insert(3, "cat") #b
myList.insert(0, 99) #c
print(myList.index("hello")) # d
print(myList.count(76)) #e
myList.remove(76) #f
myList.pop(myList.index(True)) # g
print (myList)
Create a list containing 100 random integers between 0 and 1000 (use iteration,
append, and the random module). Write a function called average that will take the list
as a parameter and return the average.
Write a Python function that will take a the list of 100 random integers between 0 and 1000
and return the maximum value. (Note: there is a builtin function named max but pretend
you cannot use it.)
Write a function sum_of_squares(xs) that computes the
sum of the squares of the numbers in the list xs. For
example, sum_of_squares([2, 3, 4]) should return 4+9+16
which is 29:
Write a function to count how many
odd numbers are there in a list
Sum up all the even numbers in a list.
Sum up all the negative numbers in a
list.
Count how many words in a list have
length 5.
Sum all the elements in a list up to but not
including the first even number.