Learn Python in One Hour
eBook - ePub

Learn Python in One Hour

Programming by Example

  1. English
  2. ePUB (mobile friendly)
  3. Available on iOS & Android
eBook - ePub

Learn Python in One Hour

Programming by Example

Book details
Book preview
Table of contents
Citations

About This Book

You're already a smart person, you don't need a 1000+ page book to get you started on the web's fastest growing programming platform. Instead, Learn Python in One Hour delivers on the promise of code literacy while saving your most precious commodity - time itself. Volkman's innovative programming-by-example approach means you focus on usage, not mindless detail. Based on the author's sold-out live seminars, you'll see Python's flexible coding technique in action as we refactor from script to procedural to object-oriented during actual problem solving.
In a twelve-lesson progression, you'll be exposed to this and more:

  • Basic file input and output operations, incuding exceptions
  • Using functions to compute and return multiple values
  • Basic elements of a class definition and how to call methods
  • Lists, dictionaries, sets, and other collections
  • Iteration through collections, files, sorted sets
  • Searching strings with regular expressions ( regex )
  • Client and server programs for REST methods
  • Using threads in Python for multiple tasks
  • CGI-BIN programming for simple HTML Forms processing
  • Six most common Python pitfalls

Take the One Hour challenge and see if you too can pick up 90% of syntax and semantics in less time than you probably spend commuting each day. About the Author Victor R. Volkman graduated cum laude from Michigan Technological University with a BS in Computer Science in 1986. Since then, he has written for numerous publications, including The C Gazette, C++ Users Journal, Windows Developers Journal, and many others. He has taught college-level programming courses at Washtenaw Community College and has served on its Computer Information Science (CIS) Faculty Advisory Board for more than a decade. Volkman says Python helped him "rediscover the joy of programming again." From Modern Software Press

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Learn Python in One Hour by Victor R. Volkman in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Python. We have over one million books available in our catalogue for you to explore.

Information

Lesson Six – A Dictionary of Lists
We’ve developed our word frequency count as far as it’s going to go, so the next type of analysis we’ll add is that of an “index.” By index, I mean the type of reference you find in the back of an old-fashioned paper book, which shows each word and the number of the page on which it appears. In our case, we’ll show each word and the line number it appears on:
1:# Exercise #6: dictionary of lists
2:
3:import collections
4:
5:def openFile():
6:fh = None
7:try:
8:fh = open("c:/temp/input.txt", "r", encoding="ascii")
9:except OSError as e:
10:print("error %d reading file %s" % (e.errno, e.filename))
11:quit()
12:return fh
13:
14:def processFile(fh):
15:numWords = 0
16:wordFreq = collections.Counter()
17:wordIndex = collections.defaultdict(list)
18:lineNum = 0
19:for line in fh:
20:lineNum += 1
21:words = line.split()
22:numWords += len(words)
23:for word in words:
24:wordFreq[word] += 1
25:wordIndex[word]. append(str(lineNum))
26:fh.close()
27:return (numWords, wordFreq, wordIndex)
28:
29:def printReport(stats):
30:(numWords, wordFreq, wordIndex) = stats
31:print("I got %d words" % numWords)
32:for key in sorted(wordFreq.keys()):
33:print("key %-12s found %2d times" % (key, wordFreq[key]))
34:for key in sorted(wordIndex.keys()):
35:print("key %-12s found on lines: %s" % (key,
36:",".join(wordIndex[key])))
37:
38:def main():
39:fh = openFile()
40:stats = processFile(fh)
41:printReport(stats)
42:
43:if __name__ == "__main__":
44:main()
Okay, this code is getting kind of long, so we’ll simply focus in on the boldfaced code, which is new since the previous lesson. On line #17, we create wordIndex as a defaultdict where each entry is a list. Although we have never previously declared the data types of what’s inside a container, doing so is necessary to harness the power of defaultdict. Those of you familiar with Java and C++ are used to having to script out elaborate declarations of every type of data structure in advance. Normally with a Python dictionary, you need to test for the presence of an entry before you can increment or append an item. With defaultdict, you get a blank entry automatically generated of the type you specified (list in this case). Specifically, an empty list [ ] in this case. Technically, list is a factory function that supplies missing values.
On line #25, we’re appending the current line number in the file to the value in wordIndex which is specified by the value of word.
On lines #35-36, we’re dumping out the content of the list of lines as a comma-separated list via the join() string method. This is a handy way to produce any value-separated string from a list of strings. Because it only works on strings, we had to cast the numbers to strings back on line #25 as shown below:
25:wordIndex[word].append(str(lineNum))
Also on lines #35-36, notice that we have split the code between two lines. Normally Python requires an entire statement to fit on one line. Because we had a “live” open parenthesis on line #35, we can carry over to line #36 and keep typi...

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright
  4. Contents
  5. Why Python?
  6. Why One Hour?
  7. Where to Get Python?
  8. Lessons Overview and Goals:
  9. Lesson One – Opening Arguments
  10. Lesson Two – Using Your Words
  11. Lesson Three – When Things Go Wrong
  12. Lesson Four – What’s the Frequency, Kenneth?
  13. Lesson Five – Fun with Functions
  14. Lesson Six – A Dictionary of Lists
  15. Lesson Seven – A Touch of Class
  16. Python Pitfalls
  17. Where Do We Go From Here?
  18. Recommended Resources
  19. About the Author
  20. Notes