Home arrow Python arrow Page 6 - Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons

Tall, Dark And Handsome - Python

This week, Python 101 discusses how to abstract out parts of yourPython code into reusable functions, add flexibility to them by allowingthem to accept different arguments, and make them return specific values.Also included: a discussion of variable scope and functions to help youwrite your own functions. Confused? All is explained within...

TABLE OF CONTENTS:
  1. Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons
  2. Cheating The Taxman
  3. Talking Movies
  4. Call Me Sometime
  5. Return To Me
  6. Tall, Dark And Handsome
  7. Arguing Your Case
  8. Enter The Funky Chameleon
  9. Flavour Of The Month
  10. Hip To Be Square
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
July 16, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Return values from a function can even be substituted for variables anywhere in a program.


#!/usr/bin/python # define a function def marryMe(): if tall == 1 and dark == 1 and handsome == 1: return "Yes!" else: return "I'm sorry, I just don't feel the same way about you." # set some variables tall = 1 dark = 1 handsome = 1 # pop the question print "Will you marry me?" # use return value in a function call print(marryMe())
Notice how the function is able to read the values of variables defined outside the function; this is related to variable scope in Python, and I plan to discuss it in detail a little further down.

Return values need not be numbers or strings alone - a function can just as easily return a list, dictionary or tuple, as demonstrated in the following example:

Python 1.5.2 (#1, Aug 25 2000, 09:33:37) [GCC 2.96 20000731 (experimental)] on linux-i386 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> def returnList(): ... return ["Huey", "Dewey", "Louie"] ... >>> def returnTuple(): ... return "macaroni", "spaghetti", "lasagne", "fettucine" ... >>> def returnDictionary(): ... return {"new hope":"Luke", "teacher":"Yoda", "bad guy":"Darth"} ... >>> print type(returnList()) >>> print type(returnTuple()) >>> print type(returnDictionary()) >>>


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

blog comments powered by Disqus
   

PYTHON ARTICLES

- 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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: