Created
December 11, 2011 19:57
-
-
Save dketov/1462411 to your computer and use it in GitHub Desktop.
Список, часть 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Определение | |
| """ | |
| a = ['spam', 'eggs', 100, 1234] | |
| print a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Присваивание | |
| """ | |
| aList = [123, 'abc', 4.56, ['inner', 'list'], 7-9j] | |
| anotherList = [None, 'something to see here'] | |
| print aList | |
| print anotherList | |
| aListThatStartedEmpty = [] | |
| print aListThatStartedEmpty | |
| print list('foo') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Добавление элементов | |
| """ | |
| scores = ["1","2","3"] | |
| # add a score | |
| score = int(raw_input("What score did you get?: ")) | |
| scores.append(score) | |
| # list high-score table | |
| for score in scores: | |
| print score | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Разница между добавлением и расширением | |
| """ | |
| li = ['a', 'b', 'c'] | |
| li.extend(['d', 'e', 'f']) | |
| print li | |
| print len(li) | |
| print li[-1] | |
| li = ['a', 'b', 'c'] | |
| li.append(['d', 'e', 'f']) | |
| print li | |
| print len(li) | |
| print li[-1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Длина списка | |
| """ | |
| a = ['spam', 'eggs', 100, 1234] | |
| print len(a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Срезы списка | |
| """ | |
| numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
| print numbers[3:6] | |
| print numbers[0:1] | |
| print numbers[7:10] | |
| print numbers[-3:-1] | |
| print numbers[-3:] | |
| print numbers[:3] | |
| print numbers[:] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Удаление среза | |
| """ | |
| a = ['spam', 'eggs', 100, 1234] | |
| # Remove some: | |
| a[0:2] = [] | |
| print a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Вставка элементов | |
| """ | |
| a = ['spam', 'eggs', 100, 1234] | |
| # Insert some: | |
| a[1:1] = ['bletch', 'xyzzy'] | |
| print a | |
| a[:0] = a # Insert (a copy of) itself at the beginning | |
| print a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| """ | |
| Удаление элемента | |
| """ | |
| li = ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements'] | |
| print li.remove("z") | |
| print li | |
| li.remove("new") | |
| print li | |
| li.remove("z") | |
| print li |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment