You are on page 1of 30

CSCI 1101

Naomi Bolotin

Tuples

1
Tuple
• ordered collection

can contain any data type

immutable
(vs. list, which is modifiable)

2
Tuple
• comma-separated list of values

fruits = 'apples', 'oranges', 'grapes'

commonly put in parentheses

fruits = ('apples', 'oranges', 'grapes')

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

collection = ('apple', 2.78, False, -1, [7, 8])

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()

words = 'hello', 'world'


print(len(words))
2

11
Basic operations
• in operator

print('hello' in words)
True

print('world' not in words)


False

12
Basic operations
• looping by item

words = 'hello', 'world'


for w in words:
print(w)

hello
world

13
Sequence operations
• indices
fruit = ('apples', 'oranges', 'grapes')
print (fruits[1])
'oranges'

14
Sequence operations
• slices

fruit = ('apples', 'oranges', 'grapes')


print fruits[1:3]
('oranges', 'grapes')

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

print('The price is',price)


The price is 12.47

=> list would also print brackets [ ]


=> + requires casting to str first

20
Swapping
• first variable normally overwritten

one = 'a'
two = 'b'

one = two
two = one
print(two) # 'b'

• temporary variable needed to save its value


temp = one
one = two
two = temp
print(two) # ‘a'

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

for last, first in directory:


print (first, last, directory[last,first]) # smith, john, 111

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

useful when tuple contains lots of data,


as can use loop to traverse it

25
Return values
• can save each item in separate variable

quotient, remainder = divmod(5, 2)


print(quotient, remainder)
21

useful when just have few pieces of data

26
Return values
• can write own function that returns tuple

def dollars_cents (price):


dollars, cents = price.split('.')
return dollars, cents

27
Return values
print(dollars_cents(12.25)) # print result returned directly
12 25

or

amounts = dollars_cents(12.25)) # save result in tuple first


print(amounts[0], amounts[1])
12 25

or

d, c = dollars_cents(12.25)) # save result in individual variables first


print(d, c)
12 25

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

You might also like