Академический Документы
Профессиональный Документы
Культура Документы
Dictionaries
CS 111: Computer Science for Scientists
Making a sandwich
Lets go back to our peanut butter sandwich algorithm
MakePeanutButterSandwichWith(condiment):
get bread out of pantry
take bread out of bag
MakeSandwichWith(condiment1,condiment2):
get bread out of pantry
take bread out of bag
MakeSandwichWith(condimentList):
get bread out of pantry
take bread out of bag
The Main
MakeSandwichWith( [peanut butter, marshmallow fluff, jelly, cheese] )
MakeSandwichWith( [jelly] )
MakeSandwichWith( [] )
Formally
Lists (Creating a list)
Three parts to lists: Creating a list, accessing
elements from the list, and modifying the list.
Creating a list:
We create a list by using the [ ] operator val = [20,40,20,3]
Each value in the list is separated by a comma. x = 55
bop = [33,x,-12]
Lists could be empty or contain a single element. noList = []
Its good practice to make elements in the list single = [55]
the same type
Not required in Python, but will save you pain.
Accessing Elements
Two main ways to access elements: all elements or single
element.
All elements:
The best way to access all elements is to use a for loop.
Which has the syntax
myList = [20,30,-10,50]
print("Starting")
for val in myList:
print(myList)
print("ending")
thing = [-33,12,55]
for a in thing:
if a > 0:
print("I'm positive")
else:
print("Don't be negative")
Question
How would you sum up all values in a list?
How would you average all values in a list?
Single Value
You can access a single value at a time by using the [] as well
The syntax is as follows
myList[index]
Index 0 1 2 3
Value 20 30 -10 50
Example
myList = [20,30,-10,50]
x = myList[0]
print(myList[2])
print(x)
myList[1] = 500
print(myList[1])
Out of Range
What happens if you have a list of 4 elements and you try and
access the 5th element in the list (i.e., index 4).
myList = [20,30,-10,50]
x = myList[4]
It CRASHES!
This will look like it works, when you write your code
But it will give you an array out of bounds error when you
run your program.
Negative Value?
What happens if you tried to access the value at index -1?
It gives you the last value in the array.
Negative values go from back to front.
myList = [20,30,-10,50]
x = myList[-1]
Changing the list
Changing the value of an element is easy: just use the []
operator
hello = [20,30,40,50]
hello[0] = 55
Bad For Loop, BAD!
You cant change values using the for loop though.
When you try and change the element you change the
variable holding the value NOT the value in the list
hello = [20,30,40,50]
for element in hello:
element+=20
print(hello)
hello = [20,30,40,50]
for element in hello:
print(hello)
data = [20,30,40]
data.append(50)
Remove Element
We can remove an individual element from the array by using
the remove command like so
data = [20,30,40,50,90]
data.remove(30)
Multidimensional Arrays
Spreadsheets
We can use arrays to make spreadsheets as well!
row/col 0 1 2
0 20 200 70
1 30 300 80
2 40 400 90
3 50 500 100
4 60 600 110
What is an array?
Its a list of lists.
Spreadsheets
We can use arrays to make spreadsheets as well!
row/col 0 1 2
0 20 200 70
1 30 300 80
2 40 400 90
3 50 500 100
4 60 600 110
What is an array?
Its a list of lists.
row/col 0 1 2
Making it 0
1
20
30
200
300
70
80
2 40 400 90
3 50 500 100
4 60 600 110
row0 = [20,200,70]
row1 = [30,300,80]
row2 = [40,400,90]
row3 = [50,500,100]
row4 = [60,600,110]
total = [row0,row1,row2,row3,row4]
print(total[3][1])
row/col 0 1 2
Making it (Shorthand) 0
1
20
30
200
300
70
80
2 40 400 90
3 50 500 100
4 60 600 110
total = [[20,200,70],
[30,300,80],
[40,400,90],
[50,500,100],
[60,600,110]]
print(total[3][1])
You dont need to write it over multiple lines, but its a lot
easier to read. Whats important is the double [[ and ]]
For Loop
You can use for loops to go through the multidimensional
total = [[20,200,70],
[30,300,80],
[40,400,90],
[50,500,100],
[60,600,110]]
each row takes on the row in total (and its a list) so we need
to go through them one at a time.
Tuples
Data Structures and Tuples
A data structure is any construct designed to hold multiple
pieces of information.
Lists are the the most widely used type of data structures.
Tuples are another data structure that is similar to lists, but
they cannot change their size after they have been created.
Make a Tuple
We make a tuples using ()
point = (4,5)