- 2
Can anyone explain this nums = list(range(3, 15, 3)) print(nums[2])
4 Réponses
+ 5
# Copy this code to play with.
nums = list(range(3, 15, 3))
print(nums) # shows the list
print()
#`list slicing starts from 0
print(nums[0])
print(nums[1])
print(nums[2])
print(nums[3])
+ 15
Let me translate this to English.
line 1 says :-
Iterate from 3 to 15 in steps of 3 and put every number in a list named *nums*
So after first line, "nums" will look something like this
{3,6,9,12}
Now second line says that take the value at index == 2 of nums and print it's value, which turns out to be 9 as list index starts from 0 and not 1.
So the final output would be 9.
+ 7
The answer is 9:
Since the list of numbers in num were 3, 15, and 3, the last number 3 is the skip count of the range between 3 and 15
When nums is printed out from the [2] thing, it resulted printing out 9:
Visualization:
0 = 3
1 = 6
2 = (9)
Note it that the last number is covered with parentheses.
Remember, counting arrays start from 0
+ 6
nums is a list which will look like this: [ 3, 6, 9, 12]
list slicing -> nums[2] will give a result of 9
I will try to find the python lessons pertaining to this and post them here for you to review