How-to articles, tricks, and solutions about PYTHON

What's the canonical way to check for type in Python?

In Python, you can use the isinstance function to check if an object is an instance of a particular type.

How to prettyprint a JSON file?

To pretty-print a JSON file in Python, you can use the json module.

Generate random integers between 0 and 9

To generate a random integer between 0 and 9 in Python, you can use the random module and the randint function.

Installing specific package version with pip

To install a specific version of a package with pip, you can use the pip install command followed by the package name and the desired version number.

Meaning of @classmethod and @staticmethod for beginner

In Python, @classmethod is a decorator that is used to define a method as a class method.

How do I split the definition of a long string over multiple lines?

You can split a string over multiple lines by using triple quotes, either ''' or """ .

Getting the class name of an instance

To get the class name of an instance in Python, you can use the __class__ attribute of the object.

What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?

There are many different tools that you can use to create isolated Python environments, each with their own benefits and drawbacks.

How do I measure elapsed time in Python?

There are several ways to measure elapsed time in Python.

Static methods in Python?

In Python, a static method is a method that belongs to a class rather than an instance of the class.

How do I append to a file?

To append to a file in Python, you can use the "a" mode of the built-in open() function.

How can I remove a trailing newline?

To remove a trailing newline from a string in Python, you can use the rstrip() method.

How to check if the string is empty?

To check if a string is empty in Python, you can use the len() function or an if statement.

How do I pad a string with zeroes?

You can use the zfill() method to pad a string with zeros on the left side.

How to read a file line-by-line into a list?

To read a file line-by-line into a list, you can use the following approach:

Why is it string.join(list) instead of list.join(string)?

The join() method is a string method, so it is called on a string object and takes a list of strings as its argument.

Delete an element from a dictionary

To delete an element from a dictionary in Python, you can use the del statement.

Delete a column from a Pandas DataFrame

You can delete a column from a Pandas DataFrame using the drop function.

How to determine a Python variable's type?

In Python, you can determine the type of a variable by using the type() function.

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:

Determine the type of an object?

To determine the type of an object in Python, you can use the type function.

How to remove an element from a list by index

To remove an element from a list by index in Python, you can use the pop() method.