Professional Documents
Culture Documents
Python Tutorial #2
Python Tutorial #2
.
Python: classes, modules and packages
.
.. .
.
Netsoc
Stephen Shaw
2010
Schedule
This week - Classes, modules, packages, regular expressions,
documentation, the PyPI
Week 3 - Miscellaneous fun with scientific computing
These slides: http://www.netsoc.tcd.ie/~stesh/python/2.pdf
c l a s s Programmer ( student ) :
''' Represents a programmer '''
def __init__ ( self , name , age , course = CS ) :
super ( student , self ) . __init__ ( name , age , course ) :
self . lives_with_parents = True
1 Obligatory plug
Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 7 / 25
Modules
Only execute the main() function if the module is being run as a script
nato = ''' alpha bravo charlie delta echo foxtrot golf hotel india �
juliet
kilo lima mike november oscar papa quebec romeo sierra tango
uniform vector whiskey x - ray yankee zulu '''. split ()
def main () :
p r i n t ( transcribe ( input ( ' utterance : ') ) )
. . . Who cares?
Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 11 / 25
Regular expressions for programmers
import re
import os
/* *
* Generate a random list of ShoppingItems from the given list ,
* of the given size .
*
* @param items
* @param desiredSize
* @return A ShoppingList object containing the given number of �
items
* @throws InsufficientItemsException if the given size is less
* than the total number of items available in the given list
*/
protected ShoppingList getShoppingList ( List < ShoppingItem > items , �
int
desiredSize ) ;
No.
Arguments :
n -- a non - negative integer
Exceptions :
ValueError -- if n is negative
'''
i f n == 0:
return 1
else :
return n * factorial ( n - 1)
>>> 7 / 5
1
>>> from __future__ import division
>>> 7 / 5
1.3999999999999999