SunQuest
 
       Python
  Home arrow Python arrow Page 4 - Python 101 (part 3): A Twist In The Ta...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python 101 (part 3): A Twist In The Tail
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2001-06-13

    Table of Contents:
  • Python 101 (part 3): A Twist In The Tail
  • Here Comes A Hero
  • Making Friends And Influencing People
  • We Don't Need Another Hero
  • Looping The Loop
  • Twist And Turn
  • Within Range()
  • Just Passin' Through

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Python 101 (part 3): A Twist In The Tail - We Don't Need Another Hero


    (Page 4 of 8 )

    Unlike strings, lists are "mutable", which means that the elementscontained within a list can be changed at will. For example, any listelement can be altered simply by assigning a new value to it via its index.

    >>> superheroes ['Spiderman', 'Superman', 'Human Torch', 'Batman'] >>> superheroes[3] = "Captain America" >>> superheroes ['Spiderman', 'Superman', 'Human Torch', 'Captain America'] >>>
    You can alter more than one value at a time by using list slices.

    >>> superheroes ['Spiderman', 'Superman', 'Human Torch', 'Captain America'] >>> superheroes[0:2] = ["Incredible Hulk", "Green Lantern"] >>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America'] >>>
    The built-in append() method makes it easy to add items to a list,

    >>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America'] >>> superheroes.append("Spawn") >>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America', 'Spawn'] >>>
    while the del() method makes it just as easy to remove them.

    >>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America', 'Spawn'] >>> del superheroes[4] >>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America'] >>> del superheroes[0:2] >>> superheroes ['Human Torch', 'Captain America'] >>>
    Note that there's also a remove() method, which allows you to remove anelement by value rather than index.

    >>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America'] >>> superheroes.remove("Green Lantern") >>> superheroes.remove("Captain America") >>> superheroes ['Incredible Hulk', 'Human Torch'] >>>
    Finally, the sort() and reverse() methods allow you to rearrange thecontents of a list.

    >>> oldFlames ['Jennifer', 'Susan', 'Tina', 'Bozo The Clown'] >>> oldFlames.sort() >>> oldFlames ['Bozo The Clown', 'Jennifer', 'Susan', 'Tina'] >>> oldFlames.reverse() >>> oldFlames ['Tina', 'Susan', 'Jennifer', 'Bozo The Clown'] >>>
    A number of other list methods are also available - take a look at thePython Library Reference at http://www.python.org/doc/current/lib/lib.htmlfor more information and examples.

    Let's move on to loops.

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


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway