How-to articles, tricks, and solutions about PYTHON

How do I get the number of elements in a list (length of a list) in Python?

To get the number of elements in a list in Python, you can use the len() function.

Limiting floats to two decimal points

To limit a float to two decimal points, you can use the round() function.

How do I print curly-brace characters in a string while using .format?

To print a curly brace character in a string that is being formatted with the .format() method, you will need to use double curly braces to escape the character.

How can I randomly select an item from a list?

You can use the random module's choice function to select a random element from a list.

How do I check if an object has an attribute?

You can use the hasattr function to check if an object has an attribute.

Importing files from different folder

To import a module from a different folder in Python, you can use the sys.path.append() function to add the path to the folder containing the module to your Python path.

Calling a function of a module by using its name (a string)

To call a function from a module by using its name as a string, you can use the importlib module.

How to leave/exit/deactivate a Python virtualenv

To leave a Python virtual environment, you can use the deactivate command.

How to print without a newline or space

To print without a newline in Python, you can use the end argument for the print() function.

Class (static) variables and methods

In Python, class variables are variables that are shared by all instances of a class.

How do I lowercase a string in Python?

You can use the lower() method to lowercase a string in Python.

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 to upgrade all Python packages with pip?

You can use the pip freeze command to generate a requirements file that includes all of the current packages and their versions, and then use pip install -r to upgrade all packages to the latest available versions.

How do I sort a list of dictionaries by a value of the dictionary?

To sort a list of dictionaries by a value of the dictionary, you can use the sorted() function and specify the key parameter to be a lambda function that returns the value you want to sort by.

How do I get the last element of a list?

To get the last element of a list, you can use negative indexing.

How do I parse a string to a float or int?

To parse a string to a float or int in Python, you can use the float() and int() functions, respectively.

Check if a given key already exists in a dictionary

To check if a given key already exists in a dictionary, you can use the in keyword.

How can I remove a key from a Python dictionary?

You can use the del statement to remove a key-value pair from a dictionary in Python.

Renaming column names in Pandas

To rename the column names of a Pandas DataFrame, you can use the DataFrame.rename() method.

Find the current directory and file's directory

To find the current directory in Python, you can use the following code:

Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?

The range() function generates a sequence of numbers, starting from the first argument, and ending before the second argument.

Convert string "Jun 1 2005 1:33PM" into datetime

To convert the string "Jun 1 2005 1:33PM" into a datetime object, you can use the datetime module in the Python standard library.

How can I access environment variables in Python?

You can use the os module in Python to access environment variables.

How do I split a list into equally-sized chunks?

Here is a function that takes a list and an integer n and splits the list into n equally sized chunks:

How do I print colored text to the terminal?

You can use the termcolor module to print colored text to the terminal in Python.