Python // Lesson 4: Functions and the Python Debugger

In this email of Learning Python we are going to cover the following:

  1. Functions (Part1)
    Video link
    Length is 8 minute
  2. Functions (Part2)
    Video link
    Length is 11 minutes
  3. Misc Topics (Part1)
    Video link
    Length is 10 minutes
  4. Misc Topics (Part2)
    Video link
    Length is 8 minutes
  5. Python Debugger (pdb)
    Video link
    Length is 10 minutes

Additional Content:

A Byte of Python, Functions

How to use the Python Debugger


Reference code for these exercises is posted on GitHub at:

1a. Create an ssh_conn function. This function should have three parameters: ip_addr, username, and password. The function should print out each of these three variables and clearly indicate which variable it is printing out.

Call this ssh_conn function using entirely positional arguments.

Call this ssh_conn function using entirely named arguments.

Call this ssh_conn function using a mix of positional and named arguments.

1b. Expand on the ssh_conn function from exercise1 except add a fourth parameter ‘device_type’ with a default value of ‘cisco_ios’. Print all four of the function variables out as part of the function’s execution.

Call the ‘ssh_conn2’ function both with and without specifying the device_type

Create a dictionary that maps to the function’s parameters. Call this ssh_conn2 function using the **kwargs technique.

2.  Create a function that randomly generates an IP address for a network. The default base network should be ‘10.10.10.’. For simplicity the network will always be a /24.

You should be able to pass a different base network into your function as an argument.

Randomly pick a number between 1 and 254 for the last octet and return the full IP address.

You can use the following to randomly generate the last octet:

import random
random.randint(1, 254)

Call your function using no arguments.
Call your function using a positional argument.
Call your function using a named argument.

For each function call print the returned IP address to the screen.

3. Similar to lesson3, exercise4 write a function that normalizes a MAC address to the following format:


This function should handle the lower-case to upper-case conversion.

It should also handle converting from ‘0000.aaaa.bbbb’ and from ’00-00-aa-aa-bb-bb’ formats.

The function should have one parameter, the mac_address. It should return the normalized MAC address

Single digit bytes should be zero-padded to two digits. In other words, this:


should be converted to:


Write several test cases for your function and verify it is working properly.

4. Copy your solution from exercise3 to exercise4. Add an ‘import pdb’ and pdb.set_trace() statement outside of your function (i.e. where you have your function calls).

Inside of pdb, experiment with:

  • Listing your code.
  • Using ‘next’ and ‘step’ to walk through your code. Make sure you understand the difference between next and step.
  • Experiment with ‘up’ and ‘down’ to move up and down the code stack.
  • Use p <variable> to inspect a variable.
  • Set a breakpoint and run your code to the breakpoint.
  • Use ‘!command’ to change a variable (for example !new_mac = [])