smathieson@smathieson-24 summary_stats % python3 Python 3.9.6 (default, Feb 3 2024, 15:58:27) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin Type "help", "copyright", "credits" or "license" for more information. # user input and random >>> phrase = input("phrase: ") phrase: creative code >>> phrase 'creative code' >>> x = input('enter num classes: ") File "", line 1 x = input('enter num classes: ") ^ SyntaxError: EOL while scanning string literal >>> x = input('enter num classes: ') enter num classes: 4 >>> x '4' >>> x = int(input('enter num classes: ')) enter num classes: 4 >>> x 4 >>> # random module >>> import random >>> >>> total = 0 >>> for i in range(10): ... total += random.randrange(1,7) ... >>> total 41 >>> for i in range(10): ... total += random.randrange(1,7) ... >>> total 83 >>> [random.randrange(1,7) for i in range(10)] [2, 5, 3, 1, 2, 3, 4, 4, 5, 5] >>> [random.randrange(1,7) for i in range(10)] [4, 6, 5, 2, 2, 3, 3, 1, 6, 4] >>> # plotting >>> >>> import matplotlib.pyplot as plt >>> rolls = [random.randrange(1,7) for i in range(10)] >>> rolls [4, 1, 4, 5, 1, 4, 3, 4, 3, 1] >>> >>> plt.plot(range(10), rolls) # x list, y list [] >>> plt.show() >>> plt.scatter(range(10), rolls) # x list, y list >>> plt.show() >>> plt.clf() >>> plt.savefig("my_plot.png") >>> plt.bar(range(10), rolls) # x list, y list >>> plt.show() >>> rolls = [random.randrange(1,7) for i in range(100000)] >>> rolls [1, 3, 3, 2, 1, 1, 5, 1, 3, 1, 2, 1, 5, 6, 2, 5, 1, 5, 4, 4, 4, 3, 4, 5, ..., 1, 6, 3] >>> plt.hist(rolls) (array([16566., 0., 16701., 0., 16484., 0., 16995., 0., 16718., 16536.]), array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5, 6. ]), ) >>> plt.show() >>> # numpy >>> import numpy as np >>> >>> arr = np.zeros((3,4)) # rows, columns >>> arr array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) >>> >>> arr[2][3] = 7 >>> arr array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 7.]]) >>> arr[2,3] = 6 >>> arr array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 6.]]) >>> arr[0,2] = 6 >>> arr[2,1] = 5 >>> arr array([[0., 0., 6., 0.], [0., 0., 0., 0.], [0., 5., 0., 6.]]) >>> arr[1] array([0., 0., 0., 0.]) >>> arr[:,1] array([0., 0., 5.]) >>> arr[1,:2] array([0., 0.]) >>> arr[2,:2] array([0., 5.]) >>> # dictionaries >>> animals = {} # empty dictionary >>> type(animals) >>> animals["dog"] = 4 >>> animals["snake"] = 0 >>> animals {'dog': 4, 'snake': 0} >>> animals["cat"] = 4 >>> animals["octopus"] = 8 >>> animals["human"] = 2 >>> animals {'dog': 4, 'snake': 0, 'cat': 4, 'octopus': 8, 'human': 2} >>> for animal in animals: ... print(animal) ... dog snake cat octopus human >>> for animal in animals: ... print(animal, animals[animal]) ... dog 4 snake 0 cat 4 octopus 8 human 2 >>> for animal in animals: ... if animals[animal] == 4: ... print(animal) ... dog cat >>> for key, value in animals.items(): ... print(key, value) ... dog 4 snake 0 cat 4 octopus 8 human 2 >>>