How-to articles, tricks, and solutions about PYTHON

Manually raising (throwing) an exception in Python

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

How do I change the size of figures drawn with Matplotlib?

To change the size of figures drawn with Matplotlib in Python, you can use the figure() function and set the figsize argument.

Understanding Python super() with __init__() methods

The super() function is a way to refer to the parent class and its attributes.

How can I delete a file or folder in Python?

To delete a file or folder in Python, you can use the os module and call the os.remove() function to delete a file, or the shutil.rmtree() function to delete a folder and all its contents.

How do I make a time delay?

There are several ways to add a delay to your Python program.

How do I pass a variable by reference?

In Python, you can pass a variable by reference by using the & operator.

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

In Python, the double star (**) is used to denote an "unpacking" operator, which allows you to unpack a dictionary or other iterable data type into keyword arguments in a function call.

How do I select rows from a DataFrame based on column values?

You can use the DataFrame.loc method to select rows from a DataFrame based on column values.

How do I concatenate two lists in Python?

There are a few ways to concatenate lists in Python.

"Least Astonishment" and the Mutable Default Argument

In Python, a default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.

How can I add new keys to a dictionary?

To add a new key-value pair to a dictionary in Python, you can use the update() method or simply assign a value to a new key using square brackets [].

How do I sort a dictionary by value?

You can use the sorted() function and pass it the dictionary, along with the key parameter set to a lambda function that returns the value from the dictionary.

What is __init__.py for?

__init__.py is a special Python file that is used to indicate that the directory it is present in is a Python package.

How to copy files

To copy a file in Python, you can use the built-in "shutil" module.

What is the difference between __str__ and __repr__?

__str__ and __repr__ are two special methods in Python classes.

Convert bytes to a string

To convert a byte object into a string, you can use the decode() method.

Catch multiple exceptions in one line (except block)

In Python, you can catch multiple exceptions in a single except block by separating the exceptions with a tuple.

How do I get the current time?

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

How to iterate over rows in a DataFrame in Pandas

You can use the iterrows() method to iterate over rows in a Pandas DataFrame.

Using global variables in a function

In Python, you can use global variables in a function by declaring the variable as global within the function.

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.

Finding the index of an item in a list

To find the index of an item in a list in Python, you can use the index() method of the list.