#

Tuesday, February 18, 2025

There is a function in Python called "Bool" function, which we can use to check the "True" or "False" status of a variable. What it actually does is that it checks whether the variable is empty itself or not.

You can clearly see that logic from following snippet.

String1 consists of some characters (aka not empty) hence the bool function shows the output as true but since String2 is empty, it opts out false.

No matter the variable type, this is how it behaves, meaning it can be a List, Tuple, Dict or Set, it is like this.

Apart of bool function, there are Logical Operators / Gates which contributes to Boolean logic.

Python has following 3 logical operator types.



AND

This checks whether all the values are true or not.
Following example code shows how this logic works,










Since both values are true, the output will be like the following.




When I change the version1 to be 12 or something it gets the other output.

OR

Let's say If I mistakenly used OR operator for the above example  and change the version1 to be 12 like the following, which will help us understand how it works,










The output will be like the following,




It means that, if one of the items are true, that's enough to make the operator opt outs true.

NOT

Let's see the following practical example to understand this logic.









This means if I enter a range of IP addresses, it will check against the values in the List and opt out the corresponding output. 







Now by adding the NOT operator, we can negate the answers, which will obviously make no sense but for the sake of understanding how it works, lets try it.





Thursday, February 6, 2025

Let's go to examples straight away to understand the syntax and logic behind the control flows.

Conditional Statements If / Elif / Else











Line 1 gets the user input as a String and since it should be an integer, it is converted in line 5 and assigned it to another variable.
Line 7 starts the conditional statement saying if the value is larger than 12, Line 8 says to display "Your router is upto date" and
Line 10 says if it is equal to 12, and Line 11 says to display "Your router is old and upgrade"
Line 12 and 13 says "Your router is very old"

This is the most basic form of control flow, we can put much elif statements as we want in between if and else. Else is the default action if none of the above statements work and elif and else statements are optional. Since code runs from top to bottom, if it hits a matching condition, it stops there ignoring all the elifs of else below it.

Let's see the outputs it can give,








Thing to remember is the indentation, Line 8 starts after 4 spaces.. Anyhow if you are using and IDE like VS Code like I do, it will do it automatically.
For Loops

Loops are there to do repetitive tasks.

Line 3 defines a List of devices.
Line 5 and 6 says, for all items in devices, print "There is a router named < > ". 


So the output will be like the following..






"items" in Line 5 is another variable (it can be anything like x or y or something..) which represents each item in the List type variable named "devices" in Line 3. The loop runs until items in the list exhausts. 
Variable type used in this example is a List, but it can be anything including a String also. In case where a String is used, it will run for all the characters in the String..
For Loops with Continue Operator

This is how you can skip some items if you don't want to print in above type of example.







It says if the item name is "R2", skip it, so the output will be





Note that in Line 6, I have used an equal mark, but there are some functions which can be used to address some other requirements as well. As an example, if I wrote the Line 6 as;

if items.startswith("R")

It will skip all items starts with R.

Nested For Loops

A For Loop can be inserted into another For Loop to get a desired out put like the following.








This will run for all the items in List "description" per every item in List "interface_list" like the following..








For Loops with Range Function

Range function can be used in for sequential tasks easily with For Loop.

It says to print "ping 192.168.1.1" until 192.168.1.4, let's see the output.






Note that it does not print 192.168.1.5; 1st number in range parentheses is inclusive and the last number is exclusive.

You can use a third number in the parentheses to add a step like in the following..

Output will be counted only for odd number..




For Loops are used in cases do a repetitive action for items in a variable making it a finite type of loop mostly.    

While Loops
 
If need to run a loop until a condition is met, have to use While Loops, perhaps it can run infinitely if the condition is not met.

Line 5 and 6 says to print the text as "less than 10" if it is less than 10 starting from original value and Line 8 increments the value of numb with a 1.
Output will be like the following..






While Loops with Break Operator

This is how you can exit from the While Loop before the condition is met.










Line 9 says to break (exit the loop) if numb is 7,


Wednesday, February 5, 2025

Strings are a Data Type in Python which is nothing but an ordered sequence of characters. They are also immutable which means they cannot be altered, you can only replace them completely.

We can use letters, numbers, special characters like underscore / dash etc or even spaces as a string. 

We can use both single quotation marks or double quotation marks to get same output. When you use three double quotation marks, you can get some interesting thing done.

As an example, let's say we want to push the following commands to a router.

router ospf 1
 network 10.0.0.0 0.0.0.255 area 0
 network 10.0.1.0 0.0.0.255 area 1

We can use the following method with \n in strings to separate lines and send "enter" key effect.

You can use triple quotation without \n to get the same output.








After it is compiled, following will be the output; what happens actually is it inserts \n automatically.






Converting/Casting Strings to Integers

There are some cases we need to convert between data types like strings to integers. As an example, we may get some information like IOS version number from a show version output which comes as a text  and will need to get into our program as an integer to run a comparison or do mathematical stuff with it. We can do this by converting string into integer by following way using some special functions.











Following is how to convert between strings, ints or floats using some other functions.

What should we keep in mind is that, it is possible.










Concatenation

This allows us to modify a String with adding some strings to it.
Following is an example.







Output will be;





It is a simple script which asks for a command to enter and adds enable, enter and the entered command ending with another enter. The + sign is the key of concatenation but all should be strings for this to work. Note that input function is getting the things entered by user as a String.

Formatting Strings

This does concatenation, changing Ints or Floats to String and all.
Let's see the following example.






When this piece of code is run, following will be the output,





It will ask for the hostname and version, and it will display it all as a String.

Following were some older ways of doing formatting,









Both the above methods are no longer used but can be seen on older written scripts.

Saturday, February 1, 2025

Variables are temporary locations in memory. 
Just like very other programming language, Python also support several variable types. 
Also Python is considered as a Dynamically Typed language, which means we don't want to define the type of the variable before assigning values to it, it does it automatically when we assign value to it.

You can use type() function to check the type of the variable and I used Ipython to get the left side snippet.


Naming Conventions
You can use Simple Letters, Capital Letters, Numbers and Underscore; but you can't start with a number.
Also try not to use mix of simple and capital letters to name a variable as it is generally accepted as a bad practice even though it is possible.

Data Types

Following are the Data Types which can be put into variables which makes the types of variables.


Data Types in red box are mutable and others are immutable. Mutable means can change values without changing the object id (the real memory slot) and immutable cannot change and need to replace and recreate the object to change it.




Strings
Str aka String type is highly used by us to store the text values like "show ip int br" or may be "show ip route". It can just include characters spaces etc. Also the numbers if it is with characters. An example value would be "router10".
You can use double quotes or single quotes to assign vales.

Integers
Int are integers which means regular numbers. It is just like a String without quotation marks and the value is just a number. 

Float
float is where the numbers have with a decimal point.
As an example, 10.0 can be mathematically identified as a integer but since there is a decimal dot, Python identifies it as a float.






Lists
Starts with square brackets.
List  is set of ordered and mutable objects. Ordered means the order of the objects matter and mutable means you can change values.






If need to get the 1st value / 2nd value of the list,






You can use append() function to add a new value to the list,






Tuples
Starts with round brackets.
Tuple is very much like a List but it is immutable means you cannot change the values.





If you want to add something, you can recreate it again using the same values and added values.









You can use id() function to check the object id of the variable when it was created at the backend, note that this will change completely when a new value is replacing the old value of the variable which means it becomes a whole new object at the backend.

Dictionaries
Starts with curly brackets.
Dict means dictionary. Dictionaries are type of variables which contains unordered key - value pairs.

Above you can see how to retrieve a value assigned to a key also.
Since this is mutable, we can add a new key - value pair like the following.

But what if we try to change a value of a already created key like the following..

It doesn't work like that instead it adds a new key - value pair.

Sets
Starts with curly brackets but no key-value pairs.
Set is a mutable collection of unordered objects. You cannot have duplicate items in a Set.






If you noticed, I added "vlan 5" twice but it only includes one of them as it cannot have duplicate entries.
Can add a value using add() function like the following.






Also you can notice that the order didn't matter and that's why we could use add() function instead of append() function. You can also remove an item using remove() function.

Booleans
Bool is just variable with a logical value, either true or false.

Note:-
Strings, Integers amd Floats can consider as immutable.








Another point is that, even though Python is a dynamic language, incase if you need to specify the data type of a variable it can be done and it's called "Casting".

Ex:- x should be '5', the character only, not the numeric value.
x = str(5)