Best way to return multiple values from a function? [closed]

Named tuples were added in 2.6 for this purpose. Also see os.stat for a similar builtin example. In recent versions of Python 3 (3.6+, I think), the new typing library got the NamedTuple class to make named tuples easier to create and more powerful. Inheriting from typing.NamedTuple lets you use docstrings, default values, and type annotations. Example (From the docs):

Accessing the index in ‘for’ loops?

Using an additional state variable, such as an index variable (which you would normally use in languages such as C or PHP), is considered non-pythonic. The better option is to use the built-in function enumerate(), available in both Python 2 and 3:

What is the necessity of plt.figure() in matplotlib?

The purpose of using plt.figure() is to create a figure object. The whole figure is regarded as the figure object. It is necessary to explicitly use plt.figure() when we want to tweak the size of the figure and when we want to add multiple Axes objects in a single figure. Parts of a Figure

Pip freeze vs. pip list

When you are using a virtualenv, you can specify a requirements.txt file to install all the dependencies. A typical usage: The packages need to be in a specific format for pip to understand, which is That is the “requirements format”. Here, django==1.4.2 implies install django version 1.4.2 (even though the latest is 1.6.x). If you … Read more

ImportError: No module named matplotlib.pyplot

You have two pythons installed on your machine, one is the standard python that comes with Mac OSX and the second is the one you installed with ports (this is the one that has matplotlib installed in its library, the one that comes with macosx does not). Is the standard mac python and since it … Read more

Converting string into datetime

datetime.strptime is the main routine for parsing strings into datetimes. It can handle all sorts of formats, with the format determined by a format string you give it: The resulting datetime object is timezone-naive. Links: Python documentation for strptime: Python 2, Python 3 Python documentation for strptime/strftime format strings: Python 2, Python 3 strftime.org is … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)