# Materials adapted from Sara Mathieson PS C:\Users\tnguyen3> python3 Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import random >>> >>> total = 0 >>> for i in range(10): ... total += random.randrange(1,7) # die roll ... >>> total 30 >>> >>> rolls = [random.randrange(1,7,2) for i in range(10)] >>> rolls [5, 1, 5, 1, 5, 3, 5, 1, 1, 5] >>> >>> >>> >>> import matplotlib.pyplot as plt >>> >>> plt.plot(range(10), rolls) [] >>> plt.show() >>> >>> plt.scatter(range(10), rolls) >>> plt.show() >>> plt.show() # nothing shows up, as the current figure has already been displayed and emptied >>> >>> rolls = [random.randrange(1,7) for i in range(10000)] >>> plt.hist(rolls) (array([1652., 0., 1732., 0., 1640., 0., 1662., 0., 1674., 1640.]), array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ]), ) >>> plt.show() >>> >>> >>> >>> random.random() 0.6718624654944848 >>> random.random() 0.5006938475303759 >>> random.random() 0.7768178636013163 >>> random.random() 0.710085846068826 >>> >>> >>> >>> import numpy as np >>> >>> arr = np.zeros((3,4)) >>> arr array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) >>> >>> arr = np.array([[1,2,3,4], [5,6,7,8]]) >>> arr array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> >>> arr[0,:] array([1, 2, 3, 4]) >>> >>> arr[:,0] array([1, 5]) >>> >>> arr[:,0:3] array([[1, 2, 3], [5, 6, 7]]) >>> >>> arr[:,:3] array([[1, 2, 3], [5, 6, 7]]) >>> >>> arr[1,2] = 8 >>> arr array([[1, 2, 3, 4], [5, 6, 8, 8]]) >>> >>> arr.shape (2, 4) >>> >>> a = [2,3,4] >>> a.shape Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute 'shape' >>> >>> b = a >>> b [2, 3, 4] >>> b[1] = 8 >>> b [2, 8, 4] >>> a [2, 8, 4] >>> >>> x = 5 >>> y = x >>> y = 3 >>> x 5 >>> >>> >>> >>> dict = {} >>> len(dict) 0 >>> dict["rabbit"] Traceback (most recent call last): File "", line 1, in KeyError: 'rabbit' >>> dict[0] = "Thao" >>> dict[1] = "Darshan" >>> dict[2] = "Edgar" >>> dict {0: 'Thao', 1: 'Darshan', 2: 'Edgar'} >>> dict[1] 'Darshan' >>> for i in dict: ... print(i) ... 0 1 2 >>> for k, v in dict.items(): ... print(v) ... Thao Darshan Edgar >>> dict[1] = "Thao" >>> dict {0: 'Thao', 1: 'Thao', 2: 'Edgar'} >>>