Skip to content

Instantly share code, notes, and snippets.

@pknowledge
Created September 2, 2018 19:20
Show Gist options
  • Save pknowledge/5060e910b86a6edbb32e1337ddd00dfe to your computer and use it in GitHub Desktop.
Save pknowledge/5060e910b86a6edbb32e1337ddd00dfe to your computer and use it in GitHub Desktop.
Python Tuples
>>> x = (1,5,3,4,8)
>>>
>>> x
(1, 5, 3, 4, 8)
>>> x[0]
1
>>> x[4]
8
>>> x[100]
Traceback (most recent call last):
File "<input>", line 1, in <module>
IndexError: tuple index out of range
>>> x[0] = 2
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> x.count(8)
1
>>> len(x)
5
>>> y = (1, 'max', 1.6)
>>> y
(1, 'max', 1.6)
>>> x
(1, 5, 3, 4, 8)
>>> y
(1, 'max', 1.6)
>>> z = x + y
>>> z
(1, 5, 3, 4, 8, 1, 'max', 1.6)
>>> a = ('hi',) * 5
>>> a
('hi', 'hi', 'hi', 'hi', 'hi')
>>> a[2]
'hi'
>>> max(x)
8
>>> min(x)
1
>>> del z
>>> z
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'z' is not defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment