How-to articles, tricks, and solutions about PYTHON
You can use the .loc property of a Pandas dataframe to select rows based on a list of values.
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.
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.
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.
You can use the .count() method to count the number of occurrences of a character in a string.
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.
You can use the line_count() function from the itertools module to get the line count of a large file cheaply 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.
In Python, super() is used to call a method from a parent class.
To connect to a MySQL database in Python, you can use the mysql-connector-python library.
In order to make a class JSON serializable in Python, you need to define two methods: __init__ and to_json.
To get the ASCII value of a character in Python, you can use the built-in ord() function.
In Python, you can put a simple if-then-else statement on one line using the ternary operator, which is represented by the "?" symbol.
You can use the traceback module to catch and print the full exception traceback without halting/exiting the program.
A mixin in Python is a class that is used to add specific functionality to other classes without inheriting from them.
.pyc files are compiled bytecode files that are generated by the Python interpreter when a .py file is imported.
The 'b' character in front of a string literal indicates that the string is a bytes literal.
You can add a new column to an existing pandas DataFrame by using the assign() method or the [] notation.
In Python, the @property decorator is used to define a method as a "getter" for a class property.
In Django, null=True and blank=True are both used to specify options for fields in a model.
In Python, you can read a text file into a string variable and strip newlines using the following code:
The self parameter in Python is used to refer to the instance of an object within a class.
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.
When comparing strings using the '==' operator, the comparison is based on the actual characters in the string.