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…
At some point in your development journey, you might have to generate random data in Python. In this tutorial, we will see how to do that.
The Python built-in random module is used to generate random numbers (e.g. integers or floats) within a specific range. It also allows generating strings and lists with random elements. The Numpy library also provides the random module to generate random data structures.
We will go through multiple ways of applying the Python built-in random module and the Numpy random module.
Let’s start with some examples!
Python provides a module to generate random numbers: the…
Knowing how to concatenate Python strings is something you need when writing your applications. Let’s see the options available to do that.
With Python you can concatenate strings in different ways, the basic one is based on the + operator. If you have two strings (string1 and string2) you can concatenate them with the expression string1 + string2. Python also provides the join() and format() methods to concatenate multiple strings together.
It’s time for some examples!
The first and most basic way to concatenate two or more Python strings is by using the + operator.
For example, let’s define two…
I’m a Tech Lead, Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!