Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion ten small Python programs

View parsed - Show only message text

Path: g2news1.google.com!news4.google.com!proxad.net!newsfeed1.ip.tiscali.net!tiscali!transit1.news.tiscali.nl!transit.news.xs4all.nl!newsgate.cistron.nl!xs4all!transit4.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path: <showel...@yahoo.com>
X-Original-To: python-l...@python.org
Delivered-To: python-l...@bag.python.org
X-Spam-Status: OK 0.046
X-Greylist: delayed 400 seconds by postgrey-1.21 at bag.python.org;
	Sat, 26 May 2007 20:50:08 CEST
X-YMail-OSG: XcfdwzYVM1kiuLY_.H5i5dxJXDapAnGzCGj7f9lCu5r5cudKwb2Sr27ZyHtxAXgdm7zlVGQk86X6QDtiBwbV9GxuTx4Ens9cRNpyqgcQDGOi8LwLvJBBkyaGN2zmoQ--
Date: Sat, 26 May 2007 11:43:25 -0700 (PDT)
From: Steve Howell <showel...@yahoo.com>
Subject: ten small Python programs
To: python-l...@python.org
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
X-BeenThere: python-l...@python.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: General discussion list for the Python programming language
	<python-list.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/listinfo/python-list>,
	<mailto:python-list-requ...@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list>
List-Post: <mailto:python-l...@python.org>
List-Help: <mailto:python-list-requ...@python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
	<mailto:python-list-requ...@python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.8216.1180205410.32031.python-list@python.org>
Lines: 115
NNTP-Posting-Host: 194.109.207.14
X-Trace: 1180205411 news.xs4all.nl 328 [::ffff:194.109.207.14]:40346
X-Complaints-To: abuse@xs4all.nl

I've always thought that the best way to introduce new
programmers to Python is to show them small code
examples.  

When you go to the tutorial, though, you have to wade
through quite a bit of English before seeing any
Python examples.

Below is my attempt at generating ten fairly simple,
representative Python programs that expose new users
to most basic concepts, as well as the overall syntax.

It was an interesting exercise.  I constrained myself
to ten lines or less, and it was pretty easy to
incorporate loops, conditionals, print, open(), lists,
tuples, dictionaries, and imported modules.  

It was harder to show classes, and my ShoppingCart
class is nothing more than an encapsulation of a list,
which has dubious value (although it's the start of
something more useful).

Anyway, here goes:

    ------
    print 'hello world'

    ------
    for name in ('peter', 'paul', 'mary'):
        print name

    ------
    # This is a Python comment. \n is a newline
    name = raw_input('What is your name?\n')
    print 'Hi', name

    ------
    parentRabbits, babyRabbits = (1, 1)
    while babyRabbits < 100:
        print 'This generation has %d rabbits' %
babyRabbits
        parentRabbits, babyRabbits = (babyRabbits,
parentRabbits + babyRabbits)


    ------
    # def defines a method in Python
    def tax(itemCharge, taxRate = 0.05):
        return itemCharge * taxRate
    print '%.2f' % tax(11.35)
    print '%.2f' % tax(40.00, 0.08)


    ------
    import re
    for test_string in [ '555-1212', 'ILL-EGAL']:
        if re.match('\d\d\d-\d\d\d\d$', test_string):
            print test_string, 'is a valid US local
phone number'
        else:
            print test_string, 'rejected'

    ------
    prices = {'apple': 0.40, 'banana': 0.50}
    myPurchase = {
        'apple': 1,
        'banana': 6}
    groceryBill = sum([prices[fruit] *
myPurchase[fruit]
        for fruit in myPurchase])
    print 'I owe the grocer $%.2f' % groceryBill


    ------
    class ShoppingCart:
        def __init__(self): self.items = []
        def buy(self, item): self.items.append(item)
        def boughtItems(self): return self.items
    myCart = ShoppingCart()
    myCart.buy('apple')
    myCart.buy('banana')
    print myCart.boughtItems()


    ------
    # indent your Python code to put into an email
    import glob
    pythonFiles = glob.glob('*.py')
    pythonFiles.sort()
    for fn in pythonFiles:
        print '    ------'
        for line in open(fn):
            print '    ' + line.rstrip()
        print

    ------
    import time
    now = time.localtime()
    hour = now.tm_hour
    if hour < 8: print 'sleeping'
    elif hour < 9: print 'commuting'
    elif hour < 17: print 'working'
    elif hour < 18: print 'commuting'
    elif hour < 20: print 'eating'
    elif hour < 22: print 'resting'
    else: print 'sleeping'




 
____________________________________________________________________________________
Expecting? Get great news right away with email Auto-Check. 
Try the Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/newmail_tools.html 

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google