Lab 5
1 create_tuple.py
code and result
list_example = ["Welcome ", "to ", "Fudan", " Python"]
# define the tuple
tuple_example = tuple(list_example)
# print the length
print("Length of tuple_example:", len(tuple_example))
# concatenate all the elements and print it
sentence = ''.join(tuple_example)
print("Concatenated sentence:", sentence)
# test modifying tuple_example
tuple_example[3] = " 285"
print(tuple_example)
running result:
1.1 define tuple
tuple_example = tuple(list_example)
When running print(tuple_example)
, the result is: ('Welcome ', 'to ', 'Fudan', ' Python')
.
1.2 print out length
print("Length of tuple_example:", len(tuple_example))
result: Length of tuple_example: 4
use len()
method to get its length
1.3 concatenate elements
sentence = ''.join(tuple_example)
When running print("Concatenated sentence:", sentence)
, the result is: Concatenated sentence: Welcome to Fudan Python
.
1.4 modify element
tuple_example[3] = " 285"
result: raise a TypeError exception TypeError: 'tuple' object does not support item assignment
Reason: tuples are immutable data types, meaning once created, their elements cannot be modified.
2 singleton_tuple.py
running result:
When running print(type(singleton_tuple))
, the result is: <class 'tuple'>
.
When running print(type(singleton))
, the result is: <class 'str'>
.
Reason:
singleton_tuple
is a tuple that contains only one element. The comma after the element, which ensures that the tuple is not mistakenly interpreted as a simple string.
singleton
is a string enclosed in parentheses without a comma. It’s not actually a tuple but just a string expression because there is no comma to differentiate it as a tuple.
3 extract_tuple.py
test cases:
# test cases
print(extract_tuple((100,200,300,400,500,600), 1, 4))
print(extract_tuple(('a','b','c','d','e','f','g'), 2, 6))
print(extract_tuple((), 0, 2))
print(extract_tuple((1,2,3,4,5), 0, 10))
print(extract_tuple((1,2,3,4,5), -1, 2))
print(extract_tuple((1,2,3,4,5), -2, 5))
print(extract_tuple((1,2,3,4,5), 2, 2))
running result:
- different arguments are tried as you can see in the screenshot above
- every result matches the expectation
Notice that except for the last function call print(extract_tuple((1,2,3,4,5), 2, 2))
, the results of all other function calls are very similar to results when we extract the list.
While in the last function call:
When running print(extract_tuple((1,2,3,4,5), 2, 2))
, the result is: (3,)
.
Unlike the extract_list()
function in the previous lab, we can see there’s a comma behind the only element.
This is because when a tuple contains only one element, a trailing comma is added after the element to differentiate it from a regular expression enclosed in parentheses.
4 unpack_tuple.py
running result:
4.1 unpack
fruit_x is: apple
fruit_y is: orange
4.2 swap
fruit_x is: orange
fruit_y is: apple
5 number_pairs.py
code
def pair_numbers(list1, list2):
pairs = []
for num1 in list1:
for num2 in list2:
pairs.append((num1, num2))
return pairs
test cases
# test cases for function pair_numbers()
print("test cases for function pair_numbers():")
print(pair_numbers([1], [1]))
print(pair_numbers([1, 2], [3, 4]))
print(pair_numbers([], []))
print(pair_numbers([1, 2, 3], [4, 5]))
print(pair_numbers([1, 2, 3], []))
print(pair_numbers([], [4, 5, 6]))
running result
- different arguments are tried as you can see in the screenshot above
- every result matches the expectation
6 sum_of_product()
code
def sum_of_product(tuple_list):
sum = 0
for tuple in tuple_list:
product = tuple[0] * tuple[1]
sum += product
return sum
test cases
# test cases for function sum_of_product()
print("\ntest cases for function sum_of_product():")
print(sum_of_product([(1, 2), (3, 4)]))
print(sum_of_product([]))
print(sum_of_product([(2, 2), (5, 3), (0, 1)]))
running result
same as the screenshot in question 5 above
- different arguments are tried as you can see in the screenshot above
- every result matches the expectation