This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Data Types



Python recognizes and differentiates between several kinds of data. Lets look at the following and some of the things we can do with them:

 

Extra Credit

You are designing an inventory and point-of-sale system for a local produce 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 yet)

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

 

You can also have a quick look at: http://pytuts.blogspot.com/2011/10/hi.html

Task Discussion


  • Fer said:

    on Nov. 6, 2013, 6:43 a.m.
  • Wouter Tebbens said:

    I posted the exercises here: http://pastebin.com/Z9S2HFbj

    on March 15, 2013, 1:19 p.m.
  • drediamond said:

    on Nov. 25, 2012, 2:04 a.m.

    cstsupport said:

    Greetings from Ireland. Thank you for contacting me. I am are currently out of the office and will have some access to my email. I will return your email and attend to queries as soon as I can. Warm Regards, Rosetta Talley
    on Nov. 25, 2012, 2:10 a.m. in reply to drediamond
  • saravanan said:

    Extra: Answers

    farmer_count={'muthuswamy':1,'ponnuswamy':2,'sakthi':3} #dictionary

    market_slogan="Best Price Best Service " #String

    pro=[('rice',1),('wheat',10),('olive',20)] #list

    growing_seaons=('spring', 'sumer', 'fall', 'winter') # tuple

    location=['salem','trichi','madurai','theni'] #list

    on Aug. 30, 2012, 1:22 p.m.
  • saravanan said:

    Chapter 10:http://pastebin.com/iYwEAWyz completed

    on Aug. 30, 2012, 12:58 p.m.
  • saravanan said:

    on Aug. 25, 2012, 2 p.m.
  • saravanan said:

    on Aug. 23, 2012, 3 p.m.
  • saravanan said:

    on Aug. 21, 2012, 2:01 p.m.
  • Rob said:

    Chapters 9 and 10:  http://dpaste.com/780966/

    on Aug. 3, 2012, 5:05 p.m.
  • Rob said:

    Chapter 8: http://dpaste.com/777731/

    on July 29, 2012, 8:59 p.m.
  • Rob said:

    Chapter 6:  http://dpaste.com/777224/

    on July 28, 2012, 11:15 p.m.
  • ionut.vlasin said:

    on July 9, 2012, 10:50 a.m.
  • pannix said:

    Exercises for chapter 5 - datatypes

    Chapter 6: http://pastebin.com/DLNABpFJ

    Chapter 8: http://pastebin.com/PsVhFQ84

    Chapter 9: http://pastebin.com/mxmSqpVy

    Chapter 10: http://pastebin.com/NM446TAa

    on May 9, 2012, 11:28 a.m.

    pannix said:

    Bonus question:

    1. dictionary {'John Doe': 1234, 'Jane Doe': 4567, 'Bob Redneck': 7890}
    2. string 'Fresh Veggies'
    3. dictionary {'potatoes': 3.59, 'apples': 5.23, 'rice': 3.66}
    4. tuple ('Spring', 'Summer', 'Autumn', 'Winter')
    5. dictionary {'John Doe': 'Fort Worth', 'Jane Doe': 'Corpus Christi'}
    on May 9, 2012, 11:49 a.m. in reply to pannix
  • E.Mosso said:

    how do you bypass the raw_input in Python v3.0 and higher? That version of Python using IDLE doesn't recognize raw_input. .The exercises I keep coming across assme that it does, and are using the 2.0 versions.. How do you input the commands in order to get the same results? I do not know. I also realized that you just leave out the print command when you want to print something in Python v.3 .. took me a sec to figure that out as well.

    on April 15, 2012, 12:49 a.m.

    Asier Iturralde Sarasola said:

    In Python >3.0 the old raw_input() has been renamed to input() and print is now a function. Some examples:

    Old: print "The answer is", 2*2
    New: print("The answer is", 2*2)
    
    Old: print x,           # Trailing comma suppresses newline
    New: print(x, end=" ")  # Appends a space instead of a newline
    
    Old: print              # Prints a newline
    New: print()            # You must call the function!
    
    Old: print >>sys.stderr, "fatal error"
    New: print("fatal error", file=sys.stderr)
    
    Old: print (x, y)       # prints repr((x, y))
    New: print((x, y))      # Not the same as print(x, y)!
    on April 15, 2012, 7:29 a.m. in reply to E.Mosso
  • gnuisance said:

    on March 10, 2012, 3:15 p.m.
  • dvdjaco said:

    I need to go now, will post some comments tomorrow!

    Extra credit:

    d = {"Joe Farmer": 55}
    s = "The Slogan"
    d =  {"produce name": 5.55}
    seasons = ('spring', 'sumer', 'fall', 'winter')
    d = {"participant name": "city"}
    

    on Feb. 18, 2012, 6:30 p.m.

    dvdjaco said:

    (I have a bunch of questions / comments regarding these exercises, I'll post them as replies to my answers for clarity)

    8.2 Performance: does the order of these 3 lines have a potential impact in the performance of the program? I mean, which of the 3 expressions is harder to evaluate? They should be written in increasing order of complexity, so that lines that don't pass the easier test are not further analysed.

    I have written them so that we first compare 2 strings, then evaluate the len() of a list and then search for a character with list.find(), but that's just a guess.

    if words[0] != 'From' : continue
    if len(words) < 3 : continue # drop lines with less than 3 words
    if words[1].find('@') == -1 : continue # discard random (body) lines starting with 'From'
    
    on Feb. 21, 2012, 6:27 a.m. in reply to dvdjaco

    dvdjaco said:

    8.3 Rewrite the guardian code in the above example without two if statements.
    Instead use a compound logical expression using the AND logical operator with a single
    if statement.

    The text should say "using the OR  logical operator", not "AND": if any of these expressions is true we should "continue"
     

    on Feb. 21, 2012, 6:34 a.m. in reply to dvdjaco

    dvdjaco said:

    9.2 Performance again. How does (1) compare to (2) in this sense?  Does (2) represent a performance improvement?

    (1)
    for c in s:
        if c not in d:
            d[c] = 1
        else:
            d[c] = d[c] + 1
    
    (2)
    for c in s: d[c] = d.get(c,0) + 1
    
    
    
    

    on Feb. 21, 2012, 6:48 a.m. in reply to dvdjaco
  • Ken Doman said:

    Chapter 6 answers here.

    Chapter 8 answers here.

    Chapter 9 answers here.

    Chapter 10 answers here.

    Extra Credit:

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

    1. farmer name and booth number
      • Farmer's name: string - "Farmer Brown"
      • Booth number: int - 42
    2. market slogan
      • string - "Fresh produce sold here"
    3. produce name and price
      • dictionary - {"corn": 1.23, "tomatoes": 0.69}
    4. names of growing seasons ('spring', 'sumer', 'fall', 'winter')
      • a tuple of strings - ('spring', 'sumer', 'fall', 'winter')
    5. cities where the farmers and participants are located (no state data necessary yet)
      • a list of strings - ["Kansas City", "Omaha", "Chicago"]
    on Jan. 17, 2012, 1:40 p.m.

    dvdjaco said:

    Hi Ken,

    your pastebin links give an "Unknown Paste ID!" message.

    on Feb. 21, 2012, 7:04 a.m. in reply to Ken Doman
  • dean said:

    My answers:

    http://goo.gl/iYb9m

    on Oct. 10, 2011, 1:19 p.m.
  • Mars83 said:

    d = {"farmer name": 1337}
    s = "Your advertising could stand here!"
    d = {"produce name": [0.00, '€']}
    tp = ('spring', 'summer', 'fall', 'winter')
    d = {"name": "city"}
    
    on Oct. 9, 2011, 11:38 a.m.