Python dictionary append

Python dictionary append

d = {‘key’:’value’} print(d) # {‘key’: ‘value’} d[‘mynewkey’] = ‘mynewvalue’ print(d) # {‘mynewkey’: ‘mynewvalue’, ‘key’: ‘value’} Here is what the above code is Doing: 1. We create a dictionary with a single key:value pair. 2. We print the dictionary. 3. We add a new key:value pair to the dictionary. 4. We print the dictionary again….

legend size matplotlib

legend size matplotlib

import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 20, 1000) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x, y1, “-b”, label=”sine”) plt.plot(x, y2, “-r”, label=”cosine”) plt.legend(loc=”upper left”) plt.ylim(-1.5, 2.0) plt.show() Here is what the above code is Doing: 1. We create a list of numbers from 0 to 20, with 1000 elements (x)….

how to save a pickle file

how to save a pickle file

import pickle a = {‘hello’: ‘world’} with open(‘filename.pickle’, ‘wb’) as handle: pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL) with open(‘filename.pickle’, ‘rb’) as handle: b = pickle.load(handle) print a == b Here is what the above code is Doing: 1. Create a dictionary. 2. Open a file in write binary mode. 3. Dump the dictionary into the file. 4. Close…

python link to jpg

python link to jpg

import urllib.request urllib.request.urlretrieve(“http://www.gunnerkrigg.com//comics/00000001.jpg”, “00000001.jpg”) Here is what the above code is Doing: 1. It’s importing the urllib.request module. 2. It’s using the urllib.request.urlretrieve() function to download the image at the URL “http://www.gunnerkrigg.com//comics/00000001.jpg” and save it to the file “00000001.jpg”. 3. It’s not doing anything with the return value of urllib.request.urlretrieve(). The urllib.request.urlretrieve() function returns a…