More Practice Python

More Practice Python

We’re going to do some more practice involving functions and variables to make sure you know them well. This exercise should be straightforward for you to type in, break down, and understand.

However, this exercise is a little different. You won’t be running it. Instead you will import it into Python and run the functions yourself.

1 def break_words(stuff):
2 """This function will break up words for us."""
3 words = stuff.split(' ')
4 return words
5
6 def sort_words(words):
7 """Sorts the words."""
8 return sorted(words)
9
10 def print_first_word(words):
11 """Prints the first word after popping it off."""
12 word = words.pop(0)
13 print word
14
15 def print_last_word(words):
16 """Prints the last word after popping it off."""
17 word = words.pop(- 1)
18 print word
19
20 def sort_sentence(sentence):
21 """Takes in a full sentence and returns the sorted words."""
22 words = break_words(sentence)
23 return sort_words(words)
24
25 def print_first_and_last(sentence):
26 """Prints the first and last words of the sentence."""
27 words = break_words(sentence)
28 print_first_word(words)
29 print_last_word(words)
30
31 def print_first_and_last_sorted(sentence):
32 """Sorts the words then prints the first and last one."""
33 words = sort_sentence(sentence)
34 print_first_word(words)
35 print_last_word(words)

Output

>>> import ex25
 >>> sentence = "All good things come to those who wait."
 >>> words = ex25.break_words(sentence)
 >>> words
 ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']
 >>> sorted_words = ex25.sort_words(words)
 >>> sorted_words
 ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
 >>> ex25.print_first_word(words)
 All
 >>> ex25.print_last_word(words)
 wait.
 >>> wrods
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 NameError: name 'wrods' is not defined
 >>> words
 ['good', 'things', 'come', 'to', 'those', 'who']
 >>> ex25.print_first_word(sorted_words)
 All
 >>> ex25.print_last_word(sorted_words)
 who
 >>> sorted_words
 ['come', 'good', 'things', 'those', 'to', 'wait.']
 >>> sorted_words = ex25.sort_sentence(sentence)
 >>> sorted_words
 ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']
 >>> ex25.print_first_and_last(sentence)
 All
 wait.
 >>> ex25.print_first_and_last_sorted(sentence)
 All
 who

First, run this like normal with python to find any errors you have made. Once you have found all the errors you can and fixed them, you will then want to follow this..

  • Line 5. You import your Python file, just like other imports you have done. Notice you do not need to put the .py at the end to import it. When you do this, you make a module that has all your functions in it to use.
  • Line 6. You made a sentence to work with.
  • Line 7. You use the module and call your first function break_words. The . (dot, period) symbol is how you tell Python, “Hey, inside there’s a function called break_words and I want to run it.”
  • Line 8. We just type words, and Python will print out what’s in that variable (line 9). It looks weird, but this is a list that you will learn about later.
  • Lines 10– 11. We do the same thing with words to get a sorted sentence.
  • Lines 13–16. We use print_first_word and print_last_word to get the first and last word printed out.
  • Line 17. This is interesting. I made a mistake and typed the words variable as wrods so Python gave me an error on lines 18–20.
  • Lines 21–22. We print the modified words list. Notice that since we printed the first and last one, those words are now missing

Study Drills

  1. Take the remaining lines of the WYSS output and fi gure out what they are doing. Make sure you understand how you are running your functions in the module.
  2. Try doing this: help and also help(break_words). Notice how you get help for your module and how the help is those odd “”” strings you put after each function in ? Those special strings are called documentation comments and we’ll be seeing more of them.

I get a None printed out for some of the functions.

You probably have a function that is missing the return at the end. Go backward through the file like I taught you and confirm that every line is right.

How can the words.pop(0) be changing the words variable then?

That’s a complicated question, but in this case words is a list, and because of that you can give it commands and it’ll retain the results of those commands. This is similar to how files and many other things worked when you were working with f.readline().

When should I print versus return in a function?

You need to understand that print is only for printing to the screen and that you can actually both print and return a value. When you understand this, then you’ll see that the question is kind of pointless. You use print when you want to print. You use return when you want to return.