CNIT 155 FINAL (PURDUE) QUESTIONS & ANSWERS
Two Ways to Create an Empty List: - Answers -list1 = [ ]
list1 = list ()
How to Create a List? - Answers -Put a sequence of items in [ ]
Ex. colors = ["red", "yellow", "blue"]
Reading 10 Values from a User then Finding the Minimum - Answers -values = [ ]
for i in range (0, 10):
v1 = int(input("Enter a value"));
values.append(v1);
print(values)
print(min(values))
Three Methods of Adding Elements to a List - Answers -list1.append(elem)
(adds a single element to the end of the list)
list1.insert(index,elem)
(inserts the elem at the given index, shifting elements to the right)
list1.extend(list2)
(adds the elements in list2 to the end of the list)
Access an Individual Element in a List Using an Index - Answers -list1[index]
Positive Indexes - Answers -- First element in the list has index 0
- Second element has index 1
-n'th element is n-1
Negative Indexes - Answers -- Index -1 identifies the last element
- -2 identifies the next to last element, etc.
Built-In List Functions in Python - Answers -function - description
len - returns the number of items in the list
min - returns the min among the elements in the list
max - returns the max among the elements in the list
sum - returns the sum of all elements in the list
Shuffle Function - Answers -from random import shuffle
shuffle(list1)
, How to Call Methods Provided by a List - Answers -list.methodName(arguments)
Traversing a List - Answers -for index in range (len(scores)):
print (index, "\t" , grade[i])
for index, grade in enumerate(scores):
print (index, "\t" ,grade[index])
To add a whole list to the end, use the extend method: - Answers -scores.extend([55,
88, 79])
# gives [85,72,56,98,84,72,55,88,79]
Inserting an element at a given location: - Answers -Names.insert (2, "Hanah")
Inserting a new element at position 2 in the list
Deleting an from a list by index - Answers -del list[index]
Deleting a list range by using a slice - Answers -del list[2:5]
Deleting a specific value from a list - Answers -colors.remove("blue")
or
if "blue" in colors:
pos = colors.index("blue")
del colors[pos]
Reversing a list - Answers -list1.reverse()
Sorting a list (ascending order) - Answers -scores = [75, 63, 92]
scores.sort()
print(scores) # gives [63, 75, 92]
Sorting a list (descending order) - Answers -scores = [75, 63, 92]
scores.sort(reverse = True)
print(scores) #gives [92, 75, 63]
ordered_list = sorted(scores)
#to store the new list
Splitting a String - Answers -list = "one two three".split()
Replication - Answers -list = [0] * 100
Two Ways to Create an Empty List: - Answers -list1 = [ ]
list1 = list ()
How to Create a List? - Answers -Put a sequence of items in [ ]
Ex. colors = ["red", "yellow", "blue"]
Reading 10 Values from a User then Finding the Minimum - Answers -values = [ ]
for i in range (0, 10):
v1 = int(input("Enter a value"));
values.append(v1);
print(values)
print(min(values))
Three Methods of Adding Elements to a List - Answers -list1.append(elem)
(adds a single element to the end of the list)
list1.insert(index,elem)
(inserts the elem at the given index, shifting elements to the right)
list1.extend(list2)
(adds the elements in list2 to the end of the list)
Access an Individual Element in a List Using an Index - Answers -list1[index]
Positive Indexes - Answers -- First element in the list has index 0
- Second element has index 1
-n'th element is n-1
Negative Indexes - Answers -- Index -1 identifies the last element
- -2 identifies the next to last element, etc.
Built-In List Functions in Python - Answers -function - description
len - returns the number of items in the list
min - returns the min among the elements in the list
max - returns the max among the elements in the list
sum - returns the sum of all elements in the list
Shuffle Function - Answers -from random import shuffle
shuffle(list1)
, How to Call Methods Provided by a List - Answers -list.methodName(arguments)
Traversing a List - Answers -for index in range (len(scores)):
print (index, "\t" , grade[i])
for index, grade in enumerate(scores):
print (index, "\t" ,grade[index])
To add a whole list to the end, use the extend method: - Answers -scores.extend([55,
88, 79])
# gives [85,72,56,98,84,72,55,88,79]
Inserting an element at a given location: - Answers -Names.insert (2, "Hanah")
Inserting a new element at position 2 in the list
Deleting an from a list by index - Answers -del list[index]
Deleting a list range by using a slice - Answers -del list[2:5]
Deleting a specific value from a list - Answers -colors.remove("blue")
or
if "blue" in colors:
pos = colors.index("blue")
del colors[pos]
Reversing a list - Answers -list1.reverse()
Sorting a list (ascending order) - Answers -scores = [75, 63, 92]
scores.sort()
print(scores) # gives [63, 75, 92]
Sorting a list (descending order) - Answers -scores = [75, 63, 92]
scores.sort(reverse = True)
print(scores) #gives [92, 75, 63]
ordered_list = sorted(scores)
#to store the new list
Splitting a String - Answers -list = "one two three".split()
Replication - Answers -list = [0] * 100