List Slicing

To get an element from a list, use square brackets with the index of the element. Indexes start at 0:
x = ["a", "b", "c", "d", "e", "f", "g"]
print(x[2])
# c
To get more than one element, use slice notation inside of the brackets. The full syntax is [start:stop:step]. Start is the starting index (inclusive), stop is the ending index (exclusive), and step is the step size. A few notes about slicing:
  • If there are no colons, the single number is used as an index to get a single element
  • If there is just one colon, the step size defaults to 1
  • If there are missing values, the default values are [0:len(list):1]
Examples:
x = ["a", "b", "c", "d", "e", "f", "g"]

print(x[1]) # b
print(x[1:]) # ["b", "c", "d", "e", "f", "g"]
print(x[1:4]) # ["b", "c", "d"]
print(x[1::2]) # ["b", "d", "f"]
print(x[1:6:3]) # ["b", "e"]

print(x[:5]) # ["a", "b", "c", "d", "e"]
print(x[:5:2]) # ["a", "c", "e"]

print(x[::2]) # ["a", "c", "e", "g"]
Use negative numbers to count backwards from the end of the list. Use -1 for the step size to get the list in reverse. Examples:
x = ["a", "b", "c", "d", "e", "f", "g"]

print(x[-1]) # g
print(x[:-2]) # ["a", "b", "c", "d", "e"]
print(x[::-1]) # ["g", "f", "e", "d", "c", "b", "a"]

Exercise 1 of 11

Print the first letter.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code

Exercise 2 of 11

Print the last letter.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code

Exercise 3 of 11

Print the second to last letter.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code

Exercise 4 of 11

Print the twentieth letter.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code

Exercise 5 of 11

Print the letters starting from d.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code

Exercise 6 of 11

Print the letters up to t.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code

Exercise 7 of 11

Print the letters from f to m.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code

Exercise 8 of 11

Print the letters in reverse.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code

Exercise 9 of 11

Print every other letter.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code

Exercise 10 of 11

Print every third letter starting with e.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code

Exercise 11 of 11

Print every other letter from f to t.
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

Your code