list indexing and slicing
Elements in the list can be accessed with
[]
brackets. We can slice lists like strings with []
as well:l = [77, 0, -1, 4, 2, 1, 5, -2]
print(l) # [77, 0, -1, 4, 2, 1, 5, -2]
print(l[0]) # 77
print(l[2]) # -1
print(l[3]) # 4
print(l[-1]) # -2
print(l[-2]) # 5
print(l[1: 3]) # [0, -1]
print(l[1: 4]) # [0, -1, 4]
print(l[1: 5]) # [0, -1, 4, 2]
print(l[2: 3]) # [-1]
print(l[:2]) # [77, 0]
print(l[2:]) # [-1, 4, 2, 1, 5, -2]
Challenge
Declare a variable called
alphabet
that keeps all the uppercase letters of the English language: alphabet = ['A', 'B', ..., 'Z']
. The input contains a single integer n
. The program should print the first n
letters in the alphabet. It’s guaranteed that 1 ≤ n ≤ 26
.Input | Output |
1 | ['A'] |
2 | ['A', 'B'] |
Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB