Lab 06 - Working with Booleans¶
In this lab, you will be working in the Python interactive interpreter with the "bool" data type, exploring its syntax and functionality.
- Lab 06 - Working with Booleans
- Task 1 - Evaluate Logical Expressions
- Task 2 - Convert Other Types to Boolean
Task 1 - Evaluate Logical Expressions¶
You've already seen booleans after learning and using string methods such as startswith and isdigit. This lab will introduce more concepts as they relate to working with boolean values, expressions, and operators.
Step 1-1¶
Create a new variable called is_layer3 and assign it the value of True (do not use quotes):
If you use quotes, it will be a string.
Step 1-2¶
Perform a type check on the variable:
Step 1-3¶
Create another variable called needs_bgp and assign it the value of False. Then print it out.
Note that True and False using capital T and F without quotes are the only two boolean values. Every object in Python always evaluates to either True or False.
Let's take a look at a few boolean expressions.
Step 1-4¶
Create 3 variables called hostname, vendor and interfaces.
>>> hostname = 'nxos-spine1'
>>> vendor = 'cisco'
>>> interfaces = ['Ethernet2/1', 'Ethernet2/2', 'Ethernet2/3']
>>>
We will use these to practice using boolean operators.
Step 1-5¶
Use the == operator to evaluate hostname with the nxos-spine2 string and vendor with the cisco string.
The == operator is used to see if one object is equal to another object.
Step 1-6¶
Use the > (greater than) and != (not equal) operators to evaluate interfaces's list length with the 3 number.
Step 1-7¶
Note that != says "does not equal", so you can also use this for the hostname check too:
Step 1-8¶
Now use the in "containment" keyword to evaluate if Ethernet2/4 is a defined interface. You're checking to see if 'Ethernet2/4' is in the interfaces list:
In short, this is checking to see if an element exists in a list.
Step 1-9¶
You can also see if a sub-string exists in a string:
This checked to see if the string "Eth" exists in the larger string "Ethernet2/4".
Note that it's case-sensitive:
Step 1-10¶
It's often good practice to normalize (if possible) before doing the comparison or logic check.
Step 1-11¶
Use the and operator to evaluate if Ethernet2/2 is a defined interface and vendor equals to cisco.
When using and, everything must be True for the expression to evaluate to True.
You can do a quick test to prove this:
Step 1-12¶
When you use or, only ONE element has to be True for the expression to be True:
Task 2 - Convert Other Types to Boolean¶
In this task, we'll explore the boolean value of a single variable. This will be used heavily when you start using if statements in Python.
In Python, an object can evaluate to either True or False.
Step 2-1¶
Create a new variable called hostname and assign it the value of "r1":
Perform a boolean check on this varible using the bool() statement:
You may be wondering what's actually happening. Yes, hostname is a string, but when used in a boolean expression, it would evaluate to True.
Why? Because it has a value of one or more characters (i.e. non-empty).
Step 2-2¶
Create a new variable called vendor and assign it the value of a null/empty string:
Perform the same boolean check:
See the difference? As long as a non-empty value is assigned, it would evaluate to True when used in an expression.
The same is true for other data types such as lists.
Step 2-3¶
Create a variable called vendors and assign it the value of ['cisco']. After it's created, do a boolean check:
Step 2-4¶
Perform the same excercise by using an empty list as the value:
With this logic, you'll be able to know if there is some value assigned to your variable when we start using conditional logic (if statements) in Python.