293 | | == Lesson 4: Libraries == |
294 | | Now that you seen the basics of the Python language, and have created your own functions. Its time to see what else is out there. It turns out there are A LOT of libraries out there for python that do pretty much everything you want, from advance machine intelligence, to 3D graphics, to yes controlling robots. These libraries were created by others and are offered for free, and others may be offered at a cost. |
295 | | |
296 | | You can find these libraries by googling for with the term "<subject> python library". For example "spell check python library". This turns up [https://pythonhosted.org/pyenchant/ PyEnchant] which describes how to use and access the library. There are probably others. |
| 286 | == Lesson 4: Packages == |
| 287 | Now that you seen the basics of the Python language, and have created your own functions. Its time to see what else is out there. It turns out there are A LOT of packages out there that contain custom functions for Python that do pretty much anything you'd want, from advanced machine intelligence, to 3D graphics, to yes, controlling robots. These packages were created by others and are offered for free, and others may be offered at a cost. |
| 288 | |
| 289 | You can find these libraries by googling for with the term "<subject> python library". For example "spell check python library". This turns up [https://pythonhosted.org/pyenchant/ | PyEnchant] which describes how to use and access the library. There are probably others. You can also go to the [[https://pypi.python.org/pypi|Python Package Index]]. What ever you do, make sure you use the correct version of the library for the version of Python you're using (in our case, Python 3) |
300 | | {{ |
301 | | #!python |
302 | | import enchant |
303 | | d=enchant.Dict("en_US") # Selects the US english dictionary |
304 | | bad=check("Helo") # Spell checks the given string, not correctly spelled, returns False |
305 | | good=check("Hello") # Correctly spelled word returns True |
306 | | }} |
307 | | |
308 | | It would be a enormous effort to create your own spell check library, so this saves a lot of time. We'll be using this when we program robots because a lot of work has already been done. |
309 | | |
310 | | |
| 293 | {{{ |
| 294 | #!python |
| 295 | import enchant |
| 296 | d=enchant.Dict("en_US") # Selects the US english dictionary |
| 297 | bad=check("Helo") # Spell checks the given string, not correctly spelled, returns False |
| 298 | good=check("Hello") # Correctly spelled word returns True |
| 299 | }}} |
| 300 | |
| 301 | It would be a enormous effort to create your own spell check library, so reusing one saves a lot of time. We'll be using this when we program robots because a lot of the work we need to do to communicate with the parts of a robot has already been done. And we want to spend time making robots do things, not figuring out how to communicate with then. |
| 302 | |
| 303 | Unfortunately, you can't include external libraries in your code in our web-based code runner and run them, so you'll have to use your imagination to see how this particular example works. You'll use them a lot in the intermediate training as the year goes on. |
| 304 | |
| 305 | = Final Exam = |
| 306 | [[image:https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwjS5oD-gY3WAhUs94MKHbpwB4sQjRwIBw&url=http%3A%2F%2Fphi2010.blogspot.com%2F&psig=AFQjCNG8rEtQbDhBSL7ojOYD9kOuL-d0ew&ust=1504665503635530]] |
| 307 | Create a program that prints a random fortune cookie saying, drawing randomly from a different lists of nouns and verb phrases. Make it sound suitable cryptic and wise. |
| 308 | The program should prompt the user for the number of sayings they want output, validate that it is indeed a number, and print out that many. |
| 309 | Use functions and control structures to make the code manageable. |
| 310 | Its best if you work in pairs for this exercise. If you need help, refer to this lesson, the [[https://docs.python.org/3/index.html | Official Python 3 Documentation]], and don't hesitate to ask the mentors. |
| 311 | |
| 312 | |
| 313 | = What's Next = |
| 314 | Well that concludes the Software Introduction. We welcome your feedback. Software takes a lot of work and study to write well, and this is just a taste. For those of you who select the Software team, we'll begin intermediate training with the Raspberry Pi, and show you how to write programs that use external libraries to communcation with the outside world through motors, sensors and the like. |