More Python Basics

In this Learning Lab we will learn about Python data types, loops and functions.


Completion Time: 45 minutes

  • Understand and use Python data types
  • Learn about and implement looping
  • Create and call functions




  • To run the code samples, you need to have Python 3 installed on your machine.
  • If you are working on a DevNet Learning Lab PC at a DevNet event, Python 3.x + is already installed.
  • See How to Set up Your Own Computer section above for how to install Python on your own machine.

Clone Git Repo

  • If you are working on a DevNet Learning Lab PC at a DevNet event,
    • Open the Git Command window by clicking on the Git CMD icon on the Task Bar or click on the Start button, then in the Run bar type: git cmd and press the Enter key.
  • If you are working from your own workstation, on your desktop open a command terminal.
  • Go to the root directory by typing: cd \
  • Create a directory called 'C:\DevNetCode\yourname' by typing: mkdir DevNetCode\<your-name>
    • For example: mkdir DevNetCode\brTiller
  • Go to the new directory by typing: cd \DevNetCode\<your-name>
    • For example: cd \DevNetCode\brTiller
  • Clone the devnet-express-code-samples repository from GitHub. Enter the command below.
    git clone

    Inside the directory you created you should now see directory 'devnet-express-code-samples'.

Step 1. Python Data Types

Learning the Data Types

In this step we'll explore Python data types. There are many data types listed in the figure below. There are simple numeric ones like integers and floats. For our purposes the differences between a float and an integer is that a float has a decimal in it. For example 52.856 is a float while 52 is an integer. Boolean data types can only be either True or False. Text is a data type that is shrouded in quotes "text" and can be alphanumeric. Text is more commonly called string. For example: "brett" is a string, so is 'brett123', but 123 is an integer.

There are also more complex data types such as lists, tuples and dictionaries. Let's explore these types.

A list contains any number of sequential elements and is defined by square brackets []. Here's a list ['Martha','Betty',5] which contains strings and an integer. Assigning a list to a variable is as simple as var=['Martha','Betty',5]. Lists are mutable which means that they can be changed. You can add data to a list, modify, delete it or even sort it. Notice that lists can have different data types inside them.

A tuple contains any number of sequential elements and is defined by parenthesis (). Here's a tuple ('Brett',9,'Cisco'). Assigning a tuple to a variable is simple: var1=('Brett',9,'Cisco') which contains strings and an integer. Tuples are similar to lists, but they are immutable which means they cannot be changed. You might use a tuple to group similar data. For example, referring to the tuple defined above the similarity is that my name is Brett and I've worked at Cisco for 9 years. Because tuples are immutable they are more efficient meaning that their data can be accessed more quickly than lists.

While lists and tuples are defined a little differently, they both have their data accessed the same manner which is by square brackets []. The first data element always starts at position zero. In the example in the list that was defined earlier, var=['Martha','Betty',5], if I entered the python statement print(var[0]), it would display Martha. If I entered the python statement print(var[1]), it would display Betty and so on. The same syntax applies to tuples. For the tuple defined above, var1=('Brett',9,'Cisco'), if I entered the python statement print(var1[0]), it would display Brett. If I entered the python statement print(var1[1]), it would display 9 and so on

A dictionary is a different than lists and tuples. Each element in a dictionary must contain a key followed by a value. This key value association is typically referred to as name-value pairs. Dictionaries are defined by curly braces {}. Here's a dictionary {"car":"corvette","age":7, "food":"pizza"} . Assigning a dictionary to a variable is simple: myvar={"car":"corvette","age":7, "food":"pizza"} . The value in a dictionary is accessed by its key. For example if I entered the python statement print(myvar["car"]), it would display corvette. If I entered the python statement print(myvar["food"]), it would display pizza and so on. Dictionaries are sometimes also called maps or associative arrays .

Examples of how to define each Python data type is shown in the table below.

Here's some example code showing how the various data types mentioned could be created and accessed.

val = 25
fval = 25.14342
str = "Hey There!"
str2 = 'Ho There!'
flag = True

my_list = ["Chocolate", "Strawberry", "Pistachao"]
my_tuple = ("Sprinkled","Glazed","Maple")
my_map = {"donut":"glazed", "ice cream":"chocolate"}

print (val)
print (fval)
print (str)
print (str2)
print (flag)

print ("My favorite ice cream flavor is " + my_list[0])
print ("My favorite donut is " + my_tuple[2])
print ("My favorite dessert is " + my_map["ice cream"] + " ice cream")

Give it a try!

  1. Open a terminal and go to the directory created back in the prerequisites section Step 1 named: DevNetCode\<your-name>
  2. Go to directory module00\00-prep-04-python-primer2. In the terminal type: cd \DevNetCode\<your-name>\devnet-express-code-samples\module00\00-prep-04-python-primer2 where <your-name> is the name you gave the directory.

Run and Review Python Script

  1. Run the python script called and check the results.
  2. Modify the file to create errors. For example, change the code my_map["ice cream"] to my_map["cream"] .
  3. Save the file, run the script. What error is displayed?
  4. Try various changes to see what errors occur and make corrections as needed.

Create and Access Data types

  1. Create a file called .
  2. Write python code that does the following:
    a. Add a list with two elements and assign it to a variable.
    b. Add a tuple with three elements and assign it to a variable.
    c. Add a dictionary with two name value pairs and assign it to a variable..
    d. Print one value from each data type you created.
  3. Save the file, run the script and check your code!
  4. After you've completed the assignment compare your solution to file

Next Step: Learn about Nested Data Types