-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_tuples.py
More file actions
23 lines (18 loc) · 848 Bytes
/
16_tuples.py
File metadata and controls
23 lines (18 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Python Tuple is a collection of objects separated by commas. A tuple is similar to a
# Python list in terms of indexing, nested objects, and repetition but the main difference
# between both is Python tuple is immutable, unlike the Python list which is mutable.
things = 'pen', 'pencil', 'water bottle', 'charger', 'phone', 'web cam'
print(type(things)) # <class 'tuple'>
print(things[0]) # pen
print(things[3:6]) # ('charger', 'phone', 'web cam')
print(things[::-1]) # ('web cam', 'phone', 'charger', 'water bottle', 'pencil', 'pen')
if 'pen' in things:
print('exist')
for item in things:
print(item)
print(len(things)) # 6
mega = ([2,3,4],[6,8,9,5])
print(mega[0]) # [2, 3, 4]
# mega[0] = [6,7,8] TypeError: 'tuple' object does not support item assignment
mega[0][1] = 666 # ([2, 666, 4], [6, 8, 9, 5])
print(mega)