From Zero to Network Automation: My First Step into Python
This is the first real step in my Zero to Network Automation journey — learning Python from the ground up. In this post, I cover the absolute basics: the data types, variables, and functions that form the foundation for everything in Python. These are my notes — concise, practical, and focused on wh
Welcome to the first real post of my Zero to Network Automation journey. I’ll be documenting everything I learn, try, fail, and succeed in when it comes to learning Python and network automation.
Check out Zero to Network Automation on my YouTube channel.
In this post, I’ll be going over the basics of Python. These are my notes.
The absolute bare minimum to use Python and to be successful from here on out.
I will get straight to the point and only go into detail when I think it is necessary.
Data Types
To start off, there are three data types to be familiar with. There are more than three in Python. But we’re just learning how to crawl.
Integer
The Integer (int) data type contains whole numbers.
Whole numbers do not include decimal numbers. That means numbers such as:
-10, 0, 1, 5, 100, etcFloating Point
Earlier, I mentioned any decimal number is not considered a whole number. They are in fact a Floating Point (float). That would include:
-10.0, 0.2, 1.5, 50.0, 100.26, etcThere is no number data type. It’s either Integer or Float.
String
For those with text values, that would be a String (strs).
A String is enclosed within single or double quotes, for example:
"Corgi"
'Corgi'It doesn’t have to be a single word. A string can be anything within the quotation: ‘I have a one year old Corgi at home.’
A String starts and ends with a quote.
You can have an empty String which would be nothing surrounded by quotes, for example: ''
Concatenation
It is possible to combine, or concatenate, Strings together.
This is performed with the + operator.
What first comes to my mind is combining my first and last name:
‘Rowell’ + ‘Dionicio’This would result in: RowellDionicio
Literally combined (concatenated) together. If I wanted there to be a space between my first and last name, I would need to concatenate an empty string:
'Rowell' + ' ' + 'Dionicio'
'Rowell Dionicio'Concatenation of different data types
You cannot concatenate a String and an Integer. This would look like: ‘Rowell’ + 2
The result would be an error:
'Rowell' + 2
Traceback (most recent call last):
File "<python-input-41>", line 1, in <module>
'Rowell' + 2
~~~~~~~~~^~~
TypeError: can only concatenate str (not "int") to strBut you could multiply a string with a number to replicate the string:
'Rowell' * 2
'RowellRowell'And if you concatenated an Integer with a Float the result would be a Float:
5 + 5.0
10.0These are things to keep in mind when working with Python.
Variables
What we will work with a lot are Variables.
A variable is an item that contains a value.
Imagine you are moving out of your room. You have three boxes. On each box you write a name: Collectibles, Toys, Clothes.
Each is going to be your variable but we need to assign something to those variables:
collectibles = ‘Pokemon cards’toys = ‘Legos’clothes = ‘Padres shirt’A variable can also hold an integer or a float:
age = 10age_next_year = 11Naming your variable
It’s important to know some rules when naming your variables:
- Cannot begin with a number
- Cannot have spaces
- Cannot be a Python keyword
- Uses only letters, numbers, and underscore (_) character
Basic Functions
We’ll learn about writing Python functions soon. But this is about knowing some basic functions we can use right now.
print()
It’s obvious. This function will display what is surrounded by quotes:
print('My name is Rowell.')We place what we want printed to the screen inside of the parentheses and inside the quotes.
input()
If we want to get information from the user we use the input() function.
Whatever is typed by the user, we’ll want to place that value into a variable so we can use it later or use it to print to the screen.
userName = input()print(userName)userName= input()
Rowell
print(userName)
RowellNotice within the print() function, I did not place the variable surrounded by quotes.
len()
To find the length of a string, we can pass it to the length function.
len('Zero to Network Automation')
26The result will end up being a variable.
str()
This function will evaluate whatever is passed to it into a string.
We can turn an integer or floating point into a string by using the function:
str(999)
'999'We can find out if this really is a string by using the type() function which will tell us what data type it is:
newString = str(999)
type(newString)
<class 'str'>int() and float()
Likewise, the int() and float() functions will evaluate to an integer and float, respectively.
newPi = int(3.14)
print(newPi)
3
type(newPi)
<class 'int'>And if we went the reverse, we could take the integer and evaluate it down to a float:
float3 = float(3)
type(float3)
<class 'float'>
print(float3)
3.0That’s all I wanted to get to for now. We’ll go through some labs on the live stream to reinforce what we learn.