Professional Documents
Culture Documents
Tuples
Tuples
Naomi Bolotin
Tuples
1
Tuple
• ordered collection
immutable
(vs. list, which is modifiable)
2
Tuple
• comma-separated list of values
3
Single tuple
• item must be followed by comma
fruit = 'apple',
or
fruit = ('apple',)
print(type(fruit))
<type 'tuple'>
4
Single tuple
• otherwise assumed to be string
fruit = ('apple')
print(type(fruit))
<type 'str'>
5
Data type
• items in tuple can be of any type, as with lists
6
Creating tuple
• ()
fruits = ()
• tuple()
fruits = tuple()
print (fruits)
()
7
tuple()
• takes one parameter
data must be passed as tuple
fruits = tuple(('apple','orange'))
print(fruits)
('apple', 'orange')
fruits = tuple(('apple',))
print (fruits)
('apple',)
8
tuple()
• or list
numbers = tuple([1, 2, 3])
print (numbers)
(1, 2, 3)
9
tuple()
• if string passed to it, will be segmented into
characters
fruit = tuple('apple')
print (fruit)
('a', 'p', 'p', 'l', 'e')
10
Basic operations
• len()
11
Basic operations
• in operator
print('hello' in words)
True
12
Basic operations
• looping by item
hello
world
13
Sequence operations
• indices
fruit = ('apples', 'oranges', 'grapes')
print (fruits[1])
'oranges'
14
Sequence operations
• slices
15
Sequence operations
• random.choice()
fruit = ('apples', 'oranges', 'grapes')
print(random.choice(fruits))
grapes
16
Sequence operations
• loop by index
for i in range(len(fruit)):
print(fruit[i])
apples
oranges
grapes
17
Immutable
• can’t replace item in tuple, as with string
fruits[0] = 'strawberries'
TypeError: object doesn't support item assignment
18
Immutable
• to modify, replace one tuple with another
fruits = ('strawberries',) + fruits[1:]
print (fruits)
('strawberries', 'oranges', 'grapes')
19
Printing
• can use tuple to print
items can be of any type
printed separated by spaces
20
Swapping
• first variable normally overwritten
one = 'a'
two = 'b'
one = two
two = one
print(two) # 'b'
21
Swapping
• can be done in one step with tuples
=> expressions on right evaluated before
assignments made
one = 'a'
two = 'b'
one, two = two, one # values of two, one saved first
# subsequently assigned to one, two
print(two) # 'a'
22
Keys
• tuple can serve as key in dictionary
(vs. list, which cannot because mutable)
directory[last,first] = number
23
Return values
• functions can only return one value
can use tuple (or list) to return multiple values
divmod()
returns tuple containing quotient and remainder
24
Return values
• can save tuple in variable
access by index
results = divmod(5, 2)
print(results[0], results[1])
2 1
25
Return values
• can save each item in separate variable
26
Return values
• can write own function that returns tuple
27
Return values
print(dollars_cents(12.25)) # print result returned directly
12 25
or
or
28
Sequences of sequences
• can have one data structure within another
list of tuples
list of lists
tuple of tuples
tuple of lists
29
Other bases
• bin(n) returns n in binary (0b prefix)
oct(n) returns n in octal (0o prefix)
hex(n) returns n in hexadecimal (0x prefix)
print(bin(10))
0b1010
print(oct(10))
0o12
print(hex(10))
0xa
30