Found 26504 Articles for Server Side Programming

Python program to convert Set into Tuple and Tuple into Set

AmitDiwan
Updated on 14-Apr-2021 12:35:03

764 Views

When it is required to convert a set structure into a tuple, and a tuple into a set, the ‘tuple’ and ‘set’ methods can be used.Below is a demonstration of the same −Example Live Demomy_set = {'ab', 'cd', 'ef', 'g', 'h', 's', 'v'} print("The type is : ") print(type(my_set), " ", my_set) print("Converting a set into a tuple") my_tuple = tuple(my_set) print("The type is : ") print(type(my_tuple), " ", my_tuple) my_tuple = ('ab', 'cd', 'ef', 'g', 'h', 's', 'v') print("The tuple is:") print(my_tuple) print(type(my_tuple), " ", my_tuple) print("Converting tuple to set") my_set = set(my_tuple) print(type(my_set), " ", my_set)OutputThe type is : ... Read More

Python Program to Take in a String and Replace Every Blank Space with Hyphen

AmitDiwan
Updated on 14-Apr-2021 12:33:35

1K+ Views

When it is required to take a string and replace every blank space with a hyphen, the ‘replace’ method can be used. It takes two parameters, the blank space, and the value with which it needs to be replaced (hyphen in this case).Below is a demonstration of the same −Example Live Demomy_string = input("Enter a string :") print("The string entered by user is :") print(my_string) my_string = my_string.replace(' ', '-') print("The modified string:") print(my_string)OutputEnter a string : A-B-C-D E- A-B-C-D E- The string entered by user is : A-B-C-D E- The modified string: A-B-C-D--E-ExplanationAn input string is asked to be entered ... Read More

Python Program to Replace all Occurrences of ‘a’ with $ in a String

AmitDiwan
Updated on 14-Apr-2021 12:25:20

778 Views

When it is required to replace all the occurrences of ‘a’ with a character such as ‘$’ in a string, the string can be iterated over and can be replaced using the ‘+=’ operator.Below is a demonstration of the same −Example Live Demomy_str = "Jane Will Rob Harry Fanch Dave Nancy" changed_str = '' for char in range(0, len(my_str)):    if(my_str[char] == 'a'):       changed_str += '$'    else:       changed_str += my_str[char] print("The original string is :") print(my_str) print("The modified string is : ") print(changed_str)OutputThe original string is : Jane Will Rob Harry Fanch ... Read More

Python Program to Find Element Occurring Odd Number of Times in a List

AmitDiwan
Updated on 14-Apr-2021 12:22:45

1K+ Views

When it is required to find an element that occurs odd number of times in a list, a method can be defined. This method iterates through the list and checks to see if the elements in the nested loops match. If they do, the counter is incremented. If that count is not divisible by 2, the specific element of the list is returned as the result. Otherwise, -1 is returned as the result.Below is a demonstration of the same −Example Live Demodef odd_occurence(my_list, list_size):    for i in range(0, list_size):       count = 0       for ... Read More

Python Program to Remove the nth Occurrence of the Given Word in a List where Words can Repeat

AmitDiwan
Updated on 14-Apr-2021 12:20:26

2K+ Views

When it is required to remove a specific occurrence of a given word in a list of words, given that the words can be repeated, a method can be defined, that iterates through the list, and increments the counter by 1. If the count and the specific occurrence match, then the specific element from the list can be deleted.Below is a demonstration of the same −Example Live Demodef remove_word(my_list, my_word, N):    count = 0    for i in range(0, len(my_list)):       if (my_list[i] == my_word):       count = count + 1       ... Read More

How can I create a stacked line graph with matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 08:07:21

1K+ Views

To create a stacked lines graph with Python, we can take the following Steps −Create x, y, y1 and y2 points using numpy.Plot the lines using numpy with the above data (Step 1) and labels mentioned.Fill the color between curve y=e^x and y=0, using the fill_between() method.Fill the color between curve y=2x and y=0, using the fill_between() method.Fill the color between curve y=log(x) and y=0, using fill_between() method.Place the curve text using the legend() method.To display the figure, use the show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 5, 100) y = x * 2 y1 = np.log(x) ... Read More

How do I change matplotlib's subplot projection of an existing axis?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 08:04:40

2K+ Views

It seems difficult to change the projection of an existing axis, but we can take the following steps to create different type projections −Using subplot() method, add a subplot to the current figure, with nrows=1, ncols=3 and current index=1.Add a title to the current axis.Using subplot() method, add a subplot to the current figure, with nrows=1, ncols=3 and current index=2, projection=hammer.Add a title to current axis, hammer.Using subplot() method, add a subplot to the current figure, with nrows=1, ncols=3 and current index=3, projection=polar.Add a title to current axis, polar.To display the figure, use the show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = ... Read More

Matplotlib Plots Lose Transparency When Saving as .ps/.eps

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 08:01:27

2K+ Views

Whenever plots are saved in .eps/.ps, then the transparency of the plots get lost.To compare them, we can take the following Steps −Create x_data and y_data using numpy.Plot x_data and y_data (Step 1), using the plot() method, with less aplha value, to make it more transparent.Use the grid() method to prove the transparency of the line.Save the created plot in .eps format.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x_data = np.linspace(1, 10, 100) y_data = np.sin(x_data) plt.plot(x_data, y_data, c='green', marker='o', alpha=.35, ms=10, lw=1) plt.grid() plt.savefig("lost_transparency_img.eps") plt.show()OutputThe PostScript backend ... Read More

How to plot a gradient color line in matplotlib?

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:58:32

9K+ Views

To plot a gradient color line in matplotlib, we can take the following steps −Create x, y and c data points, using numpy.Create scatter points over the axes (closely so as to get a line), using the scatter() method with c and marker='_'.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-1, 1, 1000) y = np.exp(x) c = np.tan(x) plt.scatter(x, y, c=c, marker='_') plt.show()Output

Superscript in Python plots

Rishikesh Kumar Rishi
Updated on 10-Apr-2021 07:56:42

12K+ Views

To put some superscript in Python, we can take the following steps −Create points for a and f using numpy.Plot f = ma curve using the plot() method, with label f=ma.Add title for the plot with superscript, i.e., kgms-2.Add xlabel for the plot with superscript, i.e., ms-2.Add ylabel for the plot with superscript, i.e., kg.To place the legend, use legend() method.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True a = np.linspace(1, 10, 100) m = 20 f = m*a plt.plot(a, f, c="red", lw=5, label="f=ma") plt.title("Force $\mathregular{kgms^{-2}}$") plt.xlabel("Acceleration $\mathregular{ms^{-2}}$") plt.ylabel("Acceleration $\mathregular{kg}$") ... Read More

Advertisements