Wednesday, August 5, 2020

Python Programming For Beginners Vol 9 rel 11


Why Learn Programming

One of the questions we are faced with as network engineers are why we should learn to program.  Programming is a powerful skill, i.e., a skill that significantly enhances capabilities; it enables tasks that would otherwise be impractical to accomplish.  However, this skill will not be at the expense of network engineering skills; these skills remain essential for a highly functional network engineer.

As with every discipline, there is a significant trend towards automation.  Task automation has been trending for several years, so it should not be a surprise that it appears in the networking realm.  It is no secret that we should be automating a lot of the things that we have previously been doing manually.  Automation is where Python can help, i.e., the automation of networking tasks.

What is Python?

Python is an interpreted, high-level, Polymorphic, general-purpose programming language. Created by Guido van Rossum, a hobby in 1989, then formalized and officially released in 1991.

Python's design prioritized readability, supplanting { with whitespaces, and indenting as the dominant vernacular.  Adhering to object-oriented tenants to assist programmers in writing exact, logical code for small and large-scale projects.

It supports multiple programming paradigms, including structured (mainly, procedural), object-oriented, and functional programming. Python has been, at many times, labeled as "batteries included language."  This primarily due to the comprehensive standard library that accompanies it.

Python, a successor to the ABC language, was conceived in the late 1980s.

NOTE:  ABC is an imperative general-purpose programming language and programming environment developed at CWI, Netherlands, by Leo Geurts, Lambert Meertens, and Steven Pemberton. It is interactive, structured, high-level, and intended to be used instead of BASIC, Pascal, or AWK.

Python 2.0, released in 2000, had added:


List comprehension. (A list comprehension is meta construct, i.e., a list of lists; it follows the mathematical set-builder notation

garbage collected (garbage collection (GC) is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects no longer in use by the program.)

Reference counting (reference counting stores the number of references, pointers, or handles to a resource, i.e., an object, a block of memory, disk space, and many others).

The Python 2 language, is officially EOF in 2020 (first planned for 2015, but delayed), and "Python 2.7.18 is conceivably Python 2.7's last instance,  and therefore the last Python 2 release."  In short, no more security patches or other improvements released for it.

Python 3.0, officially released in 2008, was a significant revision of the language that is not entirely backward compatible, and much Python 2 code does not run unmodified on Python 3.

Python interpreters support several operating systems. A global constituent of programmers develops and maintains CPython.

Note: CPython is the reference implementation of the Python programming language. ... CPython can be defined as both an interpreter and a compiler as it compiles Python code into bytecode before interpreting it.

A free and open-source reference implementation of CPython is available. A non-profit organization, Python Software Foundation, manages and directs resources for Python and CPython development.


Why Python?

As a Network Engineer, the second question is, “Why Python?” There are many programming

languages out there, Perl, Ruby, C++, Java, so which language should we choose as network engineers, and why Python?

1.     It Is Easy.  The forgiving syntax and high-level features make it one of the best languages for anybody to get started with programming. 

2.     Python Is Open Source.  Succinctly, the Python language is free for anybody to use. Python can make almost anything from web applications, mobile apps, server-side coding, artificial intelligence, and machine learning algorithms. Anything that inspires the imagination, Python will always have the way to do it. 

3.     Huge Support Community.  A vast community of programmers maintains libraries and modules publicly offered to the programming public to assist in their endeavor to obtain a solution.  For example, machine learning algorithms are already available designed to increase productivity.  With this, there is little reason not understanding why people around the world prefer Python.  Python's readable features make anybody want to start coding.

The first take-a-way is that Python is exceptionally readable. Readability means the syntax is relatively straightforward and intuitive. This readability places it first among beginner programing languages. Python is formatted so that one saves hours by eliminating the need to decrypt code to understand what it does.



When we say that Python is a beginner programing language, we do not mean it is less potent than other languages. Python is immensely powerful and can do just about anything one could ask a computer program to do. We do mean that Python is a common language chosen by coding beginners because of ease of use.

Code is essential for today's network engineers, but coding is not necessarily going to focus on a network engineer's job.  Conversely, a software developer can afford to learn a difficult language such as C++, where a network engineer has little need for this venture.

Therefore, a language like Python is a great choice. It is powerful but simple to learn.

 Specifically, Python is a very high-level language that does not need a lot of glue code to get things done, and there is no need for manually clean up.  For example, 

·        No garbage collection

·        No memory allocation deallocation

·        No declaring variables on the types of variables in advance

What are some other useful characteristics of Python?

Simplicity

Python is feature-rich. Let us now see the different features that Python has in store for us.  The most prevalent feature of Python is its simplicity. Python has made Programming fun because it is easy to read, easy to write, and simple to follow.  It allows the programmer to think more about the solution rather than syntax.

Open Source

The next important feature is that Python is an open-source language.  Open source means that it is free for anybody to use.  Modifications to Python are also allowed without any problems.

Portability

Python supports portability, i.e., Python transferred to any operating system without requiring significant rework.  Portability means that one can write code and share it with anybody, and it would work almost identically as it did initially on the parent operating system.  This portability means movements or projects and systems much more manageable.

Embeddable & Extensible

The next feature is the embedding properties.  The chances are that specific language may perform certain functions better than Python, but that does not hinder the Python enthusiast.   Python supports add-ons, plug-ins, and even adding code of other languages into itself to run those functions, which makes that add extra functionality to the program. 

Interpreted

Python is interpreted line by line, making memory and CPU management and debugging the code much more straightforward.  As one writes code, the program can assist in syntax, options of approach, and debugging right on the fly, so productivity and coding efficiency is improved.

Huge Library

Python has tremendous library support.  This extensive library can help improve and enhance solutions to programming endeavors without reinventing every task.  This library introduces programmers to the latest concepts like data science, web development, et al. with Python.

Object Orientation

Python supports object-oriented concepts.  Python can easily create and use classes and objects in Python, which means that one can model a real-world problem into the code and have security for it such that access to them is restricted.

Object ; Class;  Method;  Inheritance; Polymorphism ; Data Abstraction ; Encapsulation.

The key points of why Python makes sense are the community's size, but there are many other attributes such as ease of use, readability, and scalable power of Python that make it the right choice.

This ease of use, support community, and flexibility does make it easier to understand why Python is also becoming the default SDN language.

Object-Oriented Programming Concepts in Python

Python is an object-oriented programming language. Python is a language that affords easy creation/use of classes and objects.  Major object-oriented concepts of Python programming language are Object; Class;  Method;  Inheritance; Polymorphism; Data Abstraction; Encapsulation.

Object

An object is an element that has state and behavior. It may be physical and logical, such as a mouse, keyboard, chair, table, pen, etc.

Everything in Python is an object which has attributes and methods.

Class

A class, much like a container, can be defined as a collective of objects, a software grouping that has some specific attributes and methods. For example: if given a student class, it should contain an attribute and method, i.e., an email id, name, age, roll number, and others.

Method

A method or function is somewhat of a filter that is associated with an object. It can act upon an object without changing the original content, only producing a manipulated instance of it in the output.  In Python, the method is not unique to class instances. Any object type can have methods.

Inheritance

Inheritance specifies an object acquires all the properties and behaviors of the parent object. With inheritance, we can define a new class with little or no changes to the existing class. The new class is known as a child class, and from which it inherits the properties is called a parent class. It provides the reusability of the code.

Polymorphism

Polymorphism is when a single object can behave in different ways. For example, we have a class operator "+," and all "+" adds. However, the + acts differently depending on the context. Here, the "x" behavior depends on the objects it is adding. In the case of integers, "+" adds the two numbers, but in the case of strings, "+" concatenates the two phrases.

Encapsulation

Encapsulation restricts access to methods and variables. In encapsulation, code or data are wrapped together within a single unit from being modified by accident.

Data Abstraction

Data abstraction and encapsulation are synonymous. Data abstraction achieves through encapsulation.

Abstraction hides internal details and shows only input and outputs. It is abstracting something giving a name to a program that embodies the primary function, e.g., Coffee Maker.

Python 2 vs. Python 3

Another Python programming facet we should be aware of is what instance of Python should we use, Python 2 or Python 3?

Just be aware, Python is going through this transition of migrating from Python 2 to Python 3.  Moreover, the two languages have some compatibility issues with each other. There are certain things one must make include in code to be both be executed by Python 2 and with Python 3.

Analogous to our IPV 4 to IPV 6 transition, it has been dragged out for quite some time. 

  • ·        Python 1 was released on June 22, 2001. 

  • ·        Python 1.6, documentation was released on September 5, 2000.

  • ·        Python 2.0 was officially released on October 16, 2000.  

  • ·        Python 3.0 was officially released on December 3, 2008.

Python core maintainers are saying that they are going to continue maintaining Python 2 up through 2020. However, this does not mean that Python 2 enhancements will go away in 2020. Certainly, people will maintain Python 2, particularly for security patches, beyond 2020.

Returning to the network engineers’ questions, one important item would be “what exists already in the environment?” Is there a legacy codebase that needs to be maintained? If none exists, then start with Python 3 and start writing.

Not having a legacy codebase to contend with will probably be an uncommon situation for many people.  For many using Python already, existing Python 2 scripts are a reality.  Hence, with two python versions, one should start working through making those scripts be both Python 2 and Python 3 compatible.  Also, probably start transitioning away from using Python 2 towards Python 3 for new projects.

Python 3 was a significant revision of the language that is not entirely backward compatible. Although, many of its significant features were retrofitted to Python 2.6.x and 2.7.x versions. Python 3 includes the 2to3 utility, which handles the syntactical anomalies (some) the translation of Python 2 code to Python 3.

 

Syntax of Python

Now, what are some of the characteristics of Python?  For starters, one of the essential


aspects of Python as a programming language is that blocks of code are indicated by indentation.

Control structures like an if statements and others that are followed with a colon have subordinate executable statements indented at the same level now.  By convention, this is four spaces that we used to indicate our indentation.

The critical point here is Python partitions in blocks of code. In other words, all this code that is inside this functional statement is indented.  Unlike other languages, there are no curly braces to indicate blocks of code.


In this example, we have an elif statement and three code lines indented, which shows that this code is subordinate to the conditions of the elif statement.

Subsequently, we have an else statement that has a single line of indentation where a line of code embodies the instruction for that statement. Hence, when the subordinate code executes completely, we go back to the previous indentation level. The previous level, in this case, is this "for" statement.  Specifically, it is not inside this else statement.

The concept here to absorb is that indentation matters.  We partition blocks of code, and that typical structure will be a line ends with a colon, an indentation block of code will follow that.

There are many conventions in Python and Python programmers that are meticulous; hence, Python conventions are best to be followed.

Python programming conventions

There is a whole standard in Python, PEP 8 (pub July 5, 2001), a Style Guide for Python code.  It has what formatting should be used when writing Python code.

In addition, where we may deviate from the PEP 8 guidelines, there are linters.


NOTE: A code linter is a program that analyses source code for potential errors. A linter can detect the kinds of errors, including syntax errors, structural problems like the use of undefined variables, best practice, or code style guideline violations.)  

Linters are syntactical programs that tell if the code follows Python conventions. 

As a coder, following these conventions makes sense, for it makes code easier to follow by other people or even the original author months or years later. 

If coming from a different language that has different conventions at first, the temptation to fall back on old habits will be strong. However, in short order, the conventions will probably feel natural. 

It is a good practice when coming into any new language that is adhering to the programming language conventions.

 

What is Python-like as a language

One can get some sense of what Python is all about when executing a  command 'import this'; it summarizes the level of relatable conventions that Python has prioritized in the language, relatable phrases.

Some of these statements here one must dig into and understand what they mean in more detail, but it gives some sense of the essential things in the language.

We have talked about Python's other characteristics previously, so we have talked about things about it being a high level. In general, there is little need for intermediary glue code before writing a program, there is no need to declare variable types, and Python is a dynamically typed language.  Just by assigning a value to a variable, Python declares the variable's type just through the assignment.

In dynamically typed languages,  if we write my_STR = ‘whatever’  declaring this variable my_STR to be of the type that's on the other side of the equals sign (=), we do not have to declare beforehand. 

Similarly, we can change my_STR to be a different type, so here we change my_STR to be equal to 22 an integer.  The critical item here to remember is that variables are dynamically switched depending upon the statement's context.

In Summary

Hopefully, this narrative has given a nice introduction of who, what, where, and why Python is the chosen language for the future.  From ease of readability, portability, natural language approach, and ability to use other languages to enhance performance, just to mention a few, are argument enough to investigate its viability as your choice for programming languages to add to your tool belt.  With that, we wish you the best and happy coding!

 _________________________________________

We would like to thank our sponsors, for without them - our fine content wouldn't be deliverable!


Source
So “Once more unto the breach, dear friends, once more;”
____________________________________________________________
About Rick Ricker
An IT professional with over 23 years' experience in Information Security, wireless broadband, network and . Infrastructure design, development, and support.
Currently a Computer Science Instructor at CSULB



No comments:

Post a Comment

Thanks for your input, your ideas, critiques, suggestions are always welcome...

- Wasabi Roll Staff