Skip to content

Instantly share code, notes, and snippets.

@dketov
Created December 11, 2011 20:19
Show Gist options
  • Save dketov/1462528 to your computer and use it in GitHub Desktop.
Save dketov/1462528 to your computer and use it in GitHub Desktop.
Кортеж
# -*- 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
# -*- 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
# -*- encoding: utf-8 -*-
"""
Кортежи являются неизменяемыми
"""
T = (1, 2, 3)
T[2] = 4 # error!
T = T[:2] + (4,) # okay: (1, 2, 4)
print T
# -*- encoding: utf-8 -*-
"""
Преобразование кортежа в список
"""
T = ('cc', 'aa', 'dd', 'bb')
tmp = list(T)
tmp.sort()
print tmp
# -*- encoding: utf-8 -*-
"""
Проверка наличия элемента
"""
inventory = ("a",
"b",
"c",
"d")
if "c" in inventory:
print "You will live to fight another day."
# -*- 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