#######LISTS, ARRAYS, DICTIONARIES AND SORTING###### #Here we are going to look at arrays, lists, dictionaries and sorting #Again remember to change file extension on here to .py from .txt before executing #1. Arrays or Lists - An array is a series of items made up into a lists, an example of an array is show below #First we shall create an array called numbers and give it values #The values specified are surrounded by square brackets and split up by commas as shown numbers = [5,4,1,2,3] #Now when we print numbers it will show the whole array split up by commas print numbers #If we only wish to print one element of numbers we can do so with the following code print numbers[0] #this will print out the first value in numbers (5), we surround element by square brackets here #2. Lists - In python an array is a list and vise verse, python has no array element although sometimes lists are refered to as arrays #3. dictionaries - A dictionary is similar to a list but it has keys representing values within it #Imagine a database, a database may have an id column and also columns for first name, surname and age #a dictionary will contain data about all #The following example shows a dictionary called dict being created for peoples id, forename, surhame and age dict = {'ID': 0, 'Forename': 'John', 'Surname': 'Smith', 'Age': 25} # notice here, on the left is the key and its value is on the right #Notice above we put any strings in quotes, we don't need to put integers in quotes and split kep and value up by colon(:) print dict.items() #this will print out the values from within our dictionary #This will print out the value of 'ID' print dict['ID'] #4. Sorting - here we look at sorting arrays/lists and dictionaries #When sorting a list we can do this easilly using the sort function, we shall sort our bumbers list numbers.sort()#this will sort a list into values from low to high or ascending order, alphabetical for strings print numbers #notice after print how the elements have changed, you can print various elements also #This will sort the dictionary by keys, so in alphabetical order, note that mixing numbers and strings in keys will not sort properly print sorted(dict.keys()) #you can also sort by items by using dict.items() instead of dict.keys() #CONGRATULATIONS YOU NOW KNOW HOW TO SET UP BASIC ARRAYS, DICTIONARIES AND BASIC SORTING IN PYTHON