Copyright © Wei MEI, MLMS™—all rights reserved. 🀤

# Collections¶

Collections are groups of items. Python supports several types of collections. Three of the most common are dictionaries, lists and arrays.

# Lists¶

Lists are a collection of items. Lists can be expanded or contracted as needed, and can contain any data type. Lists are most commonly used to store a single column collection of information, however it is possible to nest lists

# Arrays¶

Arrays are similar to lists, however are designed to store a uniform basic data type, such as integers or floating point numbers.

# Dictionaries¶

Dictionaries are key/value pairs of a collection of items. Unlike a list where items can only be accessed by their index or value, dictionaries use keys to identify each item.

# Demo: dates¶

# PPT Demonstrations¶

# Challenges time¶

Check the following script and try to find the mistake:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ```
# Create a calculator function
# The function should accept three parameters:
# first_number: a numeric value for the math operation
# second_number: a numeric value for the math operation
# operation: the word 'add' or 'subtract'. The default operation is 'add'
# the function should return the result of the two numbers added or subtracted
# based on the value passed in for the operator
#
# Test your function using named notation passing in only the numbers 6 and 4
# Should return 10
#
# Test your function using named notation with the values 6,4, subtract
# Should return 2
#
# BONUS: Test your function with the values 6, 4 and divide
# Have your function return an error message when invalid values are received
``` |

solutions:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ```
# Create a calculator function
# The function should accept three parameters:
# first_number: a numeric value for the math operation
# second_number: a numeric value for the math operation
# operation: the word 'add' or 'subtract'. The default operation is 'add'
# the function should return the result of the two numbers added or subtracted
# based on the value passed in for the operator
#
def calculator(first_number, second_number, operation='ADD'):
if operation.upper() == 'ADD':
return(float(first_number) + float(second_number))
elif operation.upper() =='SUBTRACT':
return(float(first_number) - float(second_number))
else:
return('Invalid operation please specify ADD or SUBTRACT')
# Test your function using named notation passing in only the numbers 6 and 4
# Should return 10
print('Adding 6 + 4 = ' + str(calculator(first_number=6, second_number=4)))
# Test your function using named notation with the values 6,4, subtract
# Should return 2
#
print('Subtracting 6 - 4 = ' + str(calculator(first_number=6, second_number=4, operation='subtract')))
``` |