 ## assigning values in python

Operator: Example: Equivalent: = a = b a = b += a += b a = (a + b) -= a -= b a = (a – b) *= a *= b a = (a * b) /= a /= b a = (a / b) %= a %= b a = (a % b)… ## 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

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)…. ## python version command

# To check your Python version in the command line use: python –version # To check your Python verson inside a script use: import sys print(sys.version) Here is what the above code is Doing: 1. We’re importing the sys module. 2. We’re printing the version of Python that we’re using. The sys module is a… ## python join array of ints

Couple different options # Convert List as Joining > print “,”.join(str(n) for n in numbers) # Convert using Map > ‘, ‘.join(map(str, myList)) Here is what the above code is Doing: 1. The map function applies the str function to each element in the list. 2. The join function concatenates the results of the map… ## 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… 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… ## change key of dictionary python

# Basic syntax: # Approach 1: dictionary[new_key] = dictionary[old_key] del dictionary[old_key] # Approach 2: dictionary[new_key] = dictionary.pop(old_key) Here is what the above code is Doing: 1. Create a new key-value pair in the dictionary. 2. Assign the value of the old key to the new key. 3. Delete the old key-value pair from the dictionary. ## format number in python

num = 123 print(f'{num:.3f}’) # 123.000 Here is what the above code is Doing: 1. f'{num:.3f}’ is a f-string. The f stands for format. 2. The {num:.3f} is called a format specifier. 3. The num is the variable whose value is to be formatted. 4. The :.3f is called a format specifier. It means the…