Python Tutorial: Functions and Passing Lists and Dictionaries with Simple Examples

Python Tutorial: Functions and Passing Lists and Dictionaries with Simple Examples

Python Tutorial

Python Tutorial: Functions, Passing Lists, Dictionaries with Simple Examples

In Python the differences between functions, classes and methods is often best explained with examples of code. It is also the best way to learn syntax and languages for some of us. We will start off with Functions and work into Classes which is the Object Oriented Programming (OOP) in Python over the next few posts. These are merely blueprints to run data through often times in an iterated fashion. Python is much like C in syntax minus declarations, in fact it, is written in C and interpreted in a C binary. This will be the first of a series of post that are a primer to the programming language Python. This Python tutorial will focus on functions with a refresher or start learning Python for beginners to look at lists, dictionaries / associative arrays.

Python Tutorial: Python Functions

A function is a start of code reusability in Python that we will later expand on with OOP concepts. We can call a function and pass or not pass arguments to the function.For anyone totally new to programming an example of an argument would be “ping 8.8.8.8”, 8.8.8.8 was an argument we passed to the program ping.

Python Tutorial: Function without any Arguments Example

Lets make a function and call it.

Python Tutorial: Function with a single Argument Example

Ok, that was easy enough. We made a function and called it with “sld()”, that executed “def sld()” or “called” it for execution. Next lets pass an argument to be executed in the function. This begins hitting on the reusability factor of function. We are setting up a framework or routine that can be called many times and pass different data values to it each time.

We will pass an argument to a variable in the function, in this case “name” will get the string “brent is the” as it’s value. We will then print the string in the function along with the argument we passed that is represented with the variable “name”. When you define arguments in this fashion, you have to pass as many as you define to the function or you will get a syntax error.

If you had not passed an argument to the definition but defined one you would get this type of error.

Python Tutorial: Function with Multiple Arguments Example

Now let’s pass more than one variable.

Python Tutorial: Python List Examples

Lets get a bit more flexible, lets pass a list. We will pass variable length arguments meaning, you can pass as many lists as you want. Quick refresher on lists, a list holds a number of objects inside of a sequence. A tuple is similar but the difference is a tuple cannot be modified while a list can. Example of a list.

The output would look like this:

Python Tutorial: Example Passing Variable Length Argument to a List in a Function

Now lets pass a list to a function notice I am using the built-in ‘str’ function to convert the list to a string in order to print the whole thing in the function.

Output:

Python Tutorial: Python Dictionaries

A Dictionary is like it sounds. It can take a keyword and associate it to a value or object. This is often referred to as an Associative Array. Since most reading this would be interested in infrastrcuture and/or networks a good example would be a mac address table entry. A BCAM (Binary Content Addressable Memory) lookup or search index would be an associative array. If we had two fields one being a port and the other being the MAC address learned on that port are associated with a dictionary entry could look like this:

In Python that would look like this:

Python Tutorial: Python Dictionaries and Functions
  • Let’s add a few functions. One will be a new MAC address was flooded on port Gigabit 3/2. We will pass the current state of the MAC addr tables and update it with the new entry on Gig3/2.
  • The next function will replace a MAC an existing association on Gig4/22 with the new one heard from flooding.

The output looks like this:

Going to wrap up there for this review of Lists, Dictionaries and how they interact with Functions. We will get into some Object Oriented Programming (OOP) in the next post.



Thanks for stopping by.


About the Author

Brent SalisburyI have over 20 years of experience wearing various hats from, network engineer, architect, ops and software engineer. More at Brent's LinkedInView all posts by Brent Salisbury →

  1. Ciprian TomoiagaCiprian Tomoiaga02-23-2013


    Again, a very nice and neat tutorial! I like them for their simplicity.
    Could I still make a small suggestion? It’s “OOP”, not “OOB” (like you said, from “Object Oriented Programming”)

  2. Brent SalisburyBrent Salisbury02-25-2013


    Hi Ciprian, Thanks for the nice comments and pointing out the typo, I was finishing a data center design and had “out of band” (OOB) on the brain lol.

    Respect,
    -Brent

  3. Saravana KumarSaravana Kumar03-13-2014


    Awesome, this is very clear for beginners like me. Thanks a lot Mr. Brent