How-to articles, tricks, and solutions about PYTHON

How do I sort a dictionary by key?

In Python, dictionaries are unordered collections of key-value pairs.

Hidden features of Python

There are many hidden features of Python, some of which are not well-known even among experienced Python programmers.

How do I check which version of Python is running my script?

There are a few ways to check which version of Python is running your script.

Null object in Python

In Python, the "null" object is called None.

Selenium using Python - Geckodriver executable needs to be in PATH

If you are using Selenium with the Firefox web browser and you see the error message "Geckodriver executable needs to be in PATH," it means that the Selenium Python library cannot find the geckodriver executable on your system.

How can I get the concatenation of two lists in Python without modifying either one?

There are a few different ways to concatenate two lists in Python without modifying either one.

Python: finding an element in a list

To find an element in a list, you can use the in keyword.

Convert floats to ints in Pandas?

To convert floats to integers in Pandas, you can use the astype() function.

How to use multiprocessing pool.map with multiple arguments

To use the multiprocessing.pool.map() function with multiple arguments, you will need to use the starmap() method instead.

How to crop an image in OpenCV using Python

You can use the cv2.imread() function to read an image into memory, and then use the cv2.rectangle() function to draw a rectangle around the region you want to crop.

TypeError: list indices must be integers or slices, not str

This error is usually caused when you try to access an element in a list using a string as the index, rather than an integer.

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.

Extracting specific selected columns to new DataFrame as a copy

To create a new DataFrame with a subset of columns from an existing DataFrame, you can use the pandas library.

How can I make one python file run another?

To run one Python file from another, you can use the exec function or the subprocess module.

How can I find where Python is installed on Windows?

To find where Python is installed on Windows, you can follow these steps:

How to calculate number of days between two given dates

To calculate the number of days between two dates, you can use the timedelta class from the datetime module.

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.

How to serve static files in Flask

To serve static files in Flask, you will need to use the send_static_file method in your route function.

Relative imports in Python 3

Relative imports in Python 3 allow you to import modules or functions from other packages within your package hierarchy.

SyntaxError: unexpected EOF while parsing

The SyntaxError: unexpected EOF while parsing error is raised when the Python interpreter reaches the end of the file (EOF) while it is still parsing the file, and it is unable to complete the parsing process because of an error in the syntax of the code.

What is setup.py?

setup.py is a Python script used to build and install Python packages.

Import a module from a relative path

To import a module from a relative path in Python, you can use the importlib.import_module() function from the importlib module.

How to activate virtualenv in Linux?

To activate a virtual environment in Linux, you can use the source command and the path to the activate script that is located in the virtual environment's bin directory.