Python Jumpstart 2016 Cisco andor its affiliates All
- Slides: 25
Python Jumpstart © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 1
Python Commands Why do I care about the version? © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 2
Python Syntax, Data Types, Functions, … ▪ Indentation defines scope ▪ Python code blocks do not have explicit statement scope delimiters ▪ Indentation amount is relative ▪ Data Types ▪ Statements ▪ Import ▪ Print ▪ Conditional ▪ Functions ▪ Looping © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 3
Python Data Types © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 4
Python Data Types - Examples © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 5
Python Scope and Conditions In your IDE or terminal window type the following commands at the end of your program and then run it to see the output: # This is a comment print() print ("Helloworld!") num = 1 if num < 1: print ("I'm less than 1!") print ("Goodbye Cruel World!") ← Comments aren't printed ← Blank lines are nice formatting ← This simply prints a blank line ← prints "Helloworld!" ← Assigns the value 1 to variable "num" ← Note the conditional ends with a : ← Note indentation (be consistent) ← Nothing happens. Why? What happens if num = 0, 1, or >1? © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 6
Python Scope and Conditions print ("Helloworld!") num = 1 if num < 1: print ("I'm less than 1!") print ("Goodbye Cruel World!") elif num == 1: print ("I'm equal to 1!") else: print ("I'm greater than 1! ") Note ": " after each condition. Note indentation after each ": " Existing Code ← "if" Statement and condition ← Conditional block defined by indentation ← "else if" statement & condition ← Single line conditional block Add This Code ← "else" statement (optional) ← Single line conditional block Run the Program Note: You can have as many "elif" statements as you need © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 7
Python Conditions and String Concatenation print ("Helloworld!") num = 1 if num < 1: Insert Here: print ("I'm less than 1!") print ("Goodbye Cruel World!") elif num == 1: print ("I'm equal to 1!") else: print ("I'm greater than 1! ") Replace: num = input("Input a number: ") Run the program. What happens ? print ("Value is: " + str(num)) print ("Value is: ", num) © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 8
Python Conditions and String Concatenation print ("Helloworld!") num = 1 num = input("Input a number: ") if num < 1: print ("I'm less than 1!") print ("Goodbye Cruel World!") elif num == 1: print ("I'm equal to 1!") else: print ("Value is: " + str(num)) print ("Value is: ", num) num = int( input("Input a number: ") ) ERROR! "num" is assigned the string representation of your number. Need to convert it to an integer. © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 9
Python Conditions and String Concatenation print ("Helloworld!") num = 1 num = int(input("Input a number: ")) if num < 1: print ("I'm less than 1!") print ("Goodbye Cruel World!") elif num == 1: print ("I'm equal to 1!") else: Concatenates print ("Value is: " + str(num)) Strings print ("Value is: ", num) ← Accepts input from the terminal & places the result in the variable "num" The output looks "almost" the same: This concatenates the "Value" string with the character representation of the ← value contained in the variable "num" ← This prints the string followed by the character representation of "num" Note the spacing in the second print stmt © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 10
Conditional & Logical Operators © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 11
Python Looping for count in range(5) Loop from 0 to 4 for count in range(2, 5) for count in range(len(My. List)) Loop from 2 to 4 Loop from 0 to end of the list for fruit in basket Loops through a List, Tuple or Dictionary while count < 5 Must increment count while True Infinite loop. End with break statement © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 12
Looping with "for" in Python num = 1 for count in range(3): num = int(input("Input a number: ")) if num < 1: print ("I'm less than 1!") print (Goodbye Cruel World!") elif num == 1: print ("I'm equal to 1!") else: print ("Value is: " + str(num)) print ("Value is: ", num) 1. Insert the "for" statement and indent all remaining lines. These form the block of the "for" loop. 2. Run the program © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 13
Looping with "while" in Python num = 1 #for count in range(3): while num > 0: num = int(input("Input a number: ")) if num < 1: print ("I'm less than 1!") print (Goodbye Cruel World!") elif num == 1: print ("I'm equal to 1!") else: print ("Value is: " + str(num)) print ("Value is: ", num) 1. Comment out the "for" statement 2. Insert the "while" statement 3. Run the program How has the behavior changed? © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 14
Dictionaries in Python • A dictionary is different than a list or a tuple. Each element in a dictionary must contain a key followed by a value. This key value association is typically referred to as name-value pairs. Dictionaries are defined by curly braces {}. • Here's a dictionary {"car": "corvette", "age": 7, "food": "pizza"}. The value in a dictionary is accessed by its key. Dictionaries are sometimes called maps or associative arrays Add this to your code and run: What is the result? print("n. My Dictionary Example: ") My. Example = {"Type": "MS 250", "Ports": 48, "Name": "Switch 1", "Loc": "Building 5"} print (len(My. Example), My. Example) print (My. Example["Name"], " in ", My. Example["Loc"], " is a ", My. Example["Type"]) © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 15
Parsing JSON with Python © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 16
What is JSON? • JSON stands for Java Script Object Notation • Consists of text-based name-value pairs making it simple for applications to store and exchange data • Designed to be lightweight, readable, and minimal, avoiding excessive text which has sometimes been the complaint of XML (another text-based protocol for exchanging and storing data) • The structure and parsing of JSON is the same as Python dictionaries and lists • In JSON, data is set up in name value pairs just like a Python dictionary; however, in JSON, dictionaries are referred to as objects © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 17
Concept : JSON ▪ Java. Script Object Notation (JSON) [ ▪ A data-interchange text format { ▪ Really just "first_name": "Joe", "last_name": "Montana" }, { "first_name": "Jerry", "last_name": "Rice" } ▪ Collections of name/value pairs ▪ Object {} ▪ Ordered list of values ▪ Array [] ] © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 18
Practice with Dictionaries 1. Copy the following code 2. Run the program Devices={"model 1": "MR 52", "model 2": "MR 84", "model 3": "MR 42"} for model in Devices: print("Device Model: " + Devices[model]) Sample Output: Device Model MR 52 Device Model MR 84 Device Model MR 42 © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 19
Sending a REST API request 1. Copy the following code with a valid Dashboard API key (like your home lab's) 2. Run the program import requests import json api_key = '<insert api key between single quotes>' org_url = 'https: //dashboard. meraki. com/api/v 0/organizations' meraki_headers = {'x-cisco-meraki-api-key': api_key, 'content-type': 'application/json'} # get all of the organizations this api key has access to response = requests. get(org_url, headers=meraki_headers) json_output = json. loads(response. text) # loop through the json_output and print each row for row in json_output: print(row) print("Org ID: " + str(row['id'])) print("Org Name: " + row['name']) Sample Output: {u'id': 650207196201616360, u'name': u'Meraki Dev. Net Lab'} Org ID: 650207196201616360 Org Name: Meraki Dev. Net Lab © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 20
Deeply nested JSON structures In JSON, objects and arrays are often nested several layers in order to organize data. Looking at the following structure from the inside and moving outward you see that we've nested an array inside an object which itself is nested inside an object. With this structure starting from the outside and moving in we see that switches is the key with the object fixed being its value. But notice that fixed is also a key and its value is the array of switch types. As a result, to get the actual types we need to dig a little deeper. 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=" ") © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 21
Python Functions – Defining Your Own © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 22
Python Functions Defined - Examples © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 23
Python Functions Defined - Calling print ("I'm not a function") def my_function(): print("Hey I'm a function!") def brett(val): for i in range(val): print("I'm a function with args!") my_function() brett(5) © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 24
Thanks for Playing ☺ Content Credit: Cisco Dev. Net team, Paul Marsh, Meraki API team © 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 25
- Usa hockey affiliates
- Python andor
- Andor györgy
- Andor planet
- Andor logic
- Andor györgy üzleti gazdaságtan
- Andor basic
- Andor györgy üzleti gazdaságtan
- Andor györgy üzleti gazdaságtan
- Andor insert
- Andor matlab
- Name three lines
- Triage start
- Jumpstart triage system
- Jumpstart six sigma
- Start triage system
- Jumpstart jobs mr price
- Ubc library web of science
- Jump start triage
- Intershop pim
- A&ox1 shirt
- Ubc jumpstart
- Triage jump start
- Jumpstart nj angel network
- The emigree
- When a train increases its velocity its momentum