How-to articles, tricks, and solutions about PYTHON

'pip' is not recognized as an internal or external command

This error message typically occurs when the command prompt or terminal is not able to find the pip executable.

"inconsistent use of tabs and spaces in indentation"

"Inkonsistente Verwendung von Tabs und Leerzeichen in Einr├╝ckung" is an error message that you may encounter when working with Python code.

"Large data" workflows using pandas

Here is an example of a workflow for handling large data using the pandas library:

"Least Astonishment" and the Mutable Default Argument

In Python, a default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.

"Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3

This error occurs when trying to open a file that contains escape characters (such as \) in the file path, and the escape characters are not being properly interpreted by Python.

Accessing the index in 'for' loops

To access the index in a 'for' loop in Python, you can use the built-in 'enumerate' function.

Add a new item to a dictionary in Python

To add a new item (key-value pair) to a dictionary in Python, you can use the square brackets [] or the update() method.

Adding a legend to PyPlot in Matplotlib in the simplest manner possible

Here is a simple code snippet that demonstrates how to add a legend to a PyPlot in Matplotlib:

Alphabet range in Python

In Python, you can use the string module's ascii_lowercase and ascii_uppercase constants to get the range of lowercase and uppercase letters in the ASCII character set respectively.

Append integer to beginning of list in Python

To append an integer to the beginning of a list in Python, you can use the insert() method.

Argparse optional positional arguments?

In Python, the argparse module can be used to specify optional positional arguments by setting the nargs parameter to '?'.

Asking the user for input until they give a valid response

To ask the user for input and repeat the prompt until they give a valid response, you can use a while loop and use a try-except block to catch any errors that may occur when trying to convert the user input to the desired data type.

Automatically create requirements.txt

You can use the pip freeze command to automatically generate a requirements.txt file in Python.

Behaviour of increment and decrement operators in Python

In Python, the increment operator (++) and decrement operator (--) do not exist.

Best way to convert string to bytes in Python 3?

In Python 3, you can convert a string to bytes using the bytes function.

Best way to strip punctuation from a string

One way to strip punctuation from a string is to use the str.translate() method in combination with the string.punctuation constant.

Calling a function of a module by using its name (a string)

To call a function from a module by using its name as a string, you can use the importlib module.

Cannot find module cv2 when using OpenCV

The error message "Cannot find module cv2" usually indicates that the OpenCV library is not installed on your system, or that Python is unable to find the library.

Catch multiple exceptions in one line (except block)

In Python, you can catch multiple exceptions in a single except block by separating the exceptions with a tuple.

Change column type in pandas

In pandas, you can change the data type of a column using the astype() function.

Changing one character in a string

Here is a code snippet that demonstrates how to change a single character in a string in Python:

Changing the tick frequency on the x or y axis

In matplotlib, you can change the tick frequency on the x or y axis of a plot by using the set_xticks() or set_yticks() method of the Axes class.

Check if a given key already exists in a dictionary

To check if a given key already exists in a dictionary, you can use the in keyword.

1 2 3 4 5