Python 101 (part 5): Snake Oil For The Soul - Pop() Goes The Weasel
(Page 8 of 8 )
The pop() function allows you to remove an item from the end of a list,
>>> list
['red', 'green', 'blue', 'red', 'yellow', 'blue', 'black', 'white', 'green']
>>> len(list)
9
>>> list.pop()
'green'
>>> len(list)
8
>>>
and the str() function converts a list into a printable string.
>>> list
['red', 'green', 'blue', 'red', 'yellow', 'blue', 'black', 'white', 'green']
>>> str(list)
"['red', 'green', 'blue', 'red', 'yellow', 'blue', 'black', 'white',
'green']"
>>>
Finally, the max() and min() methods return the largest and smallest itemwithin a string, list or tuple.
>>> # list
>>> friends = ["Rachel", "Ross", "Joey", "Phoebe", "Monica", "Chandler"]
>>> max(friends)
'Ross'
>>> min(friends)
'Chandler'
>>> # string
>>> alphabet = "abcdefghijklmnopqrstuvwxyz"
>>> min(alphabet)
'a'
>>> max(alphabet)
'z'
>>> # tuple
>>> num = (1,2,3)
>>> max(num)
3
>>> min(num)
1
>>>
And that's about it for the moment. In this article, you learnt how to opena connection between your Python program and a file residing on yourfilesystem. You then learnt how to use built-in Python methods to read datafrom and to that file, together with some additional methods and propertiesthat might come in useful. Finally, you've also added to your store ofPython knowledge with a look at some of the new features available inPython 2.x.
Over the next few article, we'll be elevating our view a little, with alook at the Python framework for software abstractions like functions,classes and modules. Specifically, in the next article, I'll be showing youhow to build your own functions, and also discussing return values andarguments in the context of a Python program. Make sure you don't miss thatone!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |