How-to articles, tricks, and solutions about PYTHON

In Python, how do I determine if an object is iterable?

In Python, an object is considered iterable if it has an __iter__() method defined or if it has a __getitem__() method with defined indices (i.e., it can be indexed, like a list or a string).

IndentationError: unindent does not match any outer indentation level

This error occurs when there is a mismatch in the indentation level of a block of code in Python.

Installing specific package version with pip

To install a specific version of a package with pip, you can use the pip install command followed by the package name and the desired version number.

Is arr.__len__() the preferred way to get the length of an array in Python?

No, in python the built-in len() function is the preferred way to get the length of an array, list or any other iterable object.

Is it possible to break a long line to multiple lines in Python?

Yes, it is possible to break a long line of code into multiple lines in Python.

Is there a "not equal" operator in Python?

Yes, in Python there is an operator for not equal (!=) .

Is there a list of Pytz Timezones?

Yes, you can use the all_timezones attribute of the pytz library to get a list of all the timezones that it supports.

Is there a simple way to delete a list element by value?

Yes, you can use the remove() method to delete a list element by its value.

Iterating over dictionaries using 'for' loops

To iterate over a dictionary using a for loop, you can use the .items() method to get a list of the dictionary's keys and values, and then iterate over that list.

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The error message "JSONDecodeError: Expecting value: line 1 column 1 (char 0)" typically occurs when you are trying to parse a string as JSON, but the string is not in a valid JSON format.

Limiting floats to two decimal points

To limit a float to two decimal points, you can use the round() function.

List attributes of an object

In Python, you can use the built-in function dir() to list the attributes of an object.

List comprehension vs map

List comprehension and map() are both used to create new lists in Python, but they are used in slightly different ways.

List comprehension vs. lambda + filter

List comprehension and the combination of lambda functions and the filter() function in Python are both used to filter a list and return a new list with only the elements that satisfy a certain condition.

List of lists changes reflected across sublists unexpectedly

In Python, when you create a list of lists and modify one of the sublists, the change is reflected in all other sublists as well because they are all pointing to the same object in memory.

Manually raising (throwing) an exception in Python

In Python, you can raise an exception using the raise statement.

Matplotlib make tick labels font size smaller

Here is a code snippet that shows how to make the tick labels font size smaller in Matplotlib:

Maximum and Minimum values for ints

In most modern programming languages, the maximum value for an int data type is implementation-specific, but is typically in the range of 2^31 - 1 to 2^63 - 1, and the minimum value is usually -2^31 or -2^63.

Meaning of @classmethod and @staticmethod for beginner

In Python, @classmethod is a decorator that is used to define a method as a class method.

mkdir -p functionality in Python

The os.makedirs() function in Python can be used to create a directory with the -p option, which will create the entire directory path, including any missing parent directories.

Most efficient way to map function over numpy array

There are several ways to apply a function to every element of a numpy array, and the most efficient method will depend on the size and shape of the array, as well as the complexity of the function.