#

Saturday, May 3, 2025

Functions in a programming language are defined to get rid of repeating set of codes in a script. Following link is the official place to find built in functions in python.


We can also use built in function help() to get the info about a specific function through CLI, just by sending the name of the function we need help with in the parenthesis.

Basics of Defining and Calling Functions 

Following is the basic syntax to define a custom / user defined function in Python.






It can be called just by the name followed by parenthesis at anywhere in the code.
If passing variables to a function while calling it is necessary, following is the way to define it and call it.

Note:- "a" and "b" are parameters and "5" and "3" are called arguments.





The result will be;




Another example to push string arguments will be;









The result will be,




You can also notice that there is a doc string within triple quotation marks """XYZ""" which is the comment which help someone to understand the code and this is the text which optouts if someone send this function through the help function means if someone calls it by help(customf), following will be the result;







Note:- Some people prefer to view the doc string by the following way,

This is basically the same information as help funtion.


Following syntax specifies how we can define a default argument value in a function, which comes to play like in the following examples.










The result will automatically add "gmail.com" as the 2nd argument.




Following syntax shows how you can mix the order of sending arguments to a function called "keyword arguments", basically the Positional Arguments can not come after Keyword Arguments. "user" is a positional argument and domain is a keyword argument here.









Result will be,



You can send multiple arguments in a list to a function like the following.

Here the "num_list" parameter takes in the argument as a list and sum up all together, no matter how many items are in the list.






The above can be scripted in the following way also,

By this method, we don't want to send the argument as a List as the parameter is coded to take any amount of arguments. They are called star args (unpacking operator *) in python.

This can't be used with keyword arguments just like the above way but you can do something like the following if you use ** double stars.








This takes the passed arguments as a dictionary (key value pairs) and unpacking with iterating using for loop to make the following output.





We can send any amount of kwargs (keyword arguments) as you want like this.

We can also combine above different methods of sending arguments but have to use the following order.




Returning values from a Function

This is where the values which comes as a result of running a function can be used for some other thing later in a code. By default, a function returns none, but if we assigned the result to a variable, we can use it later like the following,











In the above example, "return email" means returning the final output of the function which is assigned to a variable called "email" is returned back to where the function is called and it is stored in the variable called "my_email".

So the result will be;




Note that you cannot call the variable "email" outside the function as it is cleared after running the function once.

Following example shows how to return multiple values from a funtion.











Scopes of Variables

In Python, there are 4 Scopes.

1. Built-in
2. Global
3. Local
4. Enclosing

Built-in variables are predefined keyword kind of variables which can be accessed from anywhere.
Global Scope is defined outside of any function so that it can be accessed from anywhere in the code.
Local is defined inside a function and can only be accessed within that function.
Enclosing is defined at parent functions which can be accessed by the child functions (functions within functions)

"gname" is a globally defined variable, "ename" is a enclosed variable and a local variable to function named brand() and "lname" is a locally defined variable inside the function nbrand() and nbrand() is a function inside brand().

If this script is run, the output will be like the following..

cisco
meraki
huawei
cisco

Tuesday, April 1, 2025

This post is about the most common / useful things we do with Sets in Python.
It is better if you see this as the Sets which we worked with Venn Diagrams in old day schools. This is the same thing..

 





Adding & Removing

To add an item to a Set, we can use add() function and we can use remove() to remove an item.











Note that we can use pop() function but it should be used with out a value when dealing with Sets and it will remove a random item from the Set.

Union

We can merge 2 sets into 1 with all the values. We can assign a new variable and assign the all values to new Set of all values.







Intersection

This gives the items which are common to both sets.







Difference

This is just the minus the 2nd Set from the 1st Set.


 






This can also be achieved by just arithmetic (-)



Monday, March 31, 2025

This post is about the most common / useful things we do with Dictionaries in Python.

Adding & Removing Keys

Following is the syntax to add new Keys to a Dictionary







Removing can be done with 2 ways each way has its own use case. 1st one is using del keyword which will just remove the key with it's value.








Second method is to use pop() function. Since this method returns a value, they can be assigned to a variable to store the deleted values for later reference if needed.










Keys, Values & Items Functions

Using keys() function against a Dictionaries will get all keys out and can be assigned to a List like the following.








values() function do the same thing for values..








items() function can be used to get all key-value pairs as Tuples inside a List..


 





Iterating over Dictionaries

We can run loops over through the keys and values or both in a Dictionary using the above mentioned functions just like in Lists and they are very handy in Network Engineering related work.







Above will result the following..






You can run functions like sorted() along with the other functions above like the following if needed..






Similarly values() function is also working opting the values out of a Dictionary.
Following example shows how items() function is used to opt out all keys and values in a Dictionary.







Will result:-





Unpacking Dictionaries

You can merge 2 or more Dictionaries in to one Dictionary by this.









But you can't do this if the keys are duplicated. It will replace the values.

Nesting Dictionaries in Lists

Dictionaries can be nested inside Lists. Since the Keys cannot be duplicated, we can use 2 or more Dictionaries inside Lists to store data.






Sunday, March 30, 2025

This post is about the most common / useful things we do with Lists in Python.

Iterating over Lists

Most of the time we use FOR loops to iterate over Lists as it can do things for all the items in a List easily.
As an example, printing all values in a List can be done by the following code.







The result will be;







Another example will be to find the data types of the items in a List like the following,






The result will be;







Indexing, Counting & Slicing

This way we can target the items in a List instead of looping for all items.

You can see that the indexing starts with 0 and if given a negative value, it will start reading from the end.

0th item is "vlan5" and -1th item is "vlan15".



Len() function will tell you how many items are there..





count() function will tell you how many items are there of a specific value.






We can slice the List like the following,

list[X,Y] 
As you can see, Xth item is included and Yth item is excluded.




How about the following,

This means that the slicing starts with the 2nd item (note that 2nd item means actually the 3rd item as it starts from 0) and ends with 6th item (7th) but it will only capture with a gap of 1 (every second item)

Min & Max Functions

These functions help us to get the minimum and maximum values in the List items depending on the data type. As an example, if the List is full of numbers;








Let's see what happens when they are Strings..








It did not captured the item with the minimum / maximum number of characters instead it captured the lowest and highest ASCII character in order. "b" comes 1st than "i" or "l".






You can see as the Capital Letters comes 1st, it captured "Indonesia" this time. Let's see what happens if the numbers are put in string format.






But note that you cannot use min and max functions when the data types are mixed.

Appending, Inserting and Extending

append() function adds an item to the end of the List while insert() function inserts an item to a position you want and extend() function adds multiple items to the List. But there is a rule in using extend() function and that is you have to use an item which can be iterated means which can be run through a loop like FOR loop. Examples are another list or an String..









As you can see it adds the items to the end, let's see what will happen if we try to add an item to the list directly which means we try to use extend() function like the append() function.







It was taken as a String..

Let's see a real world example using the things so far..




Above program inspects all items in the list "ip_addresses" and put in to the list "public_ips" if those items does not contain the characters "192.168".

Popping, Removing and Clearing

pop() function can be used to remove an item from a List targeting an exact position of an item. However if the position is not specified, it will remove the last item of the list.















remove() function will be useful when you need to remove an item by the value. But it if duplicate values are there, it will only remove the 1st item it finds in the list.













Sorting Lists

sort() function comes handy when you needs to sort the values of the items in a List. This just works like the min() and max () functions as if all the items are numbers, it will sort normally and if the items are strings, it will again do the sorting based on the ASCII order.












Nested Lists

We can put a List inside of another List.








You can access the individual values of the nested List like the following..






Note:-

Since Tuples are very much same as Lists, but being immutable, some of the above functions like min(), max(), len(), count() also works with Tuples.