Transcript Seminar 1

Programming Seminar 2009
Night 1
Review: Last Time
• We covered variables, for loops, functions,
modules, and how programming isn’t math
– Actually, we just scratched the surface for most of
these
• This time, we’ll be focusing more on functions
– And functionality
– With any luck, we will work in Python 3.
For loops and if statements, revisited
>>> str = input()
>>> words = 1
>>> for c in str:
>>> if c == ‘ ’:
>>>
words = words + 1
What does this do?
Defining a function
>>> def cube(n):
>>> return n*n*n
• Remember, return tells Python what value
the function should “evaluate” to.
• Similar to math functions defined in piecewise
fashion: abs(x) = {x if x >= 0; -x if x < 0}
Functions take action!
• Calling a function says: “please do this”
– E.g. input() asks for user input, waits for it to be
entered, then returns it as the “value” of the
function.
• Most functions are created to make doing a
repetitive task easy.
Okay, so what should these
“functions” do?
• Let’s take our example of a calculator program
– Inputs two numbers and an operation, the
outputs the result
• Break up this idea into smaller pieces
– Which can be accomplished in, say, a couple
statements
You tell me
• First,
• Then, we
• Last,
We can turn this into a high-level,
function-based overview
•
•
•
•
•
in = read_input()
op = pick_operation(in)
a,b = pick_numbers(in)
result = carry_out_operation(a,b)
print(result)
• Then we just have to implement each part
– Correctly…
(Your calculator program, written
collaboratively)
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
What Python can already do for you
• We now (formally) introduce the data type
• Data Type: A combination of information and
functionality given a specific name.
• Now, for a pile of examples
Standard Python Data Types
• str – a string
• list – a list of items
• tuple – a list of items (with a few more
restrictions)
– All entries have to be of the same type
– Length cannot change
• set – a collection of unique elements
• dictionaries – a mapping between one
set of data and another
Python Strings
>>> s = “This is a string”
• Ask for the 5th character
>>> print(s[5])
• Ask for the 3rd through 8th characters
>>> print(s[3:8])
• Ask for the 2nd character from the end
>>> print(s[-2])
Strings, continued
• Ask for all characters after (and including) the
4th
>>> print(s[4:])
• All of these are called slices
• Can pull the same neat tricks with lists
– Plus a few more
Lists
>>> l = [4,8,15,16,23,42]
• Ask for the last three elements
>>> print(l[-3:])
• Remove the middle two elements
>>> del l[2:-2]
>>> print(l)
• Assign the first element to 108
>>> l[0] = 108
Strings and lists can work together
>>> print(“Please enter a list of names (first and
last).”
>>> l = [] # empty list
>>> s = “First Last”
>>> while s: # so long as s is not empty
>>> s = input(“Name please: ”)
>>> l.append(s)
>>> print(“You entered ” + len(l) + “ items.”)
One more interaction
• Remember that program from last time which counted
the number of words in a sentence?
>>> s = input(“Please enter a sentence.”)
>>> l = s.split(“ ”)
>>> print(len(l))
• s.split(t) gives a list of all substrings of s broken around
t
• “This is a sentence.”.split(“ ”) gives [“This” “is”, “a”,
“sentence.”]
• “This is a sentence.”.split(“s”) gives [“Thi ”, “ i”, “ a ”,
“entence.”]
Your turn, again!
• Write a program that asks for a bunch of emails
as input.
– Output a list of all the domains (whatever is after the
@)
• Given a list of numbers, find the list of numbers
that are relatively prime to the first one.
• Given a word, print its Scrabble score
– aeioulnrst are 1 point, dg are 2, bcmp are 3, fhvwy are
4, k is 5, jx are 8, and qz are 10
On Files
• Repeating input is annoying.
– You have to type everything every time
• We can put input into a file.
>>> file = open(“README.txt”)
>>> for line in file:
>>> print(line)
>>> file.close()
Files, continued
• What’s up with open() and close()?
– Python has to ask the operating system for help.
• Details in CIS 240 (more details in CIS 380) or by
demand
• Useful for processing information
– Read in from a file, output to a file
Next Time
• Project Euler
• Classes?
– Not the kind you’ll be attending in a few days
• Tuples
• Fancier variations on simple constructs