Summary

In this chapter we saw how to branch using if, and how to create multiway branches using if with elifs and, optionally, with else. We also saw how to loop using while and for, and how to generate lists of integers using range(). We learned about the dictionary methods that provide a dictionary's keys, values, and key-value pairs (items), and we took a brief look at sorting. We also had a glimpse at how to use Python's list comprehensions and generators.

We saw how to create functions using def (and with lambda). We used positional and keyword arguments, and we developed two useful functions, frange() and simplify(). We saw how Python creates functions dynamically as it reads a .py file, and how we can use this dynamism to provide similar functionality in older Python versions to that which is available in newer versions. And we saw how to use partial function application to create wrapper functions that encapsulate a function with its arguments (closures).

We also learned how to raise exceptions, how to create custom exceptions, and how to handle exceptions. We saw how to use finally to guarantee cleanup, and we discussed some of the more complex exception-handling possibilities that Python offers. We also saw that exception handling can lead to cleaner code when we have suites where multiple exceptions could occur, and how they can be used to cleanly exit a set of deeply nested loops.

Creating custom exceptions led us on to creating simple classes; classes that have no attributes (no member data) and no methods. In the next chapter we will look more formally at classes, and learn how to create them and instantiate instances of them, with any attributes and methods we wish.

Was this article helpful?

0 0
Tuberminator

Tuberminator

The main focus of this report is to show how to get involved in video marketing on the run, how to rank quickly on YouTube and Google using FREE semi-automatic tools and services. QUICKLY AND FREE. I will show methods and techniques I use to rank my videos, as well as free resources and tools to make video clips, to get backlinks and free traffic.

Get My Free Ebook


Post a comment