How-to articles, tricks, and solutions about PYTHON

How do I concatenate two lists in Python?

There are a few ways to concatenate lists in Python.

How do I connect to a MySQL Database in Python?

To connect to a MySQL database in Python, you can use the mysql-connector-python library.

How do I convert a datetime to date?

You can use the date() function from the datetime module in Python to convert a datetime object to just a date object.

How do I convert all strings in a list of lists to integers?

Here is an example of how you might convert all strings in a list of lists to integers:

How do I count the NaN values in a column in pandas DataFrame?

You can use the isna() function to create a boolean mask of the NaN values in a column, and then use the sum() function to count the number of True values in the mask.

How do I count the occurrences of a list item?

There are a few ways you can count the occurrences of a list item in Python:

How do I create a constant in Python?

In Python, a constant is typically defined as a variable that is assigned a value that should not be modified later on in the program.

How do I create a list with numbers between two values?

Here is an example of how you might create a list of numbers between two values in python:

How do I create multiline comments in Python?

In Python, you can create a multiline comment using triple quotes (either single or double) at the beginning and end of the comment.

How do I detect whether a variable is a function?

In Python, you can use the built-in callable() function to check if a variable is a function.

How do I determine the size of an object in Python?

You can use the sys.getsizeof() function to determine the size of an object in bytes.

How do I do a case-insensitive string comparison?

You can convert both strings to lowercase or uppercase (using the lower() or upper() method) before doing the comparison.

How do I do a not equal in Django queryset filtering?

You can use the exclude() method to filter out records where a certain field is not equal to a certain value.

How do I execute a program or call a system command?

There are several ways to execute a program or call a system command in Python.

How do I expand the output display to see more columns of a Pandas DataFrame?

You can expand the output display of a Pandas DataFrame by setting the option 'display.max_columns' in pandas.

How do I find out my PYTHONPATH using Python?

You can use the sys module in Python to access the sys.path variable, which contains a list of directories that Python looks in for modules to import.

How do I find the duplicates in a list and create another list with them?

In Python, one way to find duplicates in a list and create another list with them is to use a for loop and an if statement.

How do I find the location of my Python site-packages directory?

You can use the site module to find the location of your Python site-packages directory.

How do I generate all permutations of a list?

You can use the itertools.permutations() function to generate all permutations of a list in Python.

How do I get a list of locally installed Python modules?

You can use the pip module to get a list of locally installed Python modules.

How do I get a substring of a string in Python?

To get a substring of a string in Python, you can use the string[start:end] notation.

How do I get file creation and modification date/times?

There are a few ways to get the file creation and modification date/times, depending on the programming language and operating system you are using.

How do I get the current time in milliseconds in Python?

You can use the time module in Python to get the current time in milliseconds.

How do I get the current time?

To get the current time in Python, you can use the datetime module from the standard library.