How to use filter, map, and reduce in Python 3

You can read about the changes in What’s New In Python 3.0. You should read it thoroughly when you move from 2.x to 3.x since a lot has been changed. The whole answer here are quotes from the documentation. Views And Iterators Instead Of Lists Some well-known APIs no longer return lists: […] map() and filter() return iterators. If … Read more

How to use the pass statement

Suppose you are designing a new class with some methods that you don’t want to implement, yet. If you were to leave out the pass, the code wouldn’t run. You would then get an: To summarize, the pass statement does nothing particular, but it can act as a placeholder, as demonstrated here.

How do I copy a file in Python?

shutil has many methods you can use. One of which is: Copy the contents of the file named src to a file named dst. Both src and dst need to be the entire filename of the files, including path. The destination location must be writable; otherwise, an IOError exception will be raised. If dst already exists, it will be replaced. Special files such as character or block … Read more

Manually raising (throwing) an exception in Python

How do I manually throw/raise an exception in Python? Use the most specific Exception constructor that semantically fits your issue. Be specific in your message, e.g.: Don’t raise generic exceptions Avoid raising a generic Exception. To catch it, you’ll have to catch all other more specific exceptions that subclass it. Problem 1: Hiding bugs For example: … Read more

How do I copy a file in Python?

shutil has many methods you can use. One of which is: Copy the contents of the file named src to a file named dst. Both src and dst need to be the entire filename of the files, including path. The destination location must be writable; otherwise, an IOError exception will be raised. If dst already exists, it will be replaced. Special files such as character or block … Read more