Uploaded by Edward Macgill

Python String & List Methods Practice Sheet

advertisement
CS 111X Lab 9 Practice Sheet Key
The purpose of this material is to assist you in understanding the concepts taught each week.
While not graded work, it is highly suggested you review this material to understand the
intricacies of Python which may be tested on future quizzes or exams.
String Methods
1. Some problems may result in an error, in which case you should write ‘E’ in the
answer boxes.
Problem
s = ‘pizza party’
print(s[s.find(‘ p‘) +2])
s = ‘hello world’
print(s.find(‘bob’))
s = ‘hello world’
print(s.count(‘l’))
s = ‘hello world’
print(s.count(‘H’))
s = ‘hello world’
s.upper()
print(s)
s = ‘Hello World’
print(s.lower())
s = 'pizza_pie_pizza'
print(s.replace('p', 'pizza'))
print(s.find('pizza'))
s = 'pizza_pie_pizza'
print(s.replace('q','pizza'))
s = ‘ hello world ‘
s = s.strip()
print(s)
s = 'Piza'
s = 'Rome'.join(s)
print(s)
What is printed?
s = 'Piza'
s = s.join(s)
print(s)
s = 'hello all of you'
l = s.split()
print(s)
print(l)
s = 'hiyo all of you'
l = s.split('o')
print(l)
List Methods and Aliasing
1. Some problems may result in an error, in which case you should write ‘E’ in the answer
boxes.
2.
Problem
a = ['Spike', 'Twilight', 'Pinky']
b=a
a.append('Shutter')
print(b)
a = ['Spike', 'Twilight', 'Pinky']
b = a[:]
a.append('Shutter')
print(b[-1])
a = ['Spike', 'Twilight', 'Pinky']
b = a.copy()
a.append('Shutter')
print(b[-1] == a[-1])
a = ['Spike', 'Twilight', 'Pinky']
b = a.copy()
a.insert(3, 'Shutter', )
b.append('Shutter')
print(b[-1] == a[-1])
a = ['Spike', 'Twilight', 'Pinky']
What is printed?
a.insert(1, 'Shutter', )
print(a)
def summer(list1):
total = 0
for item in list1:
total += item
list1.append(total)
list1.reverse()
a = [40,22,39]
summer(a)
print(a)
l1 = [1,2,3]
l1.append([5,6,7])
print(l1)
print(len(l1))
l1 = [1,2,3]
l1.extend([5,6,7])
print(l1)
print(len(l1))
l1 = [1,2,3]
l1.extend(4)
print(l1)
print(len(l1))
l1 = [1,2,3]
l1.extend([3,2,1])
l1.remove(4)
print(l1)
l1 = [1,2,3]
l1.extend([3,2,1])
if 4 in l1:
l1.remove(4)
print(l1)
l1 = [1,2,3]
l1.extend([3,2,1])
if 3 in l1:
l1.remove(3)
print(l1)
l1 = [1,2,3]
l1.extend([3,2,1])
while 3 in l1:
l1.remove(3)
print(l1)
l1 = [1,2,3]
l1.extend([3,2,1])
value = l1.pop(2)
print(value)
value2 = l1.pop()
print(value2)
print(l1)
l1 = [0,2,3,4,1]
while 1 in l1:
del l1[1]
print(l1)
l1 = [0,2,3,4,1]
del l1[:-2]
print(l1)
l1 = [0,2,1,2,0]
l1.index(1)
print(l1)
l1 = [0,2,1,2,0]
print(l1.index(2))
l1 = [0,2,1,2,0]
print(l1.index(3))
l1 = [0,2,1,2,0]
print(l1.count(2))
l1 = [0,2,1,2,0]
l1.sort(reverse=True)
print(l1[-1])
l1 = [0,2,1,2,0]
l1.sort()
print(l1[2])
l1 = [‘hi’,’a’, ‘bye’]
l1.sort()
print(l1[2])
l1 = ['hi','a', 10, 'bye']
l1.sort()
print(l1)
a=4
l1 = [1, 2, 3]
l2 = ['a', 'b', 'c', a]
l3 = [l1, l2]
print(l3)
a=4
l1 = [1, 2, 3]
l2 = ['a', 'b', 'c', a]
l3 = [l1, l2]
print(len(l3[1]))
a=4
l1 = [1, 2, 3]
l2 = ['a', 'b', 'c', a]
l3 = [l1, l2]
l3.append(a)
print(len(l3))
a=4
l1 = [1, 2, 3]
l2 = ['a', 'b', 'c', a]
l3 = [l1, l2]
l3[0].append(a)
print(l3[1][1])
print(l1)
print(l2)
Fill in the Blank
The following code attempts to import the module named math and use the sqrt() function. The
function takes in a number as a parameter and returns the square root of the number. Below
import the module and use the function to find the square root of a number given by the user
using the input function (input could be a decimal number).
_________ # import the appropriate module
num = _________ (input("give number")) # take user input for numerical value
print(_________ ) # use the math modules sqrt() function
A research organization has collected data on peoples favorite primary colors (yellow, red,
blue), but the data is messy. Currently, the data is stored in a list you will need to clean the data
as defined in the comments below so statistical analysis can be run.
colors = [' yEllow ', '
Green', 'blue ', 'yellow', 'blue ', ' RED', 'red', 'blue']
for i in range(len(colors)):
colors[i] = colors[i]_________ # remove the white space from each string
_________ # convert each string to lowercase
colors_________ # remove the non primary color from the list with the remove function
print(colors) # should print ['yellow', 'blue', 'yellow', 'blue', 'red', 'red', 'blue']
Download