chapter2

//__Control structures __// 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 sawhow 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 exception 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. *To know more about control structures in python,you can visit the following link: http://python.about.com/od/tutorial1/ss/begpyctrl.htm   *The examples we study in this chapter are:  [|simplified.py[|frange.py]] frange.py [|answers.py]