What is the difference between the following two loops? Assume a variable called "nums"…
- User
What is the difference between the following two loops? Assume a variable called "nums" has been created which stores a list of numbers.
Loop #1
for i in nums:
codei = i + 1Loop #2
for i in range(len(nums)):
codenums[i] = nums[i] + 1Question options:
a) Loop #2 accesses elements by indices, Loop #1 does not
b) Loop #1 accesses elements by indices, Loop #2 does not
c) Loop #2 does not update each item in the list, Loop #1 does
d) Loop #2 does not loop over the entire list, Loop #1 does
- Assistant
a) Loop #2 accesses elements by indices, Loop #1 does not.
- User
Assume that you have created a variable called "words" which stores a list of words. Which code below will print every other word in the list?
Question options:
a) print(words[:2])
b) print(words[::2])
c) for i in words[::-1]:
codeprint(i)d) for i in words[::2]:
codeprint(i) - Assistant
d) for i in words[::2]:
codeprint(i)