Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays & Loops.

Similar presentations


Presentation on theme: "Arrays & Loops."— Presentation transcript:

1 Arrays & Loops

2 Learning Objectives Understand the difference between arrays and lists
Understand and be able to use an index for a one-dimensional array or list Be able to use a loop structure to iterate through an array or list

3 Arrays or Lists An array is a data structure that allows you to hold many items of data, all of the same datatype with one name. In python Arrays are called Lists, where each element can be a different datatype An list of 10 student names could be stored in a list: In most programming languages the “index” starts at 0: userName[0], userName[1], …. userName[9] The elements of the array are typically referred to as userName[1], userName[2], …. userName[10]

4 Advantages of using Lists
Imagine that you want to input, sort and print 100 user names. It would be very inconvenient to use 100 different variable names With a list you can have as many data items as you need stored using one variable name Non-sequential nature – a program can jump between non- neighbouring elements instantly without having to browse through the whole array

5 Advantages of using Lists
Ease of iterating through an array – which allow programmers to manipulate multiple items of data with very few lines of code, replacing many separate variables. FOR number = 1 TO 100 userName[number] = Input(“ Enter a user name”) NEXT number username = sort(userName) Print (userName[number]) Next number

6 Recognising Lists or Arrays
Not all languages use square brackets as Python does for indices Some use round brackets which might cause confusion because functions and procedures also have round brackets next to their identifiers. For example in Visual Basic getPupilData(4) is a function call and pupils(4) is the 5th element (assuming zero-based array) of an array named pupils. In Python you can identify arrays by the index that appears next to array identifiers. E.g. pupils[2]

7 Activity1 : spot an array
Counter = 3 Counter = getData(5) OUTPUT Widgets[4] Names[2] = findMax(2) OUTPUT Sentence.SPLIT(“ “) FOR Counter = 1 TO LEN(Lines)-1

8 Activity1 : spot an array answer
Counter = 3 Just a variable Counter = getData(5) A var  a function OUTPUT Widgets[4] Widgets is Array! Names[2] = findMax(2) Names is Array! OUTPUT Sentence.SPLIT(“ “) Array is output FOR Counter = 1 TO LEN(Lines)-1 Lines is an array

9 Activity 2 The user inputs 5 numbers
The program outputs the first and the last number. Things to consider: Initialising the list Using prompts and input Datatype of stored data Here is an example of what the program should look like it when it runs: ARRAY Numbers[5] FOR count = 0 TO 4 OUTPUT ”Data Item” + count INPUT Numbers[count] NEXT count OUTPUT Numbers[0] OUTPUT Numbers[4] save as For Loop Data Fixed Size

10 Activity 3 Design and code solutions for the following tasks:
Task A. Modify the code in Activity1 to allow the user to enter the number of data items before the loop starts. Task B. Modify the code to allow the user continue entering data items until a break code such as -1 is entered Task C. Modify the code from Task B so the the user enters data in the same manner with the added feature that allows the list to be searched for an item of data. Your code will ask the user to enter a number to be searched for. If it is found a message displaying the index position will be displayed or if it doesn’t exist in the list display an appropriate message Task D. Modify the code in Task B to use an alternative type of loop. Eg if you have used a For loop, change it to a While or vice versa Note: for additional Python resources using loops and lists checkout this link: save as For Loop Variable Size save as List and Loop break save as List and Loop Find

11 Common Errors Since traditional arrays have fixed sizes, it is quite common to lose track of the length of the array and attempt to assign a value to a non-existent index: ARRAY Numbers[4] Numbers[5]:=9  will cause a crash with a message “Index not found” Off By One (OBO) errors Forgetting to take account of the fact that array indices start at 0 and for example using the len() function without subtracting 1 Only Python supports negative indices !

12 Validating for array size
Best practice before writing to or reading from an array is to find out its length: Wrong: ARRAY Numbers[4] FOR count = 0 TO 5 Numbers[count] = RAND (1,100) NEXT count Right: ARRAY Numbers[4] FOR count = 0 TO LEN(Numbers)-1 Numbers[count] = RAND (1,100) NEXT count In Python:

13 Useful Info Programmers count from ZERO!
The first item in a list is therefore at position ‘0’ Setting up an empty list: Adding a variable or literal to a list: To count the number of items in a list use the len() function e.g. Lists always use square brackets Setting up a list of strings when the list is initialised


Download ppt "Arrays & Loops."

Similar presentations


Ads by Google