Python
  Home arrow Python arrow Page 3 - Python 101 (part 4): Feeding The Snake
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 
Moblin 
JMSL Numerical Library 
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 4): Feeding The Snake
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 8
    2001-06-25

    Table of Contents:
  • Python 101 (part 4): Feeding The Snake
  • Running The Lights
  • Strange Food
  • Unbreakable
  • Looking Up The Dictionary
  • Of Keys And Locks

  • 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


    Python 101 (part 4): Feeding The Snake - Strange Food


    (Page 3 of 6 )

    Tuples can be concatenated with the + operator,

    >>> CreepyCrawlies ('spiders', 'ants', 'lizards') >>> pasta ('macaroni', 'spaghetti', 'lasagne', 'fettucine') >>> strangeFood = CreepyCrawlies + pasta >>> strangeFood ('spiders', 'ants', 'lizards', 'macaroni', 'spaghetti', 'lasagne', 'fettucine') >>>
    and repeated with the * operator.

    >>> pasta * 5 ('macaroni', 'spaghetti', 'lasagne', 'fettucine', 'macaroni', 'spaghetti', 'lasagne', 'fettucine', 'macaroni', 'spaghetti', 'lasagne', 'fettucine', 'macaroni', 'spaghetti', 'lasagne', 'fettucine', 'macaroni', 'spaghetti', 'lasagne', 'fettucine') >>>
    "Slices" of a tuple can be extracted using notation similar to that used with lists - take a look:

    >>> pasta = ("macaroni", "spaghetti", "lasagne", "fettucine", "tagliatelle", "rigatoni") >>> pasta[0] 'macaroni' >>> pasta[2:] ('lasagne', 'fettucine', 'tagliatelle', 'rigatoni') >>> pasta[1:3] ('spaghetti', 'lasagne') >>> pasta[:4] ('macaroni', 'spaghetti', 'lasagne', 'fettucine') >>> pasta[-4] 'lasagne' >>> pasta[-1] 'rigatoni' >>>
    The built-in len() function can be used to calculate the number of elements in a tuple,

    >>> pasta ('macaroni', 'spaghetti', 'lasagne', 'fettucine', 'tagliatelle', 'rigatoni') >>> len(pasta) 6 >>>
    while the "in" and "not in" operators can be used to test for the presence of a particular element in a tuple. A match returns 1 (true), while a failure returns 0 (false).

    >>> pasta ('macaroni', 'spaghetti', 'lasagne', 'fettucine', 'tagliatelle', 'rigatoni') >>> "macaroni" in pasta 1 >>> "ravioli" in pasta 0 >>> "ravioli" not in pasta 1 >>> "ravio" not in pasta 1 >>> "maca" in pasta 0 >>>

    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