-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevNet_Python.py
More file actions
391 lines (363 loc) · 14.8 KB
/
DevNet_Python.py
File metadata and controls
391 lines (363 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# ==================================================================================================
# This is my DevNet Express Intro to Python Exercise suite.
# Select Corresponding Exercise number or type Exit to quit.
# The simple Exercises are inline, while some of the more-complex ones are in Functions
# --------------------------------------------------------------------------------------------------
#
NumExercises = 18
# ==================================================================================================
# MathFun(radius) -
# --------------------------------------------------------------------------------------------------
#
def MathFun(Rad):
from math import pi
RadSquare = Rad ** 2
return (pi*RadSquare, RadSquare)
# ==================================================================================================
# my_function() -
# --------------------------------------------------------------------------------------------------
#
def my_function():
pass
# ==================================================================================================
# my_function1(cnt) -
# --------------------------------------------------------------------------------------------------
#
def my_function1(cnt):
print("My last count was: ",cnt)
# ==================================================================================================
# my_function2(cnt) -
# --------------------------------------------------------------------------------------------------
#
def my_function2(cnt):
import math
return(math.pi * cnt)
# ==================================================================================================
# my_function2(cnt) -
# --------------------------------------------------------------------------------------------------
#
def scopeFun(x):
x += 5
print("scopeFun: x = ", x)
return()
# ==================================================================================================
# scopeGlob() -
# --------------------------------------------------------------------------------------------------
#
def scopeGlob():
global x
x += 5
print("scopeGlob: x = ", x)
return()
# ==================================================================================================
# main() - Infinite loop. Type "Exit" to quit.
# --------------------------------------------------------------------------------------------------
#
InputString = "Enter Exercise Number (1-" + str(NumExercises) + ") or 'Exit': "
while True:
Response = str(input(InputString))
if Response == "EXIT" or Response == "Exit" or Response == "exit" or Response == "E" or Response =="e":
break
elif Response == "1":
# Introduction to Python Exercise-1
# Classic "Hello World" and two variants showing formatting and quotes
print ("Hello World!")
print()
print ('Hello World! \tHow are you?')
print()
print ("Hello World! \nHow's it going?")
print("\n=======(",Response, ")=======\n")
elif Response == "2":
# Introduction to Python Exercise-2
# Variable assignments: Integer verses Strings - The second print returns an error
myvar = 10
print (myvar + 1)
print()
myvar = "10"
#print (myvar + 1)
# The next statement wouldn't happen because of the error in the previous line
print (myvar + "1")
print("\n=======(",Response, ")=======\n")
elif Response == "3":
# Introduction to Python Exercize-3
# Comments & fomatting for reading + Intro to Conditionals and Indentation
# This is a comment and blank spaces make code readable
print()
print ("Helloworld!")
num = 1
if num < 1:
print ("I'm less than 1!")
print ("Goodbye Cruel World!")
print("\n=======(",Response, ")=======\n")
elif Response == "4":
# Introduction to Python Exercize-4
# Extend previous example to include conditionals for "elif" and "else"
print ("Helloworld!")
num = 1
if num < 1:
print ("I'm less than 1!")
print ("Cruel World!")
elif num == 1:
print ("I’m equal to 1!")
else:
print ("I’m greater than 1! ")
print("\n=======(",Response, ")=======\n")
elif Response == "5":
# Introduction to Python Exercize-5
# Building on Exercise 4. Adding input from the user. Note: This will return an error
print ("Helloworld!")
num = 1
num = input("Input a number: ")
if num < 1:
# The next statements won't happen because of the error in the previous line
print ("I'm less than 1!")
print ("Cruel World!")
elif num == 1:
print ("I’m equal to 1!")
else:
print ("I’m greater than 1! ")
print("\n=======(",Response, ")=======\n")
elif Response == "6":
# Introduction to Python Exercize-6
# Correcting the Error in Exercise #5
print ("Helloworld!")
num = 1
num = int(input("Input a number: "))
if num < 1:
print ("I'm less than 1!")
print ("Cruel World!")
elif num == 1:
print ("I’m equal to 1!")
else:
print ("I’m greater than 1! ")
print("\n=======(",Response, ")=======\n")
elif Response == "7":
# Introduction to Python Exercize-7
# Correcting the Error in Exercise #5
print ("Helloworld!")
num = 1
num = int(input("Input a number: "))
if num < 1:
print ("I'm less than 1!")
print ("Goodby Cruel World!")
elif num == 1:
print ("I’m equal to 1!")
else:
print ("Value is: " + str(num))
print ("Value is: ", num)
print("\n=======(",Response, ")=======\n")
elif Response == "8":
# Introduction to Python Exercize-8
# Iteration using For Statement
num = 1
for count in range(3):
num = int(input("Input a number: "))
if num < 1:
print ("I'm less than 1!")
print ("Goodby Cruel World!")
elif num == 1:
print ("I’m equal to 1!")
else:
print ("Value is: " + str(num))
print ("Value is: ", num)
print("\n=======(",Response, ")=======\n")
elif Response == "9":
# Introduction to Python Exercize-9
# Iteration using While Statement
num = 1
while num > 0:
num = int(input("Input a number: "))
if num < 1:
print ("I'm less than 1!")
print ("Goodby Cruel World!")
elif num == 1:
print ("I’m equal to 1!")
else:
print ("Value is: " + str(num))
print ("Value is: ", num)
print("\n=======(",Response, ")=======\n")
elif Response == "10":
# Introduction to Python Exercize-10
# Manipulating lists
print ("MyList Example\n")
MyList = []
print (len(MyList), MyList)
MyList = ['Fred', "Barney", 3, 5.5]
print (len(MyList), MyList)
print()
MyList.append("Wilma")
MyList.append("Betty")
print (len(MyList), MyList)
print()
MyList.remove(3)
MyList[2] = MyList[1]
MyList[1] = MyList[3]
MyList[3] = MyList[4]
MyList.remove(MyList[3])
print (len(MyList), MyList)
print("\n=======(",Response, ")=======\n")
elif Response == "11":
# Introduction to Python Exercize-11
# Manipulating lists - Advanced
MyList1 = ["Alpha", "Beta", "Gamma", "Delta", "Episilon", "Ztea", "Eta", "Theta", "Iota"]
MyList2 = MyList1[1:3]
MyList3 = MyList1[:3]
MyList4 = MyList1[3:]
MyList5 = MyList1[:]
print ("MyList1 Length:", len(MyList1), MyList1, id(MyList1))
print ("MyList2 Length:", len(MyList2), MyList2, id(MyList2))
print ("MyList3 Length:", len(MyList3), MyList3, id(MyList3))
print ("MyList4 Length:", len(MyList4), MyList4, id(MyList4))
print ("MyList5 Length:", len(MyList5), MyList5, id(MyList5))
print()
MyList1 = ["Alpha", "Beta", "Gamma"]
MyList2 = MyList1
print (len(MyList1), MyList1)
print (len(MyList2), MyList2)
MyList1.append("Delta")
MyList2.append("Episilon")
print (len(MyList1), MyList1, id(MyList1))
print (len(MyList2), MyList2, id(MyList2))
print()
MyList2 = list(MyList1)
print (len(MyList1), MyList1, id(MyList1))
print (len(MyList2), MyList2, id(MyList2))
print("\n=======(",Response, ")=======\n")
elif Response == "12":
# Introduction to Python Exercize-12
# Manipulating lists in lists - Advanced
MyList = [["red",[255,0,0]],["green",[0,255,0]],["blue",[0,0,255]],["yellow",[255,255,102]]]
for i in range(len(MyList)):
print (MyList[i][0],end="")
for j in range(len(MyList[i][1])):
print ("\t",MyList[i][1][j])
print()
import numpy as np
aList = np.arange(8)
print ('Starting with an 8-element array:')
print (aList)
bList = aList.reshape(4,2)
print ('\nReshaped to a 4x2 array:')
print (bList,"\n")
print()
aList = np.arange(9)
print ('Starting with a 9-element array:')
print (aList)
bList = aList.reshape(3,3)
print ('\nReshaped to a 3x3 array:')
print (bList,"\n")
print()
# Other ways to initialize a list
print("Creating Initialized lists with numpy.")
aList = np.zeros(7)
bList = np.ones(7)
print("List of zeros: ",aList)
print("List of ones: ",bList)
print("\n=======(",Response, ")=======\n")
elif Response == "13":
# Introduction to Python Exercize-13
# Manipulating Tuples in Python
print("\nMy Tuple Example:")
MyTuple = ()
print (len(MyTuple), MyTuple)
MyTuple = ("Bldg5", "Switch01", "1/0/1", 20)
print (len(MyTuple), MyTuple)
MyTuple1 = ("Bldg5", "Switch02", "1/0/1", 5)
print (len(MyTuple1), MyTuple1)
print (MyTuple[1],"GigE",MyTuple[2]," in ",MyTuple[0]," has ", MyTuple[3]-MyTuple1[3], "more errors than ",MyTuple1[1])
print()
for i in range(len(MyTuple)):
print (MyTuple[i])
print("\n=======(",Response, ")=======\n")
elif Response == "14":
# Introduction to Python Exercize-14
# List of Tuples
print("\nMy Tuple Example:")
MyTuple = ()
print (len(MyTuple), MyTuple)
MyTuple = ("Bldg5", "Switch01", "1/0/1", 20)
print (len(MyTuple), MyTuple)
MyTuple1 = ("Bldg5", "Switch02", "1/0/1", 5)
print (len(MyTuple1), MyTuple1)
print (MyTuple[1],"GigE",MyTuple[2]," in ",MyTuple[0]," has ",
MyTuple[3]-MyTuple1[3],
"more errors than ",MyTuple1[1])
print()
IntList = []
IntList.append(MyTuple)
IntList.append(MyTuple1)
print (len(IntList), IntList)
print (IntList[1][1])
print("\n=======(",Response, ")=======\n")
elif Response == "15":
print("\nMy Dictionary Example:")
for count in range(5):
my_function()
print(count)
print()
MyD = {"Type":"Cat9K","Ports":48,"Name":"Switch1","Loc":"Building 5"}
print (len(MyD), MyD)
print()
print (MyD["Name"]," in ", MyD["Loc"]," is a ", MyD["Type"])
print("\n=======(",Response, ")=======\n")
elif Response == "16":
print("\nmy_function() Examples:")
for count in range(5):
my_function()
print("My Count ",count)
print()
for count in range(5):
my_function1(count)
print()
for count in range(5):
npi = my_function2(count)
print (count, "times PI = ", npi)
print()
x = 5
scopeFun(x)
print("Main(): x = ", x)
print()
scopeGlob()
print("Main(): x = ", x)
print("\n=======(",Response, ")=======\n")
elif Response == "17":
r = int(input("Input the radius of a circle: "))
prs, rs = MathFun(r)
print("Radius = ", r, "\tr^2 = ", rs, "\tThe area of the circle is = ", prs)
print("\n=======(",Response, ")=======\n")
elif Response =="18":
food={"vegetables":["carrots","kale","cucumber","tomato"]}
for veg in food["vegetables"]:
print(veg)
print()
cars={"sports":{"Turbo 911":"Porsche","Viper":"Dodge","Corvette":"Chevy"}}
for auto in cars["sports"]:
print(auto, cars["sports"][auto])
print()
Devices={"WLC":"5520","Switch":"Nexus9k","Router":"ISR4221"}
for var in Devices:
print("Device: " + var + " Model " + Devices[var])
print()
myvar={"switches":{"fixed":["2900","3650","3850","9300","9500"]}}
print(myvar["switches"]["fixed"][0])
print("My list of access switches are:", end=" ")
for f in myvar["switches"]["fixed"]:
print(f, end=" ")
print()
myvar={"type":"donut","flavors":{"flavor":[{"type":"chocolate","id":1001},{"type":"glazed","id":1002},{"type":"sprinkled","id":1003}]}}
print("Id: " + str(myvar[flavors][flavor][0]["id"]) + " type " + myvar[flavors][flavor][0]["type"])
for f in myvar[flavors][flavor]:
print("Id is: " + str(f["id"]) + " type is: " + f["type"])
print("\n=======(",Response, ")=======\n")
elif Response == "19":
print("\n=======(",Response, ")=======\n")
elif Response =="20":
import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
print(installed_packages_list)
print("\n=======(",Response, ")=======\n")
else:
print ("Unrecognized Exercize Number. Value must be in range (1-",NumExercises,").")
print("\n=======(",Response, ")=======\n")