Is “from matplotlib import pyplot as plt” == “import matplotlib.pyplot as plt”?

Even though they are equivalent, I think there is a pretty good argument that the second form import matplotlib.pyplot as plt is objectively more readable: It is generally customary to use import matplotlib.pyplot as plt and suggested in the matplotlib documentation (see http://matplotlib.org/users/pyplot_tutorial.html etc…) so this will be more familiar to most readers. import matplotlib.pyplot … Read more

How to redirect ‘print’ output to a file?

The most obvious way to do this would be to print to a file object: However, redirecting stdout also works for me. It is probably fine for a one-off script such as this: Since Python 3.4 there’s a simple context manager available to do this in the standard library: Redirecting externally from the shell itself … Read more

What is a “method” in Python?

It’s a function which is a member of a class: Simple as that! (There are also some alternative kinds of method, allowing you to control the relationship between the class and the function. But I’m guessing from your question that you’re not asking about that, but rather just the basics.)

Invalid character in identifier

I am working on the letter distribution problem from HP code wars 2012. I keep getting an error message that says “invalid character in identifier”. What does this mean and how can it be fixed? Here is the page with the information.

What does %s mean in a python format string?

It is a string formatting syntax (which it borrows from C). Please see “PyFormat”: Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the %s placeholder. Edit: Here is a really simple example: The %s token allows me to … Read more