Created
December 11, 2011 20:19
-
-
Save dketov/1462528 to your computer and use it in GitHub Desktop.
Кортеж
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 -*- | |
""" | |
Определение | |
""" | |
t = () | |
print t | |
t = 12345, 54321, 'hello!' | |
print t[0] | |
print t | |
singleton = 'hello', # <-- note trailing comma | |
print len(singleton) | |
print singleton |
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 -*- | |
""" | |
Присваивание | |
""" | |
nudge = 1 | |
wink = 2 | |
A, B = nudge, wink # tuple assignment | |
print A, B # like A=nudge; B=wink | |
nudge = 1 | |
wink = 2 | |
nudge, wink = wink, nudge # tuples: swaps values | |
print nudge, wink # like T=nudge; nudge=wink; wink=T |
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 -*- | |
""" | |
Кортежи являются неизменяемыми | |
""" | |
T = (1, 2, 3) | |
T[2] = 4 # error! | |
T = T[:2] + (4,) # okay: (1, 2, 4) | |
print T |
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 -*- | |
""" | |
Преобразование кортежа в список | |
""" | |
T = ('cc', 'aa', 'dd', 'bb') | |
tmp = list(T) | |
tmp.sort() | |
print tmp |
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 -*- | |
""" | |
Проверка наличия элемента | |
""" | |
inventory = ("a", | |
"b", | |
"c", | |
"d") | |
if "c" in inventory: | |
print "You will live to fight another day." |
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 -*- | |
""" | |
Объединение кортежей | |
""" | |
inventory = ("a", "b", "c", "d") | |
chest = ("gold", "gems") | |
print chest | |
inventory += chest | |
print inventory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment