1.
Q1: Put the code in the appropriate order so that a
list
of integers will store multiples of 5 (from 100 down to 0) in reverse order.
range
to generate a sequence of numbers and then converting that to a list.
listName[index]
returns value stored at that index.
0
and len(listName)-1
, inclusive, or a negative value; otherwise an IndexError exception occurs at runtime.
listName[startIndex:endIndex]
returns a new list containing the elements from startIndex
to endIndex-1
(inclusive)
startIndex
starts from the beginning of the list, and omitting endIndex
goes to the end of the list
append
method to add the new value to the end of the list. Note that you do not use an assignment statement, it is just a method call.
insert
method to add the new value at the specified index. Note that you do not use an assignment statement, it is just a method call.
collection_name.insert(index, new_value)
- add a new element at the specified index in the list.
for
loop:
for var_name in collection_name
- traverses collection_name from first element to last element storing a copy of each element from collection_name in var_name for each iteration of the loop.
for i in range(len(collection_name))
- traverses collection_name
from first element to last element storing the index of each element in i
for each iteration of the loop.
i
as index into list, or var_name
as value of list element
list
of integers will store multiples of 5 (from 100 down to 0) in reverse order.
list
? (Select all that are appropriate)
favorite_authors
nine_hundred
ribbon_colors
top_ten
classroom_heights
list
named numbers
. Which of the following code snippets would be best for this task?
def add_one(numbers):
for i in range(len(numbers)):
numbers[i] = numbers[i] + 1
def add_one(numbers):
for num in numbers:
num += 1
def add_one(numbers):
for num in numbers:
numbers[num] = num + 1
num
as both index and value.def add_one(numbers):
for i in range(len(numbers)):
numbers[i] = i + 1