1 / 20

Programming Seminar 2009

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

vivian
Download Presentation

Programming Seminar 2009

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Seminar 2009 Night 1

  2. 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.

  3. For loops and if statements, revisited >>> str = input() >>> words = 1 >>> for c in str: >>> if c == ‘ ’: >>> words = words + 1 What does this do?

  4. 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}

  5. 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.

  6. 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

  7. You tell me • First, • Then, we • Last,

  8. 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…

  9. (Your calculator program, written collaboratively) >>> >>> >>> >>> >>> >>> >>> >>>

  10. 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

  11. 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

  12. 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])

  13. 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

  14. 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

  15. 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.”)

  16. 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.”]

  17. 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

  18. 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()

  19. 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

  20. Next Time • Project Euler • Classes? • Not the kind you’ll be attending in a few days • Tuples • Fancier variations on simple constructs

More Related