Michael Hall wrote:
I am still not understanding what it is I am being asked to do. What is the
differance between my_list = [] an my_list[ ] because when I use my_list[]
I get an error. Not sure what I am doing wrong.

In Python, square brackets [ ] are used for two related but different purposes. The first is for creating lists:

L = [1, 2, 4, 8]

creates a list containing four numbers and assigns it to the name L.

When using square brackets to create a new list, you don't need anything inside the brackets. If you try it, you just get an empty list:

x = []  # a list with no items
x = [42]  # a list with one item
x = [42, 23]  # a list with two items


The second is for item access. If you want to refer to the entire list L, you just say "L":

print L  # prints the entire list [1, 2, 4, 8]

But if you want to refer to only a single item of L, you need to use this 
syntax:

print L[2]  # prints "4"

(Aside: why does it print 4? Remember that Python starts counting from 0, not 1, so the 0th item is 1, the 1st item is 2, the 2nd item is 4, and the 3rd item is 8. While it might seem a bit weird at first, it actually has some good advantages.)


You can also refer to a portion of a list by taking a slice:

print L[1:3]  # prints [2, 4]

It's called a slice, because you think of it as cutting slices *between* items, like this illustration: |1|2|4|8|

The slice 1:3 cuts *before* the 1st and 3rd items. Using / instead of | for the places you cut: |1/2|4/8|

so you get [2, 4] as the slice.

When using [] for item access, there *must* be something inside the brackets:

print L[2]  # okay
print L[]  # not okay, which item did you want?

L[2] = 42  # puts 42 into item 2 of the list
L[] = 42  # an error


Does that help you?



I am asking for help writing it. I am new to programming. Lost alot of
brain matter.

If you have concrete questions, please ask.




--
Steven
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to