>>>
cubes
=
[
1
,
8
,
27
,
65
,
125
]
# something's wrong here
>>>
4
**
3
# the cube of 4 is 64, not 65!
64
>>>
cubes[
3
]
=
64
# replace the wrong value
>>>
cubes
[1, 8, 27, 64, 125]
Μπορείτε επίσης να προσθέσετε νέα στοιχεία στο τέλος
της λίστας, με τη χρήση της append():
>>>
cubes
.
append(
216
)
# add the cube of 6
>>>
cubes
.
append(
7
**
3
)
# and the cube of 7
>>>
cubes
[1, 8, 27, 64, 125, 216, 343]
Είναι δυνατόν, να αλλάξετε το μέγεθος της λίστας ή
ακόμα και να την καταργήσετε εντελώς:
>>>
letters
=
[
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
,
'g'
]
>>>
letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>>
# replace some values
>>>
letters[
2
:
5
]
=
[
'C'
,
'D'
,
'E'
]
>>>
letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>>
# now remove them
>>>
letters[
2
:
5
]
=
[]
>>>
letters
['a', 'b', 'f', 'g']
>>>
# clear the list by replacing all the elements with an
empty list
>>>
letters[:]
=
[]
>>>
letters
[]
Η ενσωματωμένη συνάρτηση len () ισχύει και για τις λίστες:
>>>
letters
=
[
'a'
,
'b'
,
'c'
,
'd'
]
>>>
len
(letters)
4
- A Mini greek notebook for Learning Python Programming
12