Python Basics

Operators : 
=
+=
*=
!=
<
>
<=
>=
==
%=
**=
//=
IN
NOT IN
AND
OR
XOR
NOT

Numbers :
Integer : Number without any decimal
Float : Number with decimal
Complex : Number with a letter

Variables :
“var” = Numbers/String/List/Tuple/Dictionary

Functions :
type(Numbers/Data Type/etc…)
input()
print()
append()
pop()
remove()
len()
min()
max()
list()
split() list = “1,2,3” ##then## list2 = list.split(“,”) ##=## [‘1’, ‘2’, ‘3’]

Strings :

define by a single or double quote ‘hello’ or “hello” you can use :
“”” triple quote
to right on different
lines”””

String can also be concatenate with +/*-
String and an Integer cannot be concatenated, you can buy buy transforming a integer by a string.
example : “hello world” + “100” <– this wont work unless you type “hello world” + str(100)

String Formatting :
a = bgp
“%s is a routing protocol” % a = bgp is a routing protocol

Lists :
List are define with [] brackets and define each item from 0+, list are mutable
example : a = [“string”or numbers or both]
to access a list :
example a[0] with equal to the 1st item of the list. [-1] will equal to the last item of the list.
you can also convert an item into a list :
example : list(hello) this will become [‘h’, ‘e’, “l”, “l”, “o”]

Tuples :
Tuple are the same thing as a list exept they are immutable.

Dictionary :
Dictionary are define with {} curly brackets they can store list or tuples and are define by key value
example : Dict1 = {“as”:”65353″, “as2″:”65354” }
example : Dict2 = {“dc1”: [“172.16.1.1″,”172.16.2.1”], “dc2”: [“172.16.3.1″,”172.16.4.1”]}

Nested Dictionary :

DICT_DCS = {
“DC1_ITEM1” : {
“location” : “ARIZONA”,
“AS” : “65353”
},
“DC2_ITEM2” : {
“location” : “MIAMI”,
“AS” : “65354”
}
}

To access an item in a dictionary you need to use :
example : dict[“item”]

Modules :
Creating a Module :
def module1(deviceX)
print(“this %s is part of the module” % deviceX)

By creating module you can import a code that have been already done, example network credidential and connection code for devices will always almost be the same.
To import a module :
import moduleX
you can import a module as another name :
import moduleX as moduleXX

Conditions :
If = match a condition
elif = or another condition
else = when there is no match

While loops :
For loops :  we can go through items and execute a code for each of them.
example :
var1 = [“1”, “2”, “3”, “4”, ]
for vars in var1:
print(vars)
1
2
3
4

Leave a Comment