2
immutable lists
a sequence of zero or more values (just like lists)
addressable by index (just like lists)
iterable (just like lists)
Use ()
to create a new tuple:
Empty tuple:
tuples
are immutableNo assignment is allowed
Tuples can be used as dictionary keys (lists cannot).
It calculates and returns the total and average of all people’s numbers.
def descr_stats(numbers):
total = 0
count = 0
for i in range(1, len(numbers), 2):
total += numbers[i]
count += 1
return total, total/count
def main():
grades = ['Eric', 90, 'Sam', 87, 'Josh', 83, 'Susan', 100]
print(descr_stats(grades))
total, mean = descr_stats(grades)
assert total == 360
assert mean == 90.0
main()
(360, 90.0)
It takes a dictionary
as argument and returns the sum and the multiplication of the values in the dictionary
def sum_and_multiplication(dictionary):
total = 0
multiplication = 1
for k in dictionary:
total += dictionary[k]
multiplication *= dictionary[k]
return total, multiplication
def main():
test_dict = {"a": 2, "b": 3, "c": 5}
total, mult = sum_and_multiplication(test_dict)
assert total == 10
assert mult == 30
main()
It takes one list of integers
as argument and returns two lists: one with all the odd numbers and the other with all the even numbers.