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

Talking Movies - 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...

  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


Let's take a simple example, which demonstrates how to define a function and call it from different places within your Python program:

#!/usr/bin/python # define a function def greatMovie(): print "Star Wars" # main program begins here print "Question: which is the greatest movie of all time?" # call the function greatMovie() # ask another question print "Question: which movie introduced the world to Luke Skywalker, Yoda and Darth Vader?" # call the function greatMovie()
Now run it - you should see something like this:

Question: which is the greatest movie of all time? Star Wars Question: which movie introduced the world to Luke Skywalker, Yoda and Darth Vader? Star Wars
Let's take this line by line. The first thing I've done is define a new function with the "def" keyword; this keyword is followed by the name of the function (and optionally, one or more arguments). All the program code attached to the function is then indented within this block - this program code could contain loops, conditional statements, or calls to other functions. In the example above, my function has been named "greatMovie", and only contains a call to Python's print() function.

Here's the typical format for a function:

def function_name(optional function arguments): 'comment' statement 1... statement 2... . . . statement n...
The comment line above allows the developer to optionally include a description of the function; if I wanted to be really verbose, I could modify the function definition in the example above to read

# define a function def greatMovie(): 'this prints the name of my favourite movie' print "Star Wars"

>>> 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: