DEVNET-1893d
DEVNET-1893d
with Python
DEVNET-1893.d
Cisco Webex Teams
Questions?
Use Cisco Webex Teams to chat
with the speaker after the session
How
1 Find this session in the Cisco Events Mobile App
2 Click “Join the Discussion”
3 Install Webex Teams or go directly to the team space
4 Enter messages/questions in the team space
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 3
What do we cover today?
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 4
Why Coding as a
Network Engineer?
The Value-Proposition for Programmability
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 6
What Changed?
Online Communities
✓ Open Source
✓ Social Code Sharing (GitHub)
You can get powerful things done with relatively small amounts of code!
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 7
Coding with Python
• Very popular programming
language
What is Python? • Free
• Huge Community
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
• Powerful
• Readable and easy to learn
Why Python for • Widely available and Open
Source
Network Engineers?
• Windows, Mac, Linux
• Routers & Switches
• Lots of training resources
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
• A Python Interpreter installed
on your system where you run
What do you need? the script
• Text editor
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
sample.py
Output
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
Let’s dive more into Python!
What python should I use?
• Python 3.x
• Python 2.7 will not be maintained past 2020
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
Using your Python Interpreter
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 15
Python Script Structure and Execution
Import Modules
Functions
Output/Input
Variables
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
hello-world.py
Fun with Python
Basic Syntax
Basic Data Types
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 19
Numerical Operators
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 20
Variables
Names >>> b = 7
>>> c = 3
• Cannot start with a number [0-9] >>> a = b + c
• Cannot conflict with a language keyword >>> a
10
• Can contain: [A-Za-z0-9_-]
• Recommendations for naming (variables, >>> string_one = "Foo"
classes, functions, etc.) can be found in PEP8 >>> string_two = "Bar"
>>> new_string = string_one + string_two
Created with the = assignment operator >>> new_string
'FooBar'
Can see list of variables in the current
scope with dir()
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 21
Working with Strings
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 22
Basic I/O
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 23
Python Conditionals & Loops
Conditionals
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 25
Conditionals | Examples
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 26
Loops
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 27
Python Functions
Functions | Don’t Repeat Yourself
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
Python Libraries
Python Libraries
sample.py
Computing Hardware
© 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public
Importing and Using Packages & Modules
Syntax:
import module >>> response = requests.get('https://google.com')
>>> response.status_code
from module import thing 200
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 32
PIP Installs Packages
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 33
Learning Outlook
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 34
Continue your learning journey…
http://developer.cisco.com
• Programming fundamentals Learning Labs
https://developer.cisco.com/learning/modules/programming-fundamentals
• Learning Paths (Start Now)
https://developer.cisco.com/startnow/
• Reserve FREE Sandbox Environments
https://developer.cisco.com/site/sandbox/
DEVNET-1897 © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 35
Complete your
online session
survey • Please complete your session survey
after each session. Your feedback
is very important.
• Complete a minimum of 4 session
surveys and the Overall Conference
survey (starting on Thursday) to
receive your Cisco Live t-shirt.
• All surveys can be taken in the Cisco Events Mobile
App or by logging in to the Content Catalog
on ciscolive.com/emea.
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 36
Continue your education
Demos in the
Walk-In Labs
Cisco Showcase
DEVNET-1893.d © 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public 37