Courses/Computer Science/CPSC 203/CPSC 203 Template/Labs Template/Week 2 - Lab 2: Datastructures

Skills

 * Understand the use of lists (arrays) to represent a collection of related data.
 * Become familiar with the functions and methods available for manipulating lists
 * Become able to write programs that use lists to manage a collection of information
 * Become able to write programs that use lists and classes to structure complex data

Datastructures
Information can be stored in many ways, in order to manipulate information we need an efficient means of being able to search and modify information. You will learn how to use data structures that store a collection of information.
 * What is a data structure?

A collection can be anything from students in a course, data from an experiment, customers of a business, cards in a deck, etc.

Lists/Arrays
Beginning with a simple example, a collection of numbers, you will learn how to store a collection of numbers in a list. You don't want to create independent variables as you don't always know how many numbers will be needed, instead you will create a list that will be able to grow and handle more additional numbers.

Lists are ordered sequences of items. For example, (0,1,2,4,8,16) is a list of numbers sorted in ascending order.

>>>arrayNum = [0,1,2,4,8,16]

We can extract a particular number from the list by typing: >>>arrayNum[3] 4 >>>arrayNum[0] 0 >>>arraynNum[5] 16

Lists, also known as arrays are a sequence of items where the entire sequence is referred to by a single name (in this case arrayNum) and individual items can be selected by indexing (arrayNum[i]).

List/Array Operations
Because lists/arrays are sequences, you can use some built-in sequence operations (commands) to help you when working with lists/arrays.

Here's a summary of operations:
 * +  means concatenation
 * *  means repetition
 * [] means indexing
 * len means the length of the sequence
 * [:] means slicing
 * for(var> in : means iteration
 * in means membership check (returns a boolean value)

Slicing Operation
You can use this operation to change the value of items in a list >>>numArray = [1,2,3,4] >>>numArray[1:3] = ["Slice", "Assignment"] >>> numArray [1, 'Slice', 'Assignment', 4]

Repetition Operation
You can use this operation to create a list of identical items >>>zeroes = [0] * 50

In this example, we create a list that contains fifty 0s in it. E.g. zeroes = [0,0,0,0,0,0,0,0,0,0,0,0.....,0]

Length Operation
You can use this operation to tell you how many objects are in the list >>> arrayNum = [1,2,3,4] >>>print len(arrayNum) 4

In this example, we print the total number of items inside the arrayNum list

Iteration Operation
You can use this operation to iterate through the list one by one >>> arrayNum = [1,2,3,4] >>> for x in arrayNum print x  1 2  3   4

In this example we printed the results of the arrayNum, but we could have in this for loop created a variable to add and do a summation of all the values. >>> arrayNum = [1,2,3,4] >>> sum = 0 >>> for x in arrayNum sum = sum + x            print sum 9

Membership Operation
You can use this operation to check if a value exists in a list >>>arrayNum = [1,2,3,4] >>> 3 in arrayNum True

In this example we test to see if 3 is part of the arrayNum list, the program returns us a boolean value of "True" indicating that 3 does exist in the arrayNum list.