How-to articles, tricks, and solutions about PYTHON

Use a list of values to select rows from a Pandas dataframe

You can use the .loc property of a Pandas dataframe to select rows based on a list of values.

Extract file name from path, no matter what the os/path format

You can use the os.path.basename() function to extract the file name from a file path, regardless of the operating system or path format.

Does Django scale?

Django is a web framework that is designed to handle high traffic and can scale to meet the demands of a large number of users.

How to overcome "datetime.datetime not JSON serializable"?

One way to overcome "datetime.datetime not JSON serializable" in Python is to use the json.dumps() method with the default argument, default=str, which converts the datetime object to a string before serializing.

Count the number of occurrences of a character in a string

You can use the .count() method to count the number of occurrences of a character in a string.

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 to get line count of a large file cheaply in Python?

You can use the line_count() function from the itertools module to get the line count of a large file cheaply in Python.

How do I print an exception in Python?

You can print an exception in Python by using the print() function and passing the exc variable which is the default variable name for an exception.

How does Python's super() work with multiple inheritance?

In Python, super() is used to call a method from a parent class.

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 to make a class JSON serializable

In order to make a class JSON serializable in Python, you need to define two methods: __init__ and to_json.

How to get the ASCII value of a character

To get the ASCII value of a character in Python, you can use the built-in ord() function.

Putting a simple if-then-else statement on one line

In Python, you can put a simple if-then-else statement on one line using the ternary operator, which is represented by the "?" symbol.

How to catch and print the full exception traceback without halting/exiting the program?

You can use the traceback module to catch and print the full exception traceback without halting/exiting the program.

What is a mixin and why is it useful?

A mixin in Python is a class that is used to add specific functionality to other classes without inheriting from them.

If Python is interpreted, what are .pyc files?

.pyc files are compiled bytecode files that are generated by the Python interpreter when a .py file is imported.

What does the 'b' character do in front of a string literal?

The 'b' character in front of a string literal indicates that the string is a bytes literal.

How to add a new column to an existing DataFrame?

You can add a new column to an existing pandas DataFrame by using the assign() method or the [] notation.

How does the @property decorator work in Python?

In Python, the @property decorator is used to define a method as a "getter" for a class property.

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.

How to read a text file into a string variable and strip newlines?

In Python, you can read a text file into a string variable and strip newlines using the following code:

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.

How do I check if a variable exists?

In Python, you can check if a variable exists by using the globals() or locals() function to check if the variable is in the global or local namespace, respectively.

Why does comparing strings using either '==' or 'is' sometimes produce a different result?

When comparing strings using the '==' operator, the comparison is based on the actual characters in the string.