Get Familiar with Lists
Another crucial data type is called list! It is used to store variables:
list = [1, 3, 6, 'a', True]
You just need to put variables inside square brackets. You don't have restrictions, so in one list, you can store numbers, strings, and boolean data; that's all!
It is necessary to know how to get a specific element from the list to work with lists.
The list's first item has the index 0
, the next one 1
, and so on.
If we start from the end, the last item has the index -1
, and the second-to-last has the index -2
, and so on.
12345678910111213list_1 = ['P', 'y', 't', 'h', 'o', 'n'] print("The first item is", list_1[0]) print("The third item is", list_1[2]) print("The fifth item is", list_1[4]) print("The last item is", list_1[5]) # And using negative indexation print("The first item is", list_1[-6]) print("The third item is", list_1[-4]) print("The fifth item is", list_1[-2]) print("The last item is", list_1[-1])
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 2.08
Get Familiar with Lists
Stryg for at vise menuen
Another crucial data type is called list! It is used to store variables:
list = [1, 3, 6, 'a', True]
You just need to put variables inside square brackets. You don't have restrictions, so in one list, you can store numbers, strings, and boolean data; that's all!
It is necessary to know how to get a specific element from the list to work with lists.
The list's first item has the index 0
, the next one 1
, and so on.
If we start from the end, the last item has the index -1
, and the second-to-last has the index -2
, and so on.
12345678910111213list_1 = ['P', 'y', 't', 'h', 'o', 'n'] print("The first item is", list_1[0]) print("The third item is", list_1[2]) print("The fifth item is", list_1[4]) print("The last item is", list_1[5]) # And using negative indexation print("The first item is", list_1[-6]) print("The third item is", list_1[-4]) print("The fifth item is", list_1[-2]) print("The last item is", list_1[-1])
Tak for dine kommentarer!