Python Functions and Built in Data Types - Slides | CSSE 490, Study notes of Computer Science

Material Type: Notes; Class: Pattern Recog Using Hid Markov; Subject: Computer Sci & Software Engr; University: Rose-Hulman Institute of Technology; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-71j
koofers-user-71j 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PYTHON FUNCTIONS AND
BUILT-IN DATA TYPES
Curt Clifton
Rose-Hulman Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Python Functions and Built in Data Types - Slides | CSSE 490 and more Study notes Computer Science in PDF only on Docsity!

PYTHON FUNCTIONS AND

BUILT-IN DATA TYPES

Curt Clifton

Rose-Hulman Institute of Technology

ANNOUNCEMENTS

Homework 1 due now

Homework 2 due start of class Thursday

Read through it soon!

I suspect you might have questions about the Haar

wavelet problem

Please give me lots of feedback as we go

SAMPLE ANIMATIONS

WHO WANTS TO SHARE?

SOME COOL

“PYTHONIC” FEATURES

Subscripting and slicing

lists (and strings)

Formal parameters

Default arguments

Keyword arguments

Docstrings

Functions on lists

Multiple assignment

Dictionaries

Q

DEFAULT ARGUMENTS

def complain (complaint = 'This is a dead parrot' ): print "Customer:" , complaint complain() complain( "If you hadn't nailed 'im to the perch, he'd be pushin' up daisies!" ) def mutable_weirdness (n, l=[]): l.append(n) print l mutable_weirdness( 4 , [ 1 , 2 , 3 ]) mutable_weirdness( 1 ) mutable_weirdness( 2 )

Default

argument

value

Line

continuation

Q

KEYWORD ARGUMENTS,

DOCSTRINGS

When a function has several parameters with default

values, you can use keyword arguments to just give a

few values

def converse (complaint = 'Bereft of life, he rests in piece' , response = "He's pinnin' for the fjords" ): """Conducts a short conversation. Conducts a short conversation between a complaining customer and a shopkeeper. """ print "Customer:" , complaint print "Shopkeeper:" , response converse(response= "There, he moved!" ) help(converse) print converse.doc

Keyword argument

Docstring

Docstring uses Q4–

LIST FUNCTIONS

Some list functions:

append(x)· insert(i, x)· remove(x)· pop(i=-1)· index(x)·

count(x)· sort()· reverse()

Lists as stacks:

Use append(x) to push items and pop() to pop them

Lists as queues:

Use append(x) to enqueue items and pop(0) to

dequeue them

MULTIPLE ASSIGNMENT

Swap?

Most languages:

temp = x

x = y

y = temp

Python:

x, y = y, x

UNIT TESTING IN PYTHON

Multiple approaches

Easiest is probably the doctest module plus

conditional execution

DOCTEST EXAMPLE

import doctest

The following function is from the Python Tutorial

def average (values): _"""Computes the arithmetic mean of a list of numbers. >>> print average([1])

>>> print average([1,2])

>>> print average([1,2,3])

>>> print average([1,-2,3])

"""_ return sum(values, 0.0) / len(values) if name == 'main' :

doctest.testmod() Conditional Execution

Test cases and

expected results