["Foo", "Bar", "Baz"]
>>> words = ["Foo", "Bar", "Baz", "Qux"]
>>> primes = [2, 3, 5, 7, 11, 13, 17, 19]
>>> asl = [21, "Male", "Sofia"]
>>> dish = ["spam", "bacon", "sausage", "spam"]
>>> actor = "Eric idle"
>>> sketch = "Spam"
>>> stuff = [actor, 42, sketch]
>>> print stuff
['Eric Idle', 42, 'Spam']
>>> stuff[1] = stuff
>>> print stuff
['Eric Idle', [...], 'spam']
len(списък)
>>> len(["Foo", "Bar", "Baz", "Qux"])
4
+
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
in
>>> "Spam" in ["Eggs", "Bacon", "Spam"]
True
>>> cheeses = ['Red Leicester', 'Cheddar', 'Emmental', 'Mozzarella',
'Tilsit', 'Limburger']
>>> cheeses[0]
'Red Leicester'
>>> cheeses[-1]
'Limburger'
>>> cheeses[1:4]
['Cheddar', 'Emmental', 'Mozzarella']
>>> cheeses[1:-1]
['Cheddar', 'Emmental', 'Mozzarella', 'Tilsit']
>>> cheeses[1:]
['Cheddar', 'Emmental', 'Mozzarella', 'Tilsit', 'Limburger']
>>> cheeses[:3]
['Red Leicester', 'Cheddar', 'Emmental']
>>> cheeses[0:6:2]
['Red Leicester', 'Emmental', 'Tilsit']
>>> cheeses[::2]
['Red Leicester', 'Emmental', 'Tilsit']
>>> cheeses[::-1]
['Limburger', 'Tilsit', 'Mozzarella', 'Emmental',
'Cheddar', 'Red Leicester']
>>> food = ['eggs', 'bacon', 'spam']
>>> print food
['eggs', 'bacon', 'spam']
>>> food[1] = 'sausage'
>>> print food
['eggs', 'sausage', 'spam']
>>> food.append('spam')
>>> print food
['eggs', 'sausage', 'spam', 'spam']
>>> del food[1]
>>> print food
['eggs', 'spam', 'spam']
>>> food[:2] = ['I', 'want', 'more']
>>> print food
['I', 'want', 'more', 'spam']
>>> del food[:3]
>>> print food
['spam']
>>> knights = ["Arthur", "Galahad"]
>>> knights.append('Bedevere')
>>> knights
['Arthur', 'Galahad', 'Bedevere']
>>> knights.extend(['Lancelot', 'Robin')
# Може и knigts += ['Lancelot', 'Robin']
>>> knights
['Arthur', 'Galahad', 'Bedevere', 'Lancelot', 'Robin']
>>> knights.sort()
>>> knights
['Arthur', 'Bedevere', 'Galahad', 'Lancelot', 'Robin']
>>> knights.reverse()
>>> knights
['Robin', 'Lancelot', 'Galahad', 'Bedevere', 'Arthur']
>>> someone = knights.pop()
>>> print someone
'Arthur'
>>> print knights
['Robin', 'Lancelot', 'Galahad', 'Bedevere']
>>> food = ['spam', 'eggs', 'sausage', 'spam', 'bacon', 'spam']
>>> food.index('eggs')
1
>>> food.count('spam')
3
>>> food.count('bacon')
1
>>> food.insert(2, 'spam')
['spam', 'eggs', 'spam', 'sausage', 'spam', 'bacon', 'spam']
for
for [променлива] in [списък]:
[блок код]
in
е част от синтаксиса, не проверка за притежание[списък]
може да е както литерал - [1, 2, 3]
така и име, което сочи към списък - knights
for
- след малко
numbers = [1, 2, 3, 5, 7, 11, 13]
answer = 0
for n in numbers:
answer += n
print "Adding %d to the answer" % n
print answer
primes = (2, 3, 5, 7, 11, 13)
>>> primes[0]
2
>>> primes[1:4]
(3, 5, 7)
>>> primes[5] = 17
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
("Foo", "Bar", ("Spam", "Larodi"), ("More, "Spam"))
>>> (a, b) = 1, 2
>>> print a
1
>>> a, b = 1, 2
>>> a, b = b, a
>>> print a
2
>>> print b
1
>>> numbers = (1, 2, 3)
>>> a, b, c = numbers
Списъците/n-орките се сравняват лексигокрафически
>>> (1, 2) < (1, 3)
True
>>> ("Foo", 2) < (1, 2)
False
>>> (1, 2) < (1, 2, 3)
True
>>> [1, 2] < [1, 3]
True
>>> (1, 2) < [1, 3]
False
set([списък])
>>> numbers = set([1, 2, 3])
>>> 1 in numbers
True
>>> 4 in numbers
False
>>> numbers.add(4)
>>> 4 in numbers
True
>>> numbers.remove(1)
>>> languages = {
'Spain': 'Spanish',
'Great Britain': 'English',
'Italy': 'Italian',
'Mexico': 'Spanish',
'France': 'French',
'USA': 'English',
}
>>> print languages['Mexico']
'Spanish'
>>> capitals = {
'Germany': 'Berlin',
'France': 'Paris',
'Brazil': 'Rio de Janeiro',
'Malaysia': 'Kuala Lumpur',
}
>>> print capitals['Brazil']
'Rio de Janeiro'
>>> capitals['Brazil'] = 'Brazil'
>>> print capitals['Brazil']
'Brazil'
>>> capitals['Sweden'] = 'Stockholm'
>>> print capitals['Sweden']
'Stockholm'
>>> del capitals['Malaysia']
>>> print capitals['Malaysia']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'capitals' is not defined
>>> capitals = {
'Germany': 'Berlin',
'France': 'Paris',
'Brazil': 'Rio de Janeiro',
'Sweden': 'Stockholm',
}
>>> capitals.get('Assyria')
None
>>> capitals.get('Assyria', "I don't know")
"I don't know"
>>> capitals.has_key('Sweden')
True
>>> capitals.keys()
['Brazil', 'France', 'Germany', 'Sweden']
>>> capitals.values()
['Berlin', 'Paris', 'Brazil', 'Stockholm']
>>> numbers = {
"One": "I",
"Two": "II",
}
>>> numbers.items()
[('One', 'I'), ('Two', 'II')]
>>> numbers.update({"Three": "III", "Four": "IV"})
{'Four': 'IV', 'Three': 'III', 'Two': 'II', 'One': 'I'}
>>> numbers.pop('Four')
'IV'
>>> numbers
{'Three': 'III', 'Two': 'II', 'One': 'I'}
>>> numbers.popitem()
{'Three': 'III'}
>>> numbers.popitem()
{'One': 'I'}
>>> dict(france="Paris", italy="Rome")
{'italy': 'Rome', 'france': 'Paris'}
>>> dict([('One', 'I'), ('Two', 'I')])
{'Two': 'I', 'One': 'I'}
for [променлива] in [колекция]
[блок]
break
излиза от цикълаcontinue
прекъсва текущата итерация и продължава изпълнението на цикъла от следващия елементrange(x, y)
връща списък от числата в интервала [x, y)
>>> range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(0, 10, 2)
[0, 2, 4, 6, 8]
>>> range(10, 0, -1)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
for n in range(0, 20):
if n % 2: continue
print n
if n > 10: break
Произвежда следния резултат:
0
2
4
6
8
10
12
roman = {1: 'I', 2: 'II', 3: 'III', 4: 'IV', 5: 'V'}
numbers = [2, 3, 5]
for n in numbers:
print n, "is", roman[n]
Получава:
2 is II in Roman numbers
3 is II in Roman numbers
5 is V in Roman numbers