If you want to serialize and deserialize Python objects you might have considered using the Python Pickle module.
The Python Pickle module allows to serialize and deserialize a Python object structure. Pickle provides two functions to write/read to/from file objects (dump() and load()). It also provides two functions to write/read to/from bytes objects.
We will go through few examples to show how pickle works both with file objects and bytes objects. We will also test it with multiple data types.
It’s time to pickle!
The Python Pickle module is used to perform serialization and deserialization of Python objects.
Have you ever seen the message “TypeError: unhashable type” when running your Python program? Do you know what to do to fix it?
The message “TypeError: unhashable type” appears in a Python program when you try to use a data type that is not hashable in a place in your code that requires hashable data. For example, as an item of a set or as a key of a dictionary.
This error can occur in multiple scenarios and in this tutorial we will analyse few of them to make sure you know what to do when you see this error.
Are you writing a Python application and do you need to check for duplicates in a list? You are in the right place, let’s find out how to work with duplicates.
There are several approaches to check for duplicates in a Python list. Converting a list to a set allows to find out if the list contains duplicates by comparing the size of the list with the size of the set. This tells if the list contains duplicates and one way to know which items are duplicates you can use collections.Counter.
There are two aspects of duplicates you might want…
While reading a Python program you might find the expression if __name__ == ‘__main__’. Let’s have a look at what it means and why you should know about it.
The condition if __name__ == ‘__main__’ is used in a Python program to execute the code inside the if statement only when the program is executed directly by the Python interpreter. When the code in the file is imported as a module the code inside the if statement is not executed.
It might not be very clear why this is done and with this tutorial the reason will become clear.
How to count unique values in a Python list? There are multiple ways and in this tutorial we will go through them as a way for you to practice several Python constructs.
The simplest way to count unique values in a Python list is to convert the list to a set considering that all the elements of a set are unique. You can also count unique values in a list using a dictionary, the collections.Counter class, Numpy.unique() or Pandas.unique().
Most of our examples will work on the same list of numbers:
numbers = [2, 3, 3, 6, 8, 8, 23…
The Python Pandas library provides a function to calculate the standard deviation of a data set. Let’s find out how.
The Pandas DataFrame std() function allows to calculate the standard deviation of a data set. The standard deviation is usually calculated for a given column and it’s normalised by N-1 by default. The degrees of freedom of the standard deviation can be changed using the ddof parameter.
In this article I will make sure the reason why we use the standard deviation is clear and then we will look at how to use Pandas to calculate the standard deviation for…
Have you ever seen the error “tuple object does not support item assignment” when working with tuples in Python? In this article we will learn why this error occurs and how to solve it.
The error “tuple object does not support item assignment” is raised in Python when you try to modify an element of a tuple. This error occurs because tuples are immutable data types. It’s possible to avoid this error by converting tuples to lists or by using the tuple slicing operator.
Let’s go through few examples that will show you in which circumstances this error occurs and…
In this tutorial you will see how to convert a tuple to a string in Python. We will go through few examples you will find useful for your own applications.
The simplest method to convert a tuple to a string in Python is with the str.join() method that generates a string by concatenating the strings in a tuple. This works fine if the elements of the tuple are strings, otherwise an alternative approach is required, for example by using the map function.
We will start with a simple example and then go through different scenarios you might also encounter in…
In this tutorial, we will create a Python program to check the availability of one or more domains. You will learn lots of concepts that you can definitely reuse in other programs.
To check if a domain is available you can call the whois command using Python and then parse the output returned by whois. This check can also be done using the nslookup or dig commands. The most common module to execute these commands in Python is subprocess.
Let’s start creating our program!
Below you can see the syntax of the whois command:
The output of this…
While working with numbers you might have found the need to use the Python Modulo operator in your program. Let’s find out more about it.
The Python Modulo operator returns the remainder of the division between two numbers and it is represented using the % symbol. The Modulo operator is part of Python arithmetic operators. Here is an example of how to use it: 5 % 2 is equal to 1 (the remainder of the division between 5 and 2).
Let’s go through some examples to explain the meaning of modulo in Python.
One of the arithmetic operators available in…
I’m a Tech Lead, Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!