Found 33676 Articles for Programming

How to find the first date of a given year using Python?

SaiKrishna Tavva
Updated on 27-Feb-2025 17:04:07

6K+ Views

The datetime module in Python can be used to find the first day of the given year. In general, this datetime module is mostly used for manipulating dates and times. Some of the Common approaches to print the first day of the given year by using Python are as follows. Datetime Module: Widely used library for manipulating dates and times in various ways. Calendar Module: Provides functions related ... Read More

Write a Python program to remove a certain length substring from a given string

Prasad Naik
Updated on 16-Mar-2021 10:17:50

188 Views

We need to write a Python program which will remove a certain substring from a given stringAlgorithmStep 1: Define a string. Step 2: Use the replace function to remove the substring from the given string.Example CodeLive Demooriginal_string = "C++ is a object oriented programming language"modified_string = original_string.replace("object oriented", "")print(modified_string)OutputC++ is a  programming languageExplanationThe in-build Python replace() function takes the following parameters:Oldstring: The string which you want to removeNewstring: The new string you want to replace in place of the oldstringCount: Optional. Number of times you want to replace the oldstring with the newstringRead More

Print dates of today, yesterday and tomorrow using Numpy

Prasad Naik
Updated on 16-Mar-2021 10:10:45

472 Views

In this program, we will print today's, yesterday's and tomorrow's dates using the numpy library.AlgorithmStep 1: Import the numpy library. Step 2: find today's date using the datetime64() function. Step 3: find yesterday's date by subtracting the output of timedelta64() function from the output of datetime64() function. Step 4: Find yesterday's date by adding the output of timedelta64() function from the output of datetime64() function.Example Codeimport numpy as np todays_date = np.datetime64('today', 'D') print("Today's Date: ", todays_date) yesterdays_date = np.datetime64('today', 'D') - np.timedelta64(1, 'D') print("Yesterday's Date: ", yesterdays_date) tomorrows_date = np.datetime64('today', 'D') + np.timedelta64(1, 'D') print("Tomorrow's Date: ... Read More

How to make several plots on a single page using matplotlib in Python?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:12:58

419 Views

Using Pandas, we can create a data frame and create a figure and axis. After that, we can use the scatter method to draw points.StepsCreate lists of students, marks obtained by them, and color codings for each score.Make a data frame using Panda’s DataFrame, with step 1 data.Create fig and ax variables using subplots method, where default nrows and ncols are 1.Set the X-axis label using plt.xlabel() method.Set the Y-axis label using plt.ylabel() method.A scatter plot of *y* vs. *x* with varying marker size and/or color.To show the figure, use plt.show() method.Examplefrom matplotlib import pyplot as plt import pandas as ... Read More

What is the currently correct way to dynamically update plots in Jupyter/iPython?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:15:11

568 Views

We can first activate the figure using plt.ion() method. Then, we can update the plot with different sets of values.StepsCreate fig and ax variables using subplots method, where default nrows and ncols are 1.Draw a line, using plot() method.Set the color of line, i.e., orange.Activate the interaction, using plt.ion() method.To make the plots interactive, change the line coordinates.ExampleIn [1]: %matplotlib auto Using matplotlib backend: GTK3Agg In [2]: import matplotlib.pyplot as plt # Diagram will get popped up. Let’s update the diagram. In [3]: fig, ax = plt.subplots() # Drawing a line In [4]: ax.plot(range(5)) In ... Read More

How can I set the 'backend' in matplotlib in Python?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:08:06

724 Views

We can use matplotlib.rcParams['backend'] to override the backend value.StepsUsing get_backend() method, return the name of the current backend, i.e., default name.Now override the backend name.Using get_backend() method, return the name of the current backend, i.e., updated name.Exampleimport matplotlib print("Before, Backend used by matplotlib is: ", matplotlib.get_backend()) matplotlib.rcParams['backend'] = 'TkAgg' print("After, Backend used by matplotlib is: ", matplotlib.get_backend())OutputBefore, Backend used by matplotlib is: GTK3Agg After, Backend used by matplotlib is: TkAgg

Scatter plot and Color mapping in Python

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:07:43

766 Views

We can create a scatter plot using the scatter() method and we can set the color for every data point.StepsCreate random values (for x and y) in a given shape, using np.random.rand() method.Create a scatter plot of *y* vs. *x* with varying marker size and/or color, using the scatter method where color range would be in the range of (0, 1000).Show the figure using plt.show().Exampleimport matplotlib.pyplot as plt import numpy as np x = np.random.rand(1000) y = np.random.rand(1000) plt.scatter(x, y, c=[i for i in range(1000)]) plt.show()OutputRead More

How to reset index in Pandas dataframe?

Prasad Naik
Updated on 16-Mar-2021 10:14:44

418 Views

In this program, we will replace or, in other words, reset the default index in the Pandas dataframe. We will first make a dataframe and see the default index and then replace this default index with our custom index.AlgorithmStep 1: Define your dataframe. Step 2: Define your own index. Step 3: Replace the default index with your index using the reset function in Pandas library.Example Codeimport pandas as pd dataframe = {'Name':["Allen", "Jack", "Mark", "Vishal"], 'Marks':[85, 92, 99, 87]} df = pd.DataFrame(dataframe) print("Before using reset_index:", df) own_index = ['a', 'j', 'm', 'v'] df = pd.DataFrame(dataframe, own_index) ... Read More

How to plot two dotted lines and set marker using Matplotlib?

Prasad Naik
Updated on 16-Mar-2021 10:15:42

2K+ Views

In this program, we will plot two lines using the matplot library. Before starting to code, we need to first import the matplotlib library using the following command −Import matplotlib.pyplot as pltPyplot is a collection of command style functions that make matplotlib work like MATLAB.AlgorithmStep 1: Import matplotlib.pyplot Step 2: Define line1 and line2 points. Step 3: Plot the lines using the plot() function in pyplot. Step 4: Define the title, X-axis, Y-axis. Step 5: Display the plots using the show() function.Example Codeimport matplotlib.pyplot as plt line1_x = [10, 20, 30] line1_y = [20, 40, 10] line2_x = ... Read More

How to draw different shapes using the Python Turtle library?

Prasad Naik
Updated on 16-Mar-2021 10:08:58

3K+ Views

In this program, we will draw different shapes using the Turtle library in Python. Turtle is a python feature like a drawing board, which lets you command a turtle to draw all over it. The different shapes that we are going to draw are square, rectangle, circle and a hexagon.AlgorithmStep 1: Take lengths of side for different shapes as input.Step 2: Use different turtle methods like forward() and left() for drawing different shapes.Example Codeimport turtle t = turtle.Turtle() #SQUARE side = int(input("Length of side: ")) for i in range(4):    t.forward(side)    t.left(90) #RECTANGLE side_a = int(input("Length of ... Read More

Advertisements