How-to articles, tricks, and solutions about PYTHON

What is the difference between null=True and blank=True in Django?

In Django, null=True and blank=True are both used to specify options for fields in a model.

What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?

There are many different tools that you can use to create isolated Python environments, each with their own benefits and drawbacks.

What is the easiest way to remove all packages installed by pip?

The easiest way to remove all packages installed by pip is to use the command pip freeze to get a list of all installed packages, and then pipe that list to pip uninstall -y, like this:

What is the maximum recursion depth in Python, and how to increase it?

The maximum recursion depth in Python is typically 1000, although this can vary depending on the operating system and system settings.

What is the meaning of single and double underscore before an object name?

In Python, a single underscore "_" before an object name indicates that the object is meant to be private, meaning that it should not be directly accessed or modified outside of the class that it is defined in.

What is the naming convention in Python for variable and function?

In Python, variable and function names should be lowercase, with words separated by underscores.

What is the purpose and use of **kwargs?

In Python, kwargs is a way to pass a keyworded, variable-length argument list.

What is the purpose of the `self` parameter? Why is it needed?

The self parameter in Python is used to refer to the instance of an object within a class.

What is the Python equivalent for a case/switch statement?

The Python equivalent for a case/switch statement is the if-elif-else structure.

What is the quickest way to HTTP GET in Python?

Here's a code snippet for making an HTTP GET request using the requests library in Python:

What is the use of "assert" in Python?

In Python, the assert statement is used to check if a certain condition is true, and if it is not true, raise an exception.

What should I do with "Unexpected indent" in Python?

"Unexpected indent" in Python means that the indentation level of a line of code is not what the interpreter was expecting.

What's the canonical way to check for type in Python?

In Python, you can use the isinstance function to check if an object is an instance of a particular type.

What's the difference between lists and tuples?

Lists and tuples are both used to store multiple items in a single variable, but they are different in a few key ways.

Which exception should I raise on bad/illegal argument combinations in Python?

You can raise the ValueError exception when you encounter bad or illegal argument combinations in Python.

Which version of Python do I have installed?

You can find the version of Python that you have installed by running the following command in your command prompt or terminal:

Why am I seeing "TypeError: string indices must be integers"?

The "TypeError: string indices must be integers" error is raised when you try to access an element of a DataFrame or Series using a string index instead of an integer index.

Why can't Python parse this JSON data?

Without a specific code snippet and the JSON data that is causing issues, it is difficult to determine why Python is unable to parse the data.

Why do I get the syntax error "SyntaxError: invalid syntax" in a line with perfectly valid syntax?

There are several reasons why you might see the SyntaxError: invalid syntax error in a line with apparently valid syntax.