Chapter 5

Can the if clause work with multiple (more than two) logical branches?

Yes! For that, you can use an additional keyword—elif. This way, you can have an unlimited number of logical branches, though it's recommended to use no more than four to five at a time.

What is the difference between for and while loops?

for loops are explicitly finite—they run for every element in a given iterable (although you can pass an infinite iterable if you need to). They are also meant to use that iterable. 

while loops are explicitly infinite until certain criteria are met—so they are good if you don't know the number of iterations it would require to meet them (or want an explicitly infinite loop, which would be stopped from within the loop itself).

How can I loop through multiple (two or more) arrays of the same length? Or of different lengths?

The best way to do that is by using the zip function, which combines those arrays into one array with elements of the same order combined together. If the length is different, zip will cut off the tails of the longer arrays.

Why do we need exceptions? How can I catch one?

Exceptions are the way Python (or any other language) halts execution and returns a corresponding message or error code. If you anticipate that the code will fail, you can catch the exception and define an alternative execution by using try/except/finally statements. This will ensure that the execution of the script as a whole won't be halted. That being said, make sure not to mute exceptions you don't want to – this might cause way more problems in the future!

What is the difference between finally and except?

In the try/except/finally clause, you have to use the first keyword, try, but can choose between using exceptfinally, or both of them. The difference is simple—code within finally is used under any circumstances—even if the exception is raised and not caught before the code halts and exits. Thus, the finally clause is invaluable if we need to properly close a certain channel—a database connection, file, or anything else.

When should the with clause be used?

The with clause is, to some degree, syntactic sugar for the try/finally clause. It is used to work with objects that have __enter__ and __exit__ methods and executes them before and after the code within the clause. Once the __enter__ method is run successfully, an execution of __exit__ is guaranteed, even if the code within fails to run—similar to how finally behaves. All file objects and many database connections use these methods, as it is a convenient and expressive method to ensure the connection is closed once work has been done. That being said, you can use this keyword in other contexts as well, whenever a similar behavior is desired.

How can I use the with clause on a custom object?

To use the with clause on a custom object, all you need is to provide two methods for that object—__enter__ and __exit__. All this statement does is run the former before the code within, and the latter afterward, under any circumstances (even if an exception is raised).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.39.16