Playing Around With def

Venturing into Chapter 6 of Hetland, I got my feet (slightly) wet with def as an exercise (in Python, def lets you define your own functions). I tried to go it alone where I could, but did reference the examples to help me with flow.

Hello World! was the most obvious of trivial guinea pigs, so meet the latest and greatest (now in hi-def!) hello.v1-2-0.py:

 

Hello World! v1.2.0

Hello World! v1.2.0

 

#!/usr/bin/env python

# hello.py v1.2.0
# Hello World!
#
# Changelog:
# v1.2.0 (10/11/2010) + Now with def magic
# v1.1.2 (09/19/2010) + Now ensures user enters a name
# v1.1.1 (09/11/2010) + Added GPLv3 license text & changelog to comments
#                     + Added user-visible program & (c) info
#                     + Re-commented program, because we were all newbs once
# v1.1 (08/08/2010) + Added "Press <enter>" prompt for terminal execution
#                   - Deleted unnecessary comments
# v1.0 (07/20/2010) - Initial release (My first program!)
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# variables & functions

name = ""

def hello(name):
    return "Hello, " + name + "!"

# (c) info

print "Hello World! v1.2.0"
print "Copyright 2010 by Benjamin Braithwaite"
print

# get user's name and print a personalized greeting

while not name.strip():
    name = raw_input("What is your name? ")
print
print hello(name)
print

# successful exit dialog

raw_input("Press <enter> to exit.")

You can download (with the click of a button) hello.v1-2-0.py from my Dropbox.

I also really liked the example of a Fibonacci number generator, so I made fibber.v1-0-0.py:

 

Fibber v1.0.0

Fibber v1.0.0

 

#!/usr/bin/env python

# fibber.py v1.0.0
# Fibber
#
# Changelog:
# v1.0.0 (10/11/2010) - Initial release
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# variables & functions

num = ""

def fib(num):
    fibseed = [0, 1]
    for i in range(num - 2): # -2 compensates for the two seed numbers
        fibseed.append(fibseed[-2] + fibseed [-1])
    return fibseed

# (c) info

print "Fibber v1.0.0"
print "Copyright 2010 by Benjamin Braithwaite"
print

# asks for size of sequence to calculate and prints as dictionary

while not num:
    num = input("How many Fibonacci numbers do you want to calculate? ")
print
print fib(num)
print

# successful exit dialog

raw_input("Press <enter> to exit.")

You can download fibber.v1-0-0.py from my Dropbox as well.

Basically these were both pure exercises, since def really isn’t entirely necessary for such really tiny programs (it rather adds to their “complexity” in my opinion), but they were valuable ones. I think def will make many things much easier for me in the future. Sorry for the sparser-than-usual commentary, but I’ve got to rush home now — cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s