Try It Out Popping Elements from a List

You need to tell pop which element it is acting on. If you tell it to work on element 0, it will pop the first item in its list, while passing pop a parameter of 1 will tell it to use the item at position 1 (the second element in the list), and so on. The element pop acts on is the same number that you'd use to access the list's elements using square brackets:

>>> todays_temperatures = [23, 32, 33, 31]

>>> todays_temperatures.append(29)

>>> todays_temperatures

[23, 32, 33, 31, 29]

>>> morning = todays_temperatures.pop(0)

>>> print "This mornings temperature was %.02f"

% morning

This mornings temperature was 23.00

>>> late_morning = todays_temperatures.pop(0)

>>> print "Todays late morning temperature was

%.02f" % late_morning

Todays late morning temperature was 32.00

>>> noon = todays_temperatures.pop(0)

>>> print "Todays noon temperature was %.02f" %

noon

Todays noon temperature was 33.00

>>> todays_temperatures

[31, 29]

Was this article helpful?

0 0

Post a comment