Basic Python
Basic Python
Basic Python
14.5 Python
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 2
1
1/8/2024
Agenda
• Why Python?
• Basic Python Syntax
• Collections and Loops
• Script Structure and Execution
Why Python?
• Domain Applicability
Established online DevOps Community
• Platform Flexibility
Run Your Code: Laptop, Server, VM, Container, Cloud, Cisco IOS Device
2
1/8/2024
Python Scripts
Text Files (UTF-8)
May contain Unicode
Some editors / terminals
don’t support Unicode
Use any Text Editor
Using a Python- aware editor
will make your life better
No Need to Compile Them
3
1/8/2024
4
1/8/2024
• >>> type(3)
<class ‘int’>
Basic Data Types
Python Values • >>> type(1.4)
type() (examples) • <class ‘float’>
int -128, 0, 42
str “Hello 😎”
Can use ‘’, “”, and “””””” • >>> type("Hello")
<class ’str’>
10
5
1/8/2024
Numerical Operators
>>> 5 + 2
Math Operations 7
Addition: + >>> 9 * 12
108
Subtraction: - >>> 13 / 4
* 3.25
Multiplication: >>> 13 // 4
Division: / 3
>>> 13 % 4
Floor Division: // 1
Modulo: % >>> 2 ** 10
1024
Power: **
11
Variables
>>> b = 7
Names >>> c = 3
• Cannot start with a number [0-9] >>> a = b + c
>>> a
• Cannot conflict with a language keyword
10
• Can contain: [A- Za- z0- 9 _ - ]
• Recommendations for naming (variables, >>> string_one = "Foo"
classes, functions, etc.) can be found in >>> string_two = "Bar"
PEP8 >>> new_string = string_one + string_two
>>> new_string
Created with the = assignment 'FooBar'
operator
Can see list of variables in the
current scope with dir()
12
6
1/8/2024
Terminology
When contained inside an object, we call…
Variable Attribute
Function Method
13
14
7
1/8/2024
Basic I/O
Get Input with input() Display Output with print()
• Pass it a prompt string • Can pass multiple values
• It will return the user’s input as a string • It will concatenate those values with
• You can convert the returned string to the separators in between (default = spaces)
data type you need int(), float(), etc. • It will add (by default) a newline (‘\n’) to
the end
15
Conditionals
Syntax: • Comparison Operators:
if expression1:
statements… • Less than <
elif expression2: • Greater than >
statements…
else: • Less than or equal to <=
statements… • Greater than or equal to >=
• Equal ==
Indentation is important!
• Not Equal !=
4 spaces indent recommended • Contains element in
You can nest if statements • Combine expressions with:
and,
or
• Negate with: not
16
8
1/8/2024
Conditionals | Examples
>>> b = 5 >>> words = "Foo Bar"
>>> if b < 0: >>> if "Bar" in words:
... print("words contains 'Bar'")
... print("b is less than zero")
... elif "Foo” in words:
... elif b == 0:
... print("words contains 'Foo'")
... print("b is exactly zero")
...
... elif b > 0:
words contains 'Bar'
... print("b is greater than zero")
... else:
... print("b is something else")
...
b is greater than zero
17
18
9
1/8/2024
19
20
10
1/8/2024
tuple t = (‘a’, 1, 18.2) >>> t[0] You cannot update tuples after they have been
‘a’ created.
21
Dictionary Methods
>>> d = {"a": 1, "b": 2, "c": 3}
Some useful dictionary methods:
>>> d.items()
{}.items() dict_items([('a’,1), ('b’,2), ('c',3)])
22
11
1/8/2024
Loops
Iterative Loops Conditional Loops
for individual_item in iterator: while logical_expression:
statements… statements…
>>> names = ["chris", "iftach", "jay"] >>> i = 0
>>> for name in names: >>> while True:
... print(name) ... print(i)
... ... i += 1
chris ...
iftach 0
1
jay
2
3
4
23
24
12
1/8/2024
25
26
13
1/8/2024
Debugging Basics
• Add print() statements
Comment and uncomment them to “enable and disable your debugging”
• Understand how to read a Python Stake Trace
1. Last Line First
2. Top to Bottom
3. Stop when you reach someone else’s code
27
28
14
1/8/2024
29
15