Python
  Home arrow Python arrow Page 10 - Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2001-07-16


    Table of Contents:
  • Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons
  • Cheating The Taxman
  • Talking Movies
  • Call Me Sometime
  • Return To Me
  • Tall, Dark And Handsome
  • Arguing Your Case
  • Enter The Funky Chameleon
  • Flavour Of The Month
  • Hip To Be Square

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 6): Hedgehogs, Pythons And Funky Chameleons - Hip To Be Square
    ( Page 10 of 10 )

    Python comes with a couple of built-in functions to help you work with your own user-defined functions. And yes, I realize that that sentence didn't make all that much sense...

    Perhaps an example will make it clearer. Consider the Python apply() function. The sole raison d'etre of apply() is to run another function with a set of arguments. Consequently, if I had a simple function named whoAmI(),

    >>> def whoAmI(name): ... print "Your name is", name ... >>>
    the following statements would be equivalent:

    >>> apply(whoAmI, ["Batman, Dark Knight"]) Your name is Batman, Dark Knight >>> whoAmI("Batman, Dark Knight") Your name is Batman, Dark Knight >>>
    This might seem pointless at first glance; however, apply() can come in handy if functions or function arguments are generated dynamically by your program, and you are unsure of their values.

    Note that the second argument to apply() must always be a sequence - list, tuple et al - containing a list of function arguments.

    The map() function runs a function on every element of a list, and places the results in another list. Consider the following function, which squares the argument passed to it.


    >>> def hipToBeSquare(num): ... return num**2 ... >>>
    And here's a list of numbers

    >>> numList = [2, 5, 9, 11, 13, 20] >>>
    Look what happens when I put them both together with map().

    >>> resultList = map(hipToBeSquare, numList) >>> resultList [4, 25, 81, 121, 169, 400] >>>
    Similar to map() is the filter() function; it runs a function on a list, and creates a subset of those items which returned true. So, if I had a function which checked whether or not a number was prime (perhaps you remember this from a previous article?) and a mixed list of prime and non-prime numbers, filter() would come in handy to separate the two.

    #!/usr/bin/python # returns 1 if prime, 0 if non-prime def isPrime(num): for count in range(2,num): if num % count == 0: return 0 break else: return 1 # list of numbers numList = [4, 3, 11, 18, 200, 171, 67, 5] # filter and print result list print filter(isPrime, numList)
    And the output is

    [3, 11, 67, 5]
    And that just about concludes this article. This time, you've taken a big step towards better software design, by learning how to abstract out parts of your Python code into reusable functions. You've learned how to add flexibility to your functions by allowing them to accept different arguments, and how to obtain return values from them. Finally, you've learned how Python treats variables inside and outside functions, and tried out three of the most common built-in helper functions available in Python.

    In the next article, I'll be expanding on this theme by discussing Python modules, constructs which add a whole new level of reusability to this powerful programming language. See you then!

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

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek