
I just have to admit it – I know just enough Python to understand but not code it!
Python is a lot like when I took “Intro to Spanish” in College, I can understand enough to get the drift of what is being said, but I could not speak a fluent sentence to save my life – And the same is exactly true with Python – Specifically Python 3.
This has literally been a point of Dread through my DevNet studies, as I can understand the examples I see, but couldn’t not freestyle a script to save my life – That stops now!
I will be posting my mistakes in programs, why I made those mistakes, and how I correct them and this thread will be a sticky post to remind me every time I look at this site GO LEARN SOME MORE PYTHON PROPERLY THIS TIME! 🙂
(I did change it to end this post with its current content because the text is moving extremely slow because of the amount of content, post #2 is right around the corner!)
That being said, this may be kind of a messy pile of post with lots of basics at first!
I will solely focus on Python 3 here, re-explain things I already know, as I may not know them as well I think I do when I type them out and debug a script so I this sticky post will be ever growing until this single post collapses the WordPress database with pictures and explanation!
I don’t want to litter a bunch of python tid bit posts all over the forum, so I will contain it all to this thread, just picked up where I left off each time I start back into videos in my class.
Learning Python 3 again properly from the beginning and building all the way to the end!
Python 3 is a strongly typed (meaning you cannot add an integer to a string literal), but also a dynamically typed language, so you are not committed to variable declarations in your script.
Programming Paradigms are when Programming Languages / Scripts are written in a Procedural manner to execute from beginning to end, however unintended side effects of the script execution dynamically changing due to a piece of the code – Which is maybe an unexpected but at the same time correct way that the code should have executed (even though it was not the original intention) – This term “Programming Paradigms” was homework.
With the intro out of the way, time to jump into some coding and intro topics!

We all know where Square 1 starts with Python – Hello World! 🙂
What this does is take the “print” function, and pass the “argument” of a string literal ‘Hello World!’ into it, this is a basic concept I missed in how easy it is is that Arguments are contained within the ( ) of functions, and the functions are run against that argument – In this case it prints the string literal Hello World! as denoted as a String Literal by the quotes used.
So when you put arguments within functions (), you are running the function against the argument to produce a result, I always missed this concept because Hello World is always the geek joke for learning Python is learning how to print Hello World! when really you are learning to run an argument against a Python function to produce a result!
Functions can (and will) run more than one argument as well, but that will come later.
It’s important to both use single or double quotes on both sides, but also to watch how you write your code, as if I wrote print(‘Hello to all the Bob’s in the room!’) as seen here:

A string literal can handle up to 3 single or double quotes at max, however in the example above had I used print(“Hello to all the Bob’s in the room!”) it would not have goofed up.

A function with no argument just invokes that function, however we printed a bunch of stuff here, but why use 5 different lines to do that when we can just do it all in one line?

Here we did a print function, a mathematical function inside of the print function, and turned 4 + 5 into an ASCII string literal rather than integers performing a math function with quotes!
A “Literal” can be a value of some type that is contained in quotes, what makes it a string literal is the quotes which tell Python I mean exactly what I type inside the quotes, literally!
A Function is a block of code that is invoked by calling out its name, which we can write our own functions as well, in Python ALL Functions return a value (even if its nothing).
Arguments are values passed into Functions to give it values to work with, that seems to be a fairly static term for “Argument” in Python is it is the values passed into Functions.
“Calling a function” is just using something like print() to simply call the function with no arguments, this is still considered “return output” in the code even though its a blank space.

I did it again! I got ahead of the course instructor, thinking I new Concatenation already, and when I printed I saw that derp, which is why I’ve decided to re-train myself entirely!

I did it again, not thinking that ” = no space, where I need ‘ ‘ to create a space:

(Yes I must display mistakes so I learn from them, this is going to be a very long journey to train myself to properly think through what I am writing and why I am writing it!)
Above I used “Concatenation” which is basically a math function of addition by adding two string literals together to form one line, where I built a space into one of the string literals, whereas with Concatenation using Variables you must add a space using ‘ ‘ or probably other means as part of the Concatenation.
Also, Concatenation is not a math function, as shown here it is for strings only:

Note not only can you not Concatenate a string to an integer value, but also this script halts execution immediately upon hitting an error in the script, to fix this up:

I am getting ahead of myself here but I couldn’t not correct that when I saw this error, here I define the integer as a string value by typing str(#), which interpreted it as two strings being Concatenated and printed 35 before moving on down the Script!

In this case instead of using assigning a Static Variable for “name” I write the Variable “name” equaling what is typed in per the “Input Function” as shown above here, then the same Concatenation line works just the same (except with my !) so I fix that here:

There we go, I know there is another way to add the ! in there cleaner, but needed to get some enthusiasm going in learning Python from the ground up here again!
When using an Input() Function to assign a value to a Variable, the Input function is read first on this line, and then once the input is entered and the enter key is hit, it then assigns that value to the defined variable name second (to get that order of operation clear).

I am familiar with the escape character \n in Python which essentially is the equivalent of hitting enter in the script, or like when I was going through David Bombals Python series there was a \n after each config entered to write it to the running config.
Now here I typed without thinking again, putting spaces after the \n, causing formatting issues:

That looks much better, and its things like this that I think I know (and have a good grasp on) but find out I don’t really truly know is why I need to start over! This can also be put into a Variable format and the Variable used as the Argument within the Print function:

More than one way to split up a string using \n Escape Character! 🙂

This is a “Tabbed String” where each value has a ‘Tab’ value or indent added to it across the screen rather than escaping down to the next line down, which is generally 4 spaces for a standard indent but may vary across systems or Code Editors / IDEs.
The “\” can be used an a special escape character in strings as well along with Triple Quotes I demonstrated above, let me see if I can get this right here the first time:

I didn’t realize that the video I was watching the statement ended with a period (which should have been inside the quotes to not set off my grammar alarms), so I will see if I can fix this up here and add a third line using triple quotes to see if I can navigate that:

I underscored a few interesting things in this VSC code that it shows the “a” in Line 8 as an \escape syntax issue, but it debugs or runs fine, also note that I tried to put “\1000\1000″\ in this line but the second 1000 started to turn into escape characters, of course if I turn the / around:

Sure enough that took care of the syntax confusion with the “a” before score value, so that is an interesting behavior from escaping in situations where it might be better to avoid it.
Also I can use the use just triple quotes to contains that line 8 (I think), and also can break my lines without the \n manually in the script and get the same output using triple quotes:

That is all looking good, so I want to see if we can break some stuff here, perfection is boring:

With only double quotes everything falls apart pretty quick, the score and # in split lines turned into Integers, “is” turned into an unknown syntax error, and we now have an if loop on line 13!
So as we can see here, that escape character “\” works magic to integrate quotes within strings where it would otherwise break it, and for some reason triple quotes specifically are make these lines quite possible whereas single or double quotes will not without the “\” character:

Just added that extra set of quotes to each statement and immediately fixed, so that is \n line break \t tab “\” escape character and Triple Quotes to break lines and now to put those 5 lines all back into a SINGLE LINE by using the “\” to escape it out of being on its own line:

I learned something new with the “\” escape character and triple quotes, I recall there was uses but I didn’t realize they were kind of forcing certain lines to be understood correctly!
Another use for the “\” esc char is to assemble multiple lines back into a single line:

Now consider if you have to define a path on a Windows machine that includes escape chars:

There are two solutions to this which I played around with but didn’t quite get (I was fixated on trying to make it work with triple quote on multiple lines):

The first method actually uses the “\” esc char to esc the esc telling Python we do not want those “\” in the line to be esc chars, and the second method is putting an “r” for “raw” String previous to the String Literal to tell Python not to manipulate this string which will have drawbacks demonstrated down the Python road a ways, as we may want to manipulate that line some day!
The rules of Variables in Python:
- Python Variable must begin with a letter (Upper or Lower case) or underscore _
- They can contain letters / numbers / underscores but cannot start with a number
- Variables are case sensitive, so different capitalization means different Variables
- Variables are created when they are attached to a value using the = operator

Shown in my string.py file, line 9 shows greeting = “Hello” statically set as a String Literal, and name = the input from the user prompted to type their name, bro – This is why we can reference them both as two exact string values in the following print function.
I’ve cleaned up the strings file to demonstrate some different Variable types, and how we can tell what data type a Variable is using the print(type(variable)) command shown here:

Its usually thought that a Variable Name is the “Data Type” or “Value” assigned to it, but I could type on line 17: greeting = 45 and line 18: somenumber = “Hello”, which now the Variables names have been re-assigned values / data types as those would swap integer / string data types for the same Variable name – So its better in Python to think of the values themselves being assigned to a variable name rather than the other way around because the Variable Name can change Data Type / Value, but the Value will keep its Data Type (unless strongly typed), and is actually stored in memory as the value being assigned to a Variable rather than the other way around in Python.
Now to how Python is strongly typed, lets look at an example I previewed earlier

Line 18 cannot properly print because “somenumber” is assigned a Value / Data Type of 45 / Integer, and + would treat it as a Math Function and error out – For this reason we can change the Data Type to allow it to work with this line of code by “Strongly Typing” it:

This allows the Integer to be treated as a String in this Context so that the variable can be used properly in the context of this line of code, but still be used as an Integer elsewhere, as we cannot turn Strings or Letters into Integers however Integers can be represented as String Data Types – For example you can perform divide “Hello” / “World” and get a return value from that math function, unless Hello = 10 and World = 2 which then math functions can be performed.
Some Math Functions can be done by Integer to String like multiplication, but for most functions the Integer must be strongly typed as a String, and instead of a math function this is Concatenation as shown here in IDLE for Python for a quick demo before I am out here:

So Python is not “weakly” typed but both “dynamically” and “strongly” typed as shown here.
The following is a list of Built-In Data Types into Python:
- numeric
- iterator
- sequence (also iterators)
- mapping
- file
- class
- exception
Right now I will focus on the “numeric” Data Types which include the following 3:
- Integer (Int) – 1, 45, 857636, 4, 10
- Float – 2.7, 324.5, 3.0
- Complex – Uses imaginary / theoretical numeric values (not discussed here)
Complex has its name implies is a very advanced Python numeric type that consists of both defined and imaginary values, relies on advanced math equations, and will not be reviewed.
Integers also know as “Int” us a whole numeric value with no fractional parts, where Floats is a whole number that includes fractional parts (decimals), the difference in computational languages is that integers compute much faster than floating point numbers (Floats).
So to be clear the numeric value 1 is an integer, where as 1.0 would be a float:

Below is a demonstration of the math “operators” for numeric values:

These are not the only type of “Operators” in Python, but the basic Math Operators, that will result in a Mathematical function resulting in a value.
Note that while Division does produce a “Float” result, the “Floored Division” returned an Integer value as Floored Division rounds down, along with the result of the “Remainder” operator in math functions as shown below:

Its good to note here that both Floored Division round down, but also the “Remainder” operator also does not produce a rounded “Float” value, but it rounds down to an Integer value of the remainder rather than a “0.8” or something like that – Only division results in a Float!
Now to introduce a Loop for the first time, I bring you a “For” Loop:

Note that it doesn’t print the Integer 5 in the for Loop, as once it hits the final # the Loop ends and it stops performing the Iterations of that Loop, however I demonstrate this to show that because Division results in a Float result it cannot be used in a For Loop as shown here:


By making it a Floored Division statement (or literally any other operator) it produces a result which can be used in a “For” Loop, this is an important concept as “For” Loops often use Variable Integers within Scripts to perform an action that amount of times – You cannot perform a function only a percentage of an “Iteration” (one run of the loop).
A review of Expressions and exactly what they are!
Expressions are the Integer Values the are “expressed” through being evaluated by operators:

Expressions are exactly as they sound which is deceptively easy, highlighted above are all the expressions I have currently in my VSC script, they are integers that are evaluated as listed:
- 16 and 4 are expressions of the Variables they are assigned to
- 16 + 4 is an expression of the value 20
- 16 – 4 is an expression of the value 12
- 16 * 4 is an expression of the value 64
- 16 / 4 is an expression of the float value 4.0
- 16 // 4 is an expression of the integer 4
- 16 % 4 is an expression of the remainder of 16 // 4
- 1 is an expression of the value of 1
- a // b is an expression of the value of the result 4
- range is an expression of the result of the values inside of it
- Finally “i” is an expression of the value of iterations in this loop
- Also each Variable within an Expression is also an expression, because line 6 must evaluate the value of A and B, to then evaluate the value of adding them together!
One rule of expressions is that the Variable on the left side of a Variable for A and B are NOT expressions, as the value on the right side of the variable is what is evaluated, whereas the Variable itself has no value and is only bound to the expressions 12 and 4.
So to really clarify that, expressions are bound to Variables, A and B have no value what so ever until they are “bound” to an expression that can be evaluated to produce a result!
The initial “i” in the “for” loop statement is not an expression because it is being bound to the value within the range the same way A and B are bound to #’s, however the “i” in the print statement IS an expression because it is evaluated to get the value of iterations in the loop!
Operator Precedence / Order of Operation / DOES NOT USE ALGEBRAIC PEMDAS!
I actually had to delete a Python Post I made awhile ago based on what I had read in “Automate the Easy Stuff” in which I thought Python used PEMDAS (the Algebraic order of operations in which equations are solved piece by piece in a specific order), where as now I have tested it over and over and I was completely wrong and will demo with this example here:

Python does give Precedence to the math operators for Division and Multiplication like PEMDAS does, but it won’t skip around the line to perform the math operations in a strict order, it will only give those two operators precedence to be operated first then re-run the entire math expression with those two expressions within the statement assigned a value per evaluation:

I kind of half way labbed this concept in an IDE without robustly testing multiple operators and finding all values in order to ensure they made sense, so this again is why I am just covering Python from the ground up because of I am finding out the things I thought I knew I don’t really know as well as I thought I did a week or so ago as of this writing (or even 30 minutes ago).
The Math Expression operations will run from the left to right, with one iteration finding and assigning the values to b / 6 and 8 * 14, then it re-runs the entire expression with those values plugged into the entire math expression which is executed again left to right:
a + b / 6 – 8 * 14
16 + 0.6666 – 112 = 95.33333*
I cannot believe how wrong I had that for so long, though I never had scripts dependent on order of operations executed that I recall, I am so glad to have cleared that up.
For the sake of clarity on the subject, I verified this is also the case with floored division:

4//6 = 0, 8 * 14 = 112, 16 – 112 = 96
If the intention is to have the expression a + b run first, it can be done, but it requires not just a single parenthesis enclosing that expression, but a cascading set of 4 parenthesis shown here:

In this example I did hug the division Int right up against the operator to see if it would throw an error, but the integers can be right on the operator if and it will work, but definitely would not pass be considered “Cleanly written code” by any standards.
The parenthesis does not need to separate every math operator as shown here, if there are some values meant to be operated together as well, we can exclude one parenthesis from the print function and keep one expression within the same Parenthesis bracket shown here:

This does change the value because the operation is being run differently by manual intervention in the operation precedence, we can also break these different expressions up into Variables to work with though this may make the code harder to read / understand than Int #’s:

The Expressions assigned to Variables c – d are equal to the expression on line 11, and by simply creating a variable to sum it all up, I code have also created an extra Variable that contained the expressed 8 * 14 however I opted to just put in the Integer value for Variable ‘e’ in this code.

So this is interestingly an expression where PEMDAS theory would apply, as with single operations of multiplication / division / addition / subtraction the * and / take precedence but have equal priority as does the + and – operators, read from left to right in the expression.
However when there are multiple instances of Division and Multiplication in the value, this is actually where PEMDAS would take effect, and Multiplication is performed before Division to allow for the Division to have their Integer Value from the * expression to perform the own evaluations within the expression.
A review of Strings Sequence Data-Types
The first thing to demonstrate in this next video is showing how single letters can be printed from a variable by entering a number value in [] as shown in the below print script:

This is because Python always begins indexing at 0, so when I type in the Integer 5 it will actually go 6 characters into the string, so that is something to keep in mind when working with Python Indexing is that it starts at 0 (kind of like how the last # in a loop won’t iterate because that is where it exits the loop).
The [] indicates a “Dictionary” in the code, and is used to pick out individual items from the code such as is it printed this single Indexed letter of “a” per its indexed position in the String which is the “Argument” being ran against the Print “Function” which makes the dictionary one of the Arguments ran against the print Function to produce a result along with the String.
On the flip side we can start from the other end of the String, and use negative Indexing:

Negative Indexing does not have a 0 value, as -0 doesn’t make mathematical sense, so -2 would print ‘a’, -3 would print ‘d’ and so forth. The interesting thing is you can actually use a subtraction math expression in the argument to arrive at the Dictionary answer as shown:

It first indexes forward 5, then when subtracting it goes back 5 to 0, and at that point it begins into the negative # space where it would start at the last character and last on on ‘d’ for the result of this negative index.
String Slicing with Dictionary
Another way to use a Dictionary within an Argument to “Slice” pieces out of the String to be displayed, an example of this can be seen below with a new variable “example”:

The range defined tells it to start at the 0 in the Index and print up to but NOT including the 5th indexed character, so though it does print the first 5 characters, technically 0-5 is 6 numbers.
The index can be started or ended anywhere within the #’s indexed for the String, for example I can get diced by using “:” to separate the proper Index #’s, or I can leave each side blank as leave one side blank means start at the beginning (0) or end (last index # in string):

With slicing one major concept is outside of the 0 indexing when determining where to slice, is that the the right hand side Index # is where it will stop printing unless it is blank, another kind of interesting part to slicing is that you can concatenate it as well to manipulate a string:

Which this is just an example to demo that is stops short of the 6th index, and then the blank prints the rest of the string, however this could be used to manipulate a string for fun:

That wonderful philosophy changed pretty dramatically, thank you Python Slicing! 🙂
(We all know I only harbor love for learning IT in my robotic heart!)
You can also use negative #’s which I just made one big blob to explain it visually here:

When using a Negative Index #, it references that Index # starting from the right and moving left, and if it is in the second part of the bracket it still applies the “Up to but not including” unless and only unless the index value is blank (which defines the rest of the string).
One key concept that may be difficult to get is why certain values don’t work, however each point has to be thought of by its index #, like my last example where it is being instruct to print -8 to the 11th index which would be starting at the right and moving left which doesn’t work.
Using “Steps” in Slices of Strings, and how to make them useful!
Still using my “example” variable, I made a very simple demo of what steps are in Slices:

The 3rd # specified in the slice (I only used the Step Values here for clarity), it will print only every # index specified in the range and the Step value, for example the first line I specified the whole String, and it does “I” “h” “r” as the 1st / 3rd / 5th Index values moving all the way to the end where the following two output do this in increments of 3 and then 4.
Now to look at a more practical use, I will use a # to show how we can use this to separate out data using steps to create a script that is honestly at this point beyond me, however first:

Here we are just printing out every 4th index which is a separator of a number in this variable, where some are spaces / commas / colons / semicolons, and then we can use this to write a bit of more length code to extra the number with a script based upon these separators to demonstrate how it CAN be done scripted to pull out certain values that are logically separated as such:

I got a little bit ahead of the course with some Google-fu as I couldn’t just break out this kind of scripting and not know what is happening, so I will break out the bullet points for this one:
- We are starting at the Index 1 so the Index 0 spot doesn’t through off our groove
- We are declaring the separators to be every 4th index spot in this String
- We define values with a few different “Methods” of .join and .split in there
- “”.join() method is a way of storing the values of iterations separated by separators, and the script goes on the say if char is not a separator then ” ” which means make it a string value
- .split() method is invoked by itself by the (), this is what tells the script to make the char string values into a list
- Finally the print([int(val)for val in values]) tells the script that the Strings stored in the .join method are integers, and to list them out as numeric Integers rather than Strings in the list
I could very well be wrong in how I am thinking about that, but that is how I interpret it!
A quick look at Slicing in “Steps” Backwards in Python, and its rules / limitations
Now I said that you cannot slice backwards, you are back to step backwards to produce a result in reverse, like counting down to zero which can be done with Steps but not with printing a solid string because that just logically does not make sense so let us look at a demo below:


The first demo shows that the Indexing “Up to but not including” counts against the 0 index, however when doing “Steps” backwards by -1 Python will know that you mean to reverse the string and will print all the way to the end in reverse (all the way to including 0). You can also do it even with just two colons stepping back by -1, which is called a Python “Slicing Idiom” :

One more important distinction is with Backward Step Splicing is the below concept:

- When it was “::-50” it gave “z” as the output
- When it was “5:0:-50” it gave “f” as the output
- When it is now “0:5:-50” it doesn’t give a result and it never will in this format
For Backwards Step Slicing, the first index # must be larger in value than the second, or it will not produce any kind of result ever as there is logically no result that can be produced.
With that I am going to actually end this Python post here as the text is moving slooow!
I forgot that near the end of my giant DevNet posts my text was moving in slow motion because it was overloaded with content, and this is starting to do the same thing, so I will create a new post once I pick up the next Python topic as this caps off this section of the course.
Lots of ground covered and great new topics covered so far, onward and upwards!
Hiya,
Hey, this is really cool, what your doing here. I have the exact same sentiments as you regarding Python and implementing it into both CCNA and DevNet studies.
I look forward to following your ongoing posts❗️
Best Regards,
Dave
LikeLiked by 1 person
Thanks so much for the kind words! The DevNet material did really uncover huge gaps in Python, so I am very much enjoying learning it from the beginning and already feeling more confident in my Development / Automation understanding as it all intersects with Python at some point. Hope my ramblings prove useful! 🙂
LikeLike