Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 83 of 2547
How to Get the next key in Dictionary in Python?
Dictionaries are powerful data types in Python that consist of key-value pairs. While accessing values in a dictionary is straightforward, there may be situations where you need to find the next key in a dictionary. Since Python dictionaries maintain insertion order (Python 3.7+), we can explore different methods to get the next key in a dictionary. Using keys() and index() Method The most straightforward approach is to convert dictionary keys into a list and use indexing to find the next key. Syntax dictionary_name.keys() list_object.index(key_name, start, end) The keys() method returns a view object ...
Read MoreHow to Get a sorted list of random integers with unique elements using Python?
Generating random numbers is one of the most popular techniques in programming, statistics, machine learning models, etc. Creating a sorted list of random integers with unique elements is a common task. In this article, we will explore different approaches to get a sorted list of random integers with unique elements using Python. Using sample() Function from Random Module The random.sample() method generates random samples of k elements from a given population without replacement, ensuring unique elements. Syntax random.sample(population, k) sorted(iterable, key=None, reverse=False) The sample() function takes a population (iterable) and returns k unique ...
Read MoreHow to generate k random dates between two other dates using Python?
Generating random dates is essential in data science applications like neural network training, stock market analysis, and statistical modeling. Python provides several approaches to generate k random dates between two specified dates using different libraries and techniques. Using random and datetime Modules The datetime module handles date operations while random generates random numbers. Combining these modules allows us to create random dates within a specified range. Example The following function generates k random dates by adding random days to the start date within the valid range ? import random from datetime import timedelta, datetime ...
Read MoreGet the Most recent previous business day using Python
In business applications, developers often need to calculate the most recent business day (excluding weekends). This is crucial for financial systems, reporting tools, and data analysis where operations only occur on weekdays. Using the datetime Library The datetime library provides built-in classes to handle dates and times. We can use timedelta to subtract days and check if the result is a business day using the weekday() method. Syntax timedelta(days=) The timedelta class represents the difference between two dates. The days parameter specifies how many days to add or subtract. Example This ...
Read MoreGaussian fit using Python
Data analysis and visualization are crucial nowadays, where data is the new oil. Typically data analysis involves feeding the data into mathematical models and extracting useful information. The Gaussian fit is a powerful mathematical model that data scientists use to model data based on a bell-shaped curve. In this article, we will understand Gaussian fit and how to implement it using Python. What is Gaussian Fit A bell-shaped curve characterizes the Gaussian distribution. The bell-shaped curve is symmetrical around the mean (μ). We define a probability density function as follows: f(x) = (1 / (σ * ...
Read MorePython - Updating value list in dictionary
In Python, dictionaries store data as key-value pairs where values can be lists. Updating these value lists is a common operation when working with dynamic data structures. Syntax dictionary_name[key] = new_value_list You can update a value list by assigning a new list to the specific key using the assignment operator (=). This replaces the existing list associated with that key. Method 1: Direct Assignment Replace the entire value list with a new list ? # Create a dictionary with initial values student_grades = { 'math': [85, 90, ...
Read MoreVisualizing O(n) using Python
Time complexity is a fundamental concept in computer science that measures how an algorithm's runtime changes as the input size grows. O(n) represents linear time complexity, where execution time increases proportionally with input size. Understanding and visualizing O(n) helps developers write efficient code and analyze algorithm performance. What is O(n) Time Complexity? O(n) indicates that an algorithm's execution time grows linearly with the input size 'n'. If you double the input, the execution time roughly doubles too. The most common example is a simple loop that processes each element once ? def linear_search(arr, target): ...
Read MorePython - Visualize graphs generated in NetworkX using Matplotlib
NetworkX is a powerful Python library for creating, manipulating, and studying complex networks. When combined with Matplotlib, it provides excellent capabilities for visualizing graphs with customizable layouts, colors, and labels. Basic Graph Visualization The simplest way to visualize a NetworkX graph is using the draw() function with Matplotlib ? import networkx as nx import matplotlib.pyplot as plt # Create a simple graph G = nx.Graph() G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(3, 1) # Draw and display the graph nx.draw(G, with_labels=True, node_color='lightblue', node_size=500) plt.title("Basic Graph Visualization") plt.show() Visualizing Graphs with Node Labels and Edge ...
Read MorePython - Valid Ranges Product
The valid ranges product problem involves finding the product of consecutive non-zero elements in a list, treating zeros as delimiters. This is useful in data processing and algorithmic challenges where you need to process segments of data separated by specific values. Problem Definition Given a list containing numbers and zeros, we need to ? Identify continuous sequences of non-zero numbers Calculate the product of elements in each valid sequence Return a list of all products For example, in [4, 9, 0, 0, 3, 4, 5, 0, 0, 4, 0], the valid ranges are [4, ...
Read MorePython - Use of slots
Python's __slots__ is a class attribute that restricts which attributes can be assigned to instances, providing memory optimization and faster attribute access. By defining slots, Python avoids creating a __dict__ for each instance, significantly reducing memory usage. Syntax class MyClass: __slots__ = ('attr1', 'attr2', 'attr3') The __slots__ attribute accepts a tuple, list, or any iterable containing the names of allowed attributes. Once defined, instances can only have these specific attributes. Basic Usage of Slots Here's a simple example demonstrating how slots work ? class Person: ...
Read More