mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Issue #28587: Improve list examples in the tutorial
This commit is contained in:
parent
6023d33433
commit
8c5e190d36
1 changed files with 20 additions and 24 deletions
|
@ -99,30 +99,26 @@ objects:
|
||||||
|
|
||||||
An example that uses most of the list methods::
|
An example that uses most of the list methods::
|
||||||
|
|
||||||
>>> a = [66.25, 333, 333, 1, 1234.5]
|
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
|
||||||
>>> print(a.count(333), a.count(66.25), a.count('x'))
|
>>> fruits.count('apple')
|
||||||
2 1 0
|
2
|
||||||
>>> a.insert(2, -1)
|
>>> fruits.count('tangerine')
|
||||||
>>> a.append(333)
|
0
|
||||||
>>> a
|
>>> fruits.index('banana')
|
||||||
[66.25, 333, -1, 333, 1, 1234.5, 333]
|
3
|
||||||
>>> a.index(333)
|
>>> fruits.index('banana', 4) # Find next banana starting a position 4
|
||||||
1
|
6
|
||||||
>>> a.index(333, 2) # search for 333 starting at index 2
|
>>> fruits.reverse()
|
||||||
2
|
>>> fruits
|
||||||
>>> a.remove(333)
|
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
|
||||||
>>> a
|
>>> fruits.append('grape')
|
||||||
[66.25, -1, 333, 1, 1234.5, 333]
|
>>> fruits
|
||||||
>>> a.reverse()
|
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
|
||||||
>>> a
|
>>> fruits.sort()
|
||||||
[333, 1234.5, 1, 333, -1, 66.25]
|
>>> fruits
|
||||||
>>> a.sort()
|
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
|
||||||
>>> a
|
>>> fruits.pop()
|
||||||
[-1, 1, 66.25, 333, 333, 1234.5]
|
'pear'
|
||||||
>>> a.pop()
|
|
||||||
1234.5
|
|
||||||
>>> a
|
|
||||||
[-1, 1, 66.25, 333, 333]
|
|
||||||
|
|
||||||
You might have noticed that methods like ``insert``, ``remove`` or ``sort`` that
|
You might have noticed that methods like ``insert``, ``remove`` or ``sort`` that
|
||||||
only modify the list have no return value printed -- they return the default
|
only modify the list have no return value printed -- they return the default
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue