Home arrow Python arrow Page 8 - Python 101 (part 5): Snake Oil For The Soul

Pop() Goes The Weasel - Python

Now that you know how to create and use Python's tuples, listsand dictionaries, it's time to get your hands dirty. In this article, findout how to read from, and write to, files on the filesystem with built-inPython methods, and also take a look at some of the new features availablein Python 2.x.

  1. Python 101 (part 5): Snake Oil For The Soul
  2. Air In A Bottle
  3. Treading The Right Path
  4. Standing In Line
  5. Learning To Write
  6. Desperately Seeking Python
  7. String Theory
  8. Pop() Goes The Weasel
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
July 09, 2001

print this article


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!

>>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: