Session 1
Session 1
Python
Ahmed Elaraby
Ahmed Elaraby 1
Network Operational Problems
• Daily repetitive tasks.
• Infrastructure scaling.
• Network Engineers no longer need to learn all the different syntax from diverse
vendors, codes can do that !
• Scalability of networks, adding new operational nodes to the networks in just few
clicks.
Ahmed Elaraby 3
Why learn Programming
Ahmed Elaraby 4
Programming in Networks
• Network Automation.
Ahmed Elaraby 5
• Two forms of network programmability :
• On-box
• Off-box
• Automation
• Well specified task run on its own.
• Orchestration
• Automating a lot of things at once.
• Multiple tasks to execute a workflow.
• Is automation, coordination and management.
Ahmed Elaraby 8
Languages For Automation
• Strong binding between Network Automation Tools and Programming
languages.
Ahmed Elaraby 9
Which Programming Language should we learn
Ahmed Elaraby 10
Type of Languages
Ahmed Elaraby 11
Why Python
• Interpreted
• Object-Oriented
• Extensible–easily import other code
• Embeddable–easily place your code in non-python programs
• Extensive libraries
• Simple
• Easy to Learn
• Free & Open source
• High Level Language –memory management
Ahmed Elaraby 12
Getting started with Python
• Usually a system may have one of the 3 famous Operating System.
• Microsoft Windows
• Apple Mac OS
• Linux Distribution OS
• Linux
• $sudo apt-get install python3
• Windows
Ahmed Elaraby 14
What is PIP
• PIP is a package manager for Python packages, or modules.
• You use PIP to install and update packages and modules as per required by your
python code.
• If you do not have PIP installed, you can download and install it from,
• https://pypi.org/project/pip/
Ahmed Elaraby 16
Deeper into Python
Ahmed Elaraby 17
Python Comments
• Comments can be used to explain Python code.
• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.
• you can add a multiline string (triple quotes) in your code, and place your
comment inside it:
• """
• This is a comment
• written in
• more than just one line
• """
Ahmed Elaraby 18
Variables
• A variable is a named place in the memory where a programmer can
store data and later retrieve the data using the variable “name”.
Ahmed Elaraby 19
Constants
• Fixed values such as numbers, letters, and strings are called “constants” -
because their value does not change.
Ahmed Elaraby 20
Data Types
• In Python variables and constants have a “data type”.
• In Python variables are “dynamically” typed. In some other languages you have to
explicitly declare the type before you use the variable.
• In C/C++:
• Int a;
• float b;
• a=5
• b = 0.43
• In Python:
• a=5
• a = “Hello”
• a = [ 5, 2, 1]
Ahmed Elaraby 21
Some Data Types in Python
• Tuple (Example: ( 4, 2, 7, 3) )
Ahmed Elaraby 22
String Data Type
• A string is a sequence of characters.
Ahmed Elaraby 23
Looking Inside Strings
• We can get at any single character in a string using an index specified in square brackets.
Ahmed Elaraby 24
Slicing Strings
• We can also look at any continuous section of a string using a colon operator.
• The second number is one beyond the end of the slice -“up to but not including”.
• If the second number is beyond the end of the string, it stops at the end.
• If we leave off the first number or the last number of the slice, it is assumed to be the beginning or end of
the string respectively.
• >>> a = 'Hello‘
• >>> b = a + 'There‘
• >>> print b
• HelloThere
Ahmed Elaraby 26
Handling User Input
• We prefer to read data in using strings and then parse and convert the data as we need.
• This gives us more control over error situations and/or bad user input.
• Int()
• Float()
• Str()
Ahmed Elaraby 28
Python Collections (Arrays)
• There are four collection data types in the Python programming language :
• When choosing a collection type, it is useful to understand the properties of that type
Choosing the right type for a particular data set could mean retention of meaning, and, it
could mean an increase in efficiency or security
Ahmed Elaraby 29
Lists
• A list is a collection which is ordered and changeable In Python lists are written with square
brackets.
• Lists are created using square brackets:
• thislist = ["apple", "banana", "cherry", "apple", "cherry"]
• print( thislist )
• ['apple', 'banana', 'cherry', 'apple', 'cherry']
• You can specify a range of indexes by specifying where to start and where to end the range.
• print( thislist[1:4]) >>> ['banana', 'cherry', 'apple‘]
• When specifying a range, the return value will be a new list with the specified items.
Ahmed Elaraby 30
• List Length : To determine how many items a list has, use the len() function.
• Print( len(thislist)) >>> 5
• From Python's perspective, lists are defined as objects with the data type 'list':
• print( type( mylist)) >>> <class 'list'>
• It is also possible to use the list() constructor when creating a new list.
• thislist = list(("apple", "banana", "cherry"))
• print( thislist)
• ['apple', 'banana', 'cherry']
Ahmed Elaraby 31
• To determine if a specified item is present in a list use the in keyword:
• thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
>>> Yes, 'apple' is in the fruits list
• To add an item to the end of the list, use the append() method:
• thislist.append("orange")
>>> ['apple', 'banana', 'cherry', 'orange']
• To append elements from another list to the current list, use the extend()
method :
• tropical = ["mango", "pineapple", "papaya"]
• thislist.extend(tropical)
>>> ['apple', 'banana', 'cherry', 'mango', 'pineapple', 'papaya']
Ahmed Elaraby 33
• The remove() method removes the specified item :
• thislist = ["apple", "banana", "cherry"]
• thislist.remove("banana")
• print(thislist)
>>> ['apple', 'cherry']
• If you do not specify the index, the pop() method removes the last item.
• The del keyword also removes the specified index:
• del thislist[0] >>> ['banana', 'cherry']
• You can also loop through the list items by referring to their index number.
• for i in range(len(thislist)):
print(thislist[i])
• You can loop through the list items by using a while loop.
• i=0
• while i < len(thislist):
print(thislist[i])
i=i+1
• List Comprehension offers the shortest syntax for looping through lists:
• [print(x) for x in thislist]
Ahmed Elaraby 35
List Comprehension
• List comprehension offers a shorter syntax when you want to create a new list
based on the values of an existing list.
• fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
• newlist = [x for x in fruits if "a" in x]
• print(newlist)
>>> ['apple', 'banana', 'mango']
• The reverse() method reverses the current sorting order of the elements.
• thislist.reverse()
>>> ['cherry', 'Kiwi', 'Orange', 'banana']
Ahmed Elaraby 37
• There are ways to make a copy, one way is to use the built-in List
method copy().
• thislist = ["apple", "banana", "cherry"]
• mylist = thislist.copy()
• print(mylist)
>>> ['apple', 'banana', 'cherry']
Ahmed Elaraby 38
• There are several ways to join, or concatenate, two or more lists in Python.
• One of the easiest ways are by using the + operator.
• list1 = ["a", "b", "c"]
• list2 = [1, 2, 3]
• list3 = list1 + list2
• print(list3)
>>> ['a', 'b', 'c', 1, 2, 3]
• Another way to join two lists is by appending all the items from list2 into list1,
one by one:
• for x in list2:
list1.append(x)
• Or you can use the extend() method, which purpose is to add elements from
one list to another list:
• list1.extend(list2)
Ahmed Elaraby 39
List Methods
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Ahmed Elaraby 40