How-to articles, tricks, and solutions about PYTHON

Unzipping files in Python

Here is a code snippet that demonstrates how to unzip a file using the zipfile module in Python:

Python list of dictionaries search

Here is a code snippet that demonstrates how to search for a specific value in a list of dictionaries in Python:

How do I use raw_input in Python 3?

In Python 3, the input() function can be used in place of raw_input() to read input from the user.

if else in a list comprehension

Here's an example of using an if-else statement within a list comprehension:

List attributes of an object

In Python, you can use the built-in function dir() to list the attributes of an object.

Python Requests throwing SSLError

Here's a code snippet that demonstrates how to use the Python requests library to make a GET request to a URL, while handling a possible SSLError:

Working with UTF-8 encoding in Python source

Here is a code snippet that demonstrates how to work with UTF-8 encoding in a Python source file:

How do I put a variable’s value inside a string (interpolate it into the string)?

In Python, you can use the format() method or f-strings (available in Python 3.6 and above) to interpolate a variable's value into a string.

Extracting just Month and Year separately from Pandas Datetime column

You can extract the month and year separately from a Pandas datetime column using the dt accessor.

How to initialize a two-dimensional array in Python?

You can use the built-in list function to create a 2D array (also known as a list of lists) in Python.

In Python, how do I convert all of the items in a list to floats?

You can use a for loop and the float() function to convert each item in a list to a float.

Convert columns to string in Pandas

To convert all columns in a Pandas DataFrame to strings, you can use the following code snippet:

Converting Dictionary to List?

In Python, you can convert a dictionary to a list of its keys or values using the keys() and values() methods, respectively.

I'm getting Key error in python

A KeyError in Python is raised when a dictionary key is not found in the dictionary.

Check if a word is in a string in Python

You can use the in keyword to check if a word is in a string in Python.

ImportError: No module named matplotlib.pyplot

This error message occurs when the matplotlib.pyplot module is not installed or not imported properly in your Python environment.

How to search for a string in text files?

Here's a Python code snippet that demonstrates how to search for a string in all text files in a directory:

Best way to strip punctuation from a string

One way to strip punctuation from a string is to use the str.translate() method in combination with the string.punctuation constant.

Python int to binary string?

You can use the built-in bin() function to convert an integer to a binary string in Python.

Dump a NumPy array into a csv file

Here's an example code snippet that demonstrates how to dump a NumPy array into a CSV file:

How do I do a case-insensitive string comparison?

You can convert both strings to lowercase or uppercase (using the lower() or upper() method) before doing the comparison.

Is it possible to break a long line to multiple lines in Python?

Yes, it is possible to break a long line of code into multiple lines in Python.