Lab 04 - Working with Integers¶
This lab provides an introduction to working with integers in Python. While integers and numerical operations aren't used that much in the course, it's still worth understanding some of the basics of integers.
Task 1 - Explore Basic Integer Operations¶
Step 1-1¶
Create the variable vlan_id and assign it the value of 10.
Note: You worked with numbers in the strings lab as well, but they were just characters in a string. Pay special attention to your syntax and definitions:
"10"is a string, but10is an integer (number)!
Step 1-2¶
Check the data type of vlan_id:
Step 1-3¶
Using the dir(vlan_id) or dir(int) statement to view all built-in methods for integers:
>>> dir(vlan_id)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
>>>
You'll notice there are not many very relevant methods for integers.
Step 1-4¶
Create a new variable called vid and assign it the value of "100" using quotes:
Step 1-5¶
Print both vlan_id and vid using the print statement:
Notice how you cannot tell the data type from a print statement. Be careful and be consistent with the data types you choose!
Step 1-6¶
Print them both again without using the print statement:
Here you can see which one the string is by noticing the single quotes wrapped around the value.
Step 1-7¶
Let's now take a look at a concept called "casting" - converting one data type to another.
Create a variable called ipaddr and assign it the value of "10.2.9.1" and also create another variable called mask assigning it a value of 24:
Step 1-8¶
Now try concatenating ipaddr and mask while inserting a / in between both:
You should see this error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>>
Note that you cannot concatenate a string and an integer. You must convert the integer to a string.
Step 1-9¶
You can convert the integer to a string using the str() function:
Step 1-10¶
Here are a few more examples looking at using str() and int().
Ensure you still have vlan_id with an integer value of 10.
Walk through the process of converting this to a new variable that is a string equivalent.