This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Python Programming 101

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Data Types

Go back to: General discussion

Please read the Data Types course document and complete the excercises. Post your answers to Pastie or Github and then add a link to them below. If you have questions about a specific topic or excercise, post it here as well.

Extra Credit

You are designing an inventory and point-of-sale system for a local producers' market. Aspects of the system include:

  1. farmer name and booth number
  2. market slogan
  3. produce name and price
  4. names of growing seasons ('spring', 'sumer', 'fall', 'winter')
  5. cities where the farmers and participants are located (no state data necessary)

Q: How can each aspect be represented in Python data types? Give an example for each.

Tyler Cipriani's picture
Tyler Cipriani
Tue, 2011-05-10 20:47

Posted my assignments up on my github at http://www.tylercipriani.com/python101/ At the end of chapter 10 it asks you to compare your results with the letter frequency page on wikipedia - so for a little extra fun I decided to use BeautifulSoup to parse that Wikipedia page and actually compare the results - I describe the full process on that github page posted above.

1. Dict with Tuple key - ex namebooth[(John, Farmer)] => Booth 10
2. String - ex 'Your Mom's Favorite Python Book'
3. Dict - ex namePrice['goldenGooseFruit'] => '$500'
4. List - ex growingSeasons = ['spring','summer','fall',winter']
5. Dict with Tuple Key - ex farmersParticipants[(John, Farmer)] => Wichita

1 person liked this
Johan Mares's picture
Johan Mares
Wed, 2011-05-18 19:33

Man, you are working us hard; 4 chapters in a week!
Exercises chapter 6: http://www.pastie.org/1922634
To get zfill to work I had to use repr() instead of str() to use zfill() on a number (exercise 6.6). What is difference between repr and str?

PS: I accidentally studied chapter 7 as well.

1 person liked this
Brylie Oxley's picture
Brylie Oxley
Thu, 2011-05-19 22:47

Sorry :-) We can stretch this task out over two weeks, although it seems like you are ahead of the game.

Johan Mares's picture
Johan Mares
Wed, 2011-05-18 19:30

Exercises chapter 8: http://www.pastie.org/1922678

In exercise 3 I used an 'or' instead of an 'and' to combine both conditions.

1 person liked this
Johan Mares's picture
Johan Mares
Thu, 2011-05-19 19:23

Exercises chapter 9: http://www.pastie.org/1927674

I am pretty amazed that Python can handle a statement like
domain = words[1].split('@')[1]
which I used in exercise 9.5. You take the second element of a list, apply a string function to it which results in a list and returns the second element, all in one go. Whether it is easy to read for humans is something else. What do you think?

1 person liked this
Brylie Oxley's picture
Brylie Oxley
Thu, 2011-05-19 22:50

Clever indeed :)

It is slightly hard to read, as you point out. How do you think it stands up to the 'Zen of Python'?

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

1 person liked this
Johan Mares's picture
Johan Mares
Fri, 2011-05-20 12:44

Definitely not zen, but cool.

Christian Walch's picture
Christian Walch
Thu, 2011-05-19 21:46

Homework for for Data Types:

http://pastie.org/1928301

1 person liked this
Jimmy Moore's picture
Jimmy Moore
Fri, 2011-05-20 03:00

Took a little while to set up a github account:

Set up last week's homework on github.

Chapter 6: https://github.com/jwmnatl/jwmnatl.github.com/blob/master/python101/chap...

I still have Chapters 8, 9 & 10 to go

Johan Mares's picture
Johan Mares
Fri, 2011-05-20 12:43

Exercises chapter 10: http://pastie.org/1930991

Problem with foreign languages were the diactricis (accents, umlaut, ...). How do you solve this?

Jimmy Moore's picture
Jimmy Moore
Wed, 2011-05-25 06:32

Brylie, I am a little stuck with Ex. 10.1. I am using the top 10 most common words example from the chapter, and the output is a list of 27 emails with "cwen@ipui.edu 5" at the end rather than just the one email and count returned:

my code:
file = raw_input('Enter a file name: ')
try:
fhand = open(file)
except:
print 'File cannot be opened: ', file
exit()
emails = dict()
for line in fhand:
words = line.split()
# print 'Debug', words
if len(words) < 2 or words[0] != 'From' : continue
emails[words[1]] = emails.get(words[1], 0) + 1
lst = []
for key, val in emails.items():
lst.append((val,key))
lst.sort(reverse=True)

for val, key in lst[:1]:
print key, val

Tyler Cipriani's picture
Tyler Cipriani
Wed, 2011-05-25 16:28

I put this code in my editor and it seemed to work just fine. When called on mbox-short.txt the program runs as follows:

Enter a file name: mbox-short.txt
cwen@iupui.edu 5

My suggestion is to play with your indentation levels as they're not indicated by posting your code here. The furthest indented line I used with your code is the lst.append((val,key)) which is indented twice and it is the only line indented twice - this generates the expected output. Put your code up on gist or pastie if you're still having trouble.

Jimmy Moore's picture
Jimmy Moore
Sat, 2011-05-28 22:28

Thanks, Tyler. I know the indentations did not show but since I was posting at 12:30 am, I didn't really care. In any event, you nailed it. My second for loop was in the first for loop. Totally makes sense now...not at 12:30 in the morning, but now it does. I moved the second for loop to the left and the code works. Thanks again, Tyler!

Chapter 10: https://github.com/jwmnatl/jwmnatl.github.com/blob/master/python101/chap...

Sally Lloyd's picture
Sally Lloyd
Sun, 2011-05-29 14:14

Exercise 8.1 | http://www.pastie.org/1988496
Exercise 8.4 | http://www.pastie.org/1988583

Not sure my method for 8.4 is quite correct, but get the same outcome.
Have had some unexpected things crop up these last couple of weeks, so haven't been able to devote as much time and reading to this task as I would like to. Hopefully post the rest of the exercises soon.

Tyler Cipriani's picture
Tyler Cipriani
Tue, 2011-05-31 16:13

Your method for 8.4 works fine. The more pythonian method for achieving this outcome, and a method that'll serve you well going forward, is to use a 'continue' to continue with the next iteration of a loop. So if you find that for which you're searching in an array (i.e. 'if x is in array') you can just skip to the next word in the array rather than delete the word and re-append the word to the results array. I've made this adjustment to your code in this gist: https://gist.github.com/bc1f7fe1f16f4522677a for context. Hopefully any of this made sense - I'm not so sure after having re-read it. Let me know if I can clarify.

mark lawson's picture
mark lawson
Mon, 2011-05-30 07:28

posted chap 6 exercises

http://pastie.org/1991662

mark lawson's picture
mark lawson
Mon, 2011-05-30 07:32

oops - in ex6.5, should have typed the print(found) so it looked like this:
print(float(found)) sorry..

Mark Hutchison's picture
Mark Hutchison
Wed, 2011-06-08 07:50

Finished Data Types chapters: https://github.com/brotherhutch/python101

Extra Credit:
1) Dictionary with Tuple key: {('Beanstalk', 'Jack'): 'B1'}
2) String: 'Steve Jobs makes \'em; we fix \'em.'
3) Dictionary: {'Garbanzo Beans': 4.95}
4) List: ('spring', 'summer', 'fall', 'winter')
5) Dictionary with Tuple key: {('Beanstalk', 'Jack'): 'Abbotsford'}