Earthquakes are natural phenomena that can have effects on human life and infrastructure. With the availability of geospatial and seismic datasets, scientists and researchers can analyze and visualize the earthquake data to identify the patterns, trends, and risk zones. Python, along with libraries like Matplotlib, Pandas, and NumPy, provides the tools to process and visualize the data. In this article, we are going to analyse and visualize earthquake data in Python with Matplotlib. Analyzing and visualizing Earthquake For the examples in this article, we use the dataset named "demo_1.csv" that contains information about the earthquakes. The data ... Read More
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once. For example, thing and night are anagrams of each other. In this article, we are going to learn how to check an anagram in a Python program using collections.Counter() method. This method counts the number of occurrences of each element in a collection (like characters in a string), helping us to compare two words for identical character counts. For example, Scenario Input: str1= "listen", str2= "silent" Output: True Two strings are said ... Read More
The given task is to perform an alternate summation on a Python List. For example, adding the elements at even indices (0, 2, 4) or odd indices (1, 3, 5), depending on the requirement. This means we need to add every other element from the given list. Let us see an input scenario - Scenario Input: [11, 22, 33, 44, 55] Output: 99 Explanation: Here, we are going to add the elements at the even indices by using the slicing [::2]. Alternate Element Summation Using sum() Function The alternate element summation in a list (Python) can ... Read More
Slicing is used to extract a portion of a sequence (such as a list, a tuple, string) or other iterable objects. Python provides a flexible way to perform slicing using the following syntax. list[start:stop:step] Alternate Range Slicing The alternate range slicing is used to retrieve the elements from a list by skipping the elements at a fixed interval, using the step parameter. For example, if we want every second element from a list, we set step=2. Let’s observe some examples to understand how alternate range slicing works. Example 1 Consider the following ... Read More
Lists in Python are ordered collections that store and manipulate a sequence of elements. In this article, we are going to learn alternate cycling in Python. It is used when we need to work with the alternate element, instead of processing every element in the list.Alternate cycling is the process of accessing or iterating through list elements by skipping elements in a fixed pattern. Following is an example scenario - Scenario Input:[1, 2, 3, 4, 6] Output:[1, 3, 5] Explanation: In this case, the alternate cycling starts from the index 0 and skips the next ... Read More
In this article, we will learn how to add a custom column in a tuple list (i.e., a list of tuples) in Python. Tuples store sequences of data enclosed in parentheses as shown below: Tuples = (11, 22, 33) And the list of tuples is represented as follows: List of tuples = [(11, 22, 33), (44, 55, 66), (77, 88, 99)] Suppose we have a list called "students" that stores student data (i.e., name, age) as tuples. If we represent it in the form of rows and columns, the first element of the tuples ... Read More
Our task is to add list items to a tuple list (i.e, a list of tuples) in Python. Tuples store sequences of data enclosed in parentheses, as shown below: Tuples = (11, 22, 33) And the list of tuples is represented as follows: List of tuples = [(11, 22, 33), (44, 55, 66), (77, 88, 99)] Scenario Suppose we have a list of tuples, "a", and another list of items, "b". We have to add all items of the "b" to each item of the "a" as follows - Input Lists: a = [(2, 3), ... Read More
In Python, some special methods have names that start and end with double underscores. These are called dunder methods. One such method is __exit__, which is used in context managers for cleanup tasks. Context Manager in Python Context Mangers helps to manage resources such as files, database connections, or network links. It sets up a temporary environment to run a block of code. When the block ends, Python closes the file or disconnects the connection automatically. This prevents unnecessary resource consumption when we leave a file open or keep a connection (such as, network, database) active. Python runs context managers ... Read More
Python does not require a main function to start execution like many other programming languages. Instead, it uses a special built-in variable called __name__ to determine how a Python script is being executed (directly or, is it imported as a module into another script). In this article, we will learn about the __name__ variable in Python. Understanding the __name__ Variable The __name__ variable is a built-in variable that holds the name of the current module. When you run a script directly, Python sets __name__ to __main__. If the same script is imported into another file as a module, __name__ ... Read More
What is the __future__ Module? The __future__ is a built-in Python module that is used to import features from newer versions of Python into older versions. This helps you to write code that will work the same way in both old and new versions of Python. It is mainly used when you are migrating code from Python 2 to Python 3, or when you want to try out new features before they become standard (default) in future releases. For example, in older Python versions, dividing two integers would return an integer. But if you import the division feature like this: ... Read More