Sunday, June 30, 2019

The for in Loop In Python

One of the strongest features, in my opinion, that Python has is its ability to work with sequences. Sequences are ranges, lists, tuples, sets and dictionaries. I think that pythons elegance in working with these sequences is due to the design decision not to include extra syntax.

Most languages have two for statements...the for loop and the for in loop. Python only uses the for in loop and has a built in function called range (xrange in python 2.7) to do basic indexing.

for i in range(10): print(i)
Because python uses zero indexes the above example prints 0-9 and not 1-10. Ranges actually stop at count-1.

It's tempting to use the above form of the for loop to iterate or cycle through all indexes of a list as the below example shows but there are better ways to accomplish the same task.

lst = [0,1,2,3,4,5,6,7,8,9]
for i in range(len(10)): 
  print(i)
This is not the most pythonic or cleanest way to loop through a list.


Counting indexes and passing the length to the range function is just a little too much work and is really unnecessary because you can accomplish the same thing by passing the list directly into the for in loop.

for value in [0,1,2,3,4,5,6,7,8,9]: 
  print(value)
I just passed a literal list into the above code but I could have passed in a list variable like I do in the below examples.

For most cases the above syntax is all that is needed and I estimate that I use it 90% of the time and rarely use any other looping mechanism. I suspect that the gate keepers of Python had the same experience and that is why they chose only two loop structures ( while and for in ). Programmers coming from a different language such as C++, C# or Java are usually quick to resort to the range function because they need access to the index not just the value. However, python has a better solution to this problem by using the built-in function enumerate.

The below example uses the enumerate function to iterate through all values in a list with the for in loop and print the index and the value accordingly.


>>> lst = [10, 3, -1, 40, 9, 1, 0, 3, 3]
>>> for index, value in enumerate(lst):
...   print("index: " + str(index) + " value: " + str(value))
... 
index: 0 value: 10
index: 1 value: 3
index: 2 value: -1
index: 3 value: 40
index: 4 value: 9
index: 5 value: 1
index: 6 value: 0
index: 7 value: 3
index: 8 value: 3
>>> 
This implementation would be blessed by the python elders and priests.

The same task using the range function, as shown below, is a little more wordy and is more difficult to debug.


>>> lst = [10, 3, -1, 40, 9, 1, 0, 3, 3]
>>> for index in range(len(lst)):
...   print("index: " + str(index) + " value: " + str(lst[index]))
... 
index: 0 value: 10
index: 1 value: 3
index: 2 value: -1
index: 3 value: 40
index: 4 value: 9
index: 5 value: 1
index: 6 value: 0
index: 7 value: 3
index: 8 value: 3
>>> 
This would be considered non-pythonic because it is not the most readable and compact implementation.

The former example using enumerate is most likely to get a thumbs up from fellow python developers but the later is probably going to give you a little bit of push back. I would prefer the enumerate implementation as well but because I have written a good number of C, COBOL, C++ and C# programs I don't judge either one as being good or bad. Its important to note that many developers are very good at looping through collections and they may not even see the benefit in the enumerate function.

The for in range structure  is still a useful construct and is needed if we want to:
  1. Iterate over a fixed number without indexing a collection. 
  2. Calling an internal or external API that gives us a count and a "get item" method instead of a collection.
  3. Skipping or "stepping" through index values.
Ranges can actually be saved into a variable, count backwards, step by positive and negative numbers and can be converted to other sequences like lists and tuples. The range function and xrange function are actually implemented differently and have a different processing cost. Even though their usage may be rare they do belong in python!

More on ranges can be found online here:
https://pynative.com/python-range-function/

A good book on python such as the one below can also be a valuable resource in learning python.

if, if..else, if..elif..else

When you need to execute code if and only if a certain condition is met then you can use the if statement. If statements can be very simple or very complex.

if

In it's simplest form an if statement in python looks like if a == b: <<statement>>

if a == b: a = 100
One liner if statements are sometimes pointed at as not pythonic due to readability. But I believe this is unfounded when the statement is very short.

Indentation is very important when writing structures such as if statements in python. If we expand the above statement to two lines the if statement only executes lines that are indented.

if a == b: 
  a = 100
  b = 0
  c = random(1,0)
d = 1 #always executes 

if..else

You can also use the keyword else that will only execute if the condition in the conditional expression is false. You can only have one else in an if statement. The following example will always print hello because the condition is the literal value True.


if False:
  pass
else:
  print("Hello")
The pass keyword in python just tells python to do nothing. Many programming languages allow you to write a comment or simply leave a blank line but python requires an empty if to at least have pass statement in its body.

You can put several if statements together if you have multiple conditions and statements to run.

if s == "Hello":
  print("World")

if s2 == "":
  print("The string is empty")

if s3 == "abc"
  print("I know my abcs")

if..elif..else

If you want to check multiple conditions but only if the previous condition(s) are false then use the if..elif structure. The following example is very similar to the above series of if statements but each condition is only evaluated if the previous condition is false.

if s == "Hello":
  print("World")
elif s2 == "":
  print("The string is empty")
elif s3 == "abc":
  print("I know my abcs")
The keyword elif just means else if. The makers of python just wanted to save three stroke on a keyboard!

The else statement must go at the end of this series of statements. It will be executed if all if and elif conditions are false. For example:

if s == "Hello":
  print("World")
elif s2 == "":
  print("The string is empty")
elif s3 == "abc":
  print("I know my abcs")
else:
  print("Nothing else was true!")

Backwards If Statements

You can also put an if statement proceeding another statement in python. Unlike the first example this version of a one liner if statement is considered good form and is very useful when converting raw data into something readable. The else is required in this case because otherwise the variable has no assignment if the condition is false.


s = "Not Entered" if s == "" else s
Notice that you don't need the colon (:) in this statement

You may be tempted to add an elif to the above example but it will not work. The elif statement doesn't apply to this if structure.


#This will not run. The elif keyword is not valid here
s = "Not Entered if s == "" elif s == "*" "Err" else s

Nested If Statements

In python, and in most modern programming languages for that matter, you can nest or place if statements within if statements for more complex conditions.

a = 1
b = 1
c = 1
d = 1
if a == b:
  if a == c:
    if a == d:
      print("a b c and d are all equal")
else:
  print("a is not equal to b")

The above example is pretty lengthy and is for demonstration purposes. It can be written instead, without nested if statements, as:

a = b = c = d = 1
if a == b == c == d:
   print("a b c and d are all equal")
else:
   print("a is not equal to b")

You can also nest if, if..elif..else statements inside of the else and elif statement bodies like this:


a = b = c = d = 10
if a == b:
  if a == c:
    pass
  elif a == d:
    pass
elif a == 0:
  if a != 1:
    pass
  else:
    print("Hello!")
else:
  print("Hello")

Nesting if statements too deep can lead to unreadable code. Sometimes it is the best solution to the problem but I find that is very rare. It is not often that I need to nest an if statement more than two levels deep in any programming language.

COBOL programs are notorious for having a "dangling if" problem. Reading deeply nested if statements in COBOL is very difficult because its hard to tell, in a very nested if structure, what if statement goes to what else or else if. Python doesn't have this problem because it uses indentation. You can easily see what if, elif, and if statements go together because they are on the same column.

Saturday, June 29, 2019

Python Conditional Expressions

Python supports conditional expressions. Conditional expressions allow you to execute statements only when a specific condition is met. Conditions are mathematical expressions such as:

a == 0         #Is a equal to zero?
a != 0         #Is a equal to zero?
a > 100        #Is a greater than 100
a >= 100       #Is a greater than or equal to 100
a < c          #Is a less than c
a <= c         #Is a less than or equal to c

Conditionals can be used outside of an if, while or other conditional statement. If you set the variable a to a value you can use the above expressions to evaluate the result. The below program sets a to 0 and then evaluates its condition


>>> a = 0
>>> a == 0
True
>>> a < 0
False
>>> a <= 0
True
>>> a > 0
False
>>> a == 10000
False
>>> 

Conditional expressions evaluate to a boolean type. If you pass them to the built-in type function you can see they are boolean objects.

>>> type(a == 0)
<class 'bool'>
>>> 

Because conditional expressions can be evaluated to a variable, really an object, we can store the conditional for later use.

>>> is_a_equal_to_zero = a == 0
>>> is_a_equal_to_zero
True
>>> 

Once the variable is_a_equal_to_zero is assigned it stores the last known evaluation of a == 0 and not what it currently is. If you re-assign a to a new value it will not change is_a_equal_to_zero.


>>> is_a_equal_to_zero = a == 0
>>> is_a_equal_to_zero
True
>>> a = 10000
>>> is_a_equal_to_zero
True
>>> 

Conditional Expressions are fundamental in programming and it is difficult to get any "real work" done without having some kind of conditional logic. You can combine conditional expressions with if statements, while loops and other python structures.

Friday, June 28, 2019

Comments in Python

Single Line Comment


Python uses the symbol # to represent a comment. Unlike many languages like C++ and C# there is no begin and end comment.

#This is a comment
print("Hello World")
#This is a comment too

You can also put a comment after a line of code but not before a line of code.


print("Hello World") #This is a comment too!

Python comments are stripped before the code is compiled to byte code for execution so unlike some interpreted languages it does not cause a penalty during runtime. If there are a bunch of comments then it could cause extra compile time.

A Multiline Comment Hack

I was quickly corrected by one of my fellow python developers when I referred to multiline strings as comments. They are in fact just strings that are not assigned a variable.

"""
This is a multiline string.
It is often used for long comments...
but it is really just a string that is not assigned to a variable
"""
def print_hello():
  print("Hello World")

Writing Loops With Break and Continue

The simplest form of a loop in python is the while True: infinite loop but you can pass other expressions to the while loop that will make it infinite.

i = 0
while 1:
 i += 1
 print("Hello "+ str(i))

The above will do the same thing as passing while True.

You can write other conditional expressions in a while loop to manipulate the number of repetitions.

i = 0
while i <= 10:
 i += 1
 print("Hello "+ str(i))

The above will print hello n 10 times instead of printing forever.

The break Statement

You can also write infinite loops and break out of them using the break statement in python. This is very common and replaces the goto statement that is often demonized in modern programming.  When you break out of a while loop or any other loop your code stops execution where break is encountered and continue execution after the while loop.

Recall that python works with indentation so that the while loop ends when the indentation is no longer there.


i = 0
while 1:
 i += 1
 if i <= 10:
  break
 print("Hello "+ str(i))
a = i

In the case above we do the same loop but a little less compact. When the break statement is hit execution continues at a= i. 

The continue Statement

Similar to the break statement the continue statement is used to control the flow of the while loop. Instead of exiting the while or other looping mechanism it is used to skip statements below it and go to the beginning of the loop. It will re-evaluate the condition. Because we are doing an infinite while loop it will always be true. It is usually combined with an if statement like above but the if is not required in either one.


i = 0
while 1:
 i += 1
 if i <= 10:
  break
 print("Hello "+ str(i))
a = i

The above program prints hello n exactly like the other two examples except it skips the print command when i has the value 5. If i = 5 all statements under continue are skipped. Often times you will see code that always evaluates to true or simply has a continue statement without a conditional statement ( if statement ). This is sometimes used to skip code that should not be ran. It is equivalent to commenting code. However, it is bad practice and code that never runs is referred to as DEAD code.

Dead code is frowned upon and is often the target of re-factoring or re-writing of a program.


while 1: 
 print("Hello")
 continue
 print("World") # <---- DEAD CODE

The above program will print Hello forever / infinite but never reaches print("World").

More about loops in python can be found here:

https://www.w3schools.com/python/python_while_loops.asp




I must confess that I have sinned in this way many times. Often times I write "dead code" like the above example because I am troubleshooting the loop to see what will happen if I skip to the end or to see what happens under certain conditions if I continue or break out of the loop. Unfortunately, "The road to hell is paved with good intentions" and I sometimes forget to take those statements out before committing or saving my work.

Thursday, June 20, 2019

PIP - Python's Package Management System

Python has an easy to use and very robust package management system called pip. In order to take advantage of another programmer's work you have to import their source code into your program. The Python package management system offers a true advantage over compiled language such as C++ and even other interpreted languages like Ruby.

Pip is included with Python 3 but if you don't have it installed the instructions can be found here:
Pip Installation

Its dead simple to include an external package using pip:

$ pip install mortgage

In some operating systems you may need to install and use pip3.

$ pip3 install mortgage

For instance pip3 installation on a Debian system:

$ sudo apt-get update
$ sudo apt-get install python3-pip
$ pip3 --version

https://linuxize.com/post/how-to-install-pip-on-debian-9/After you have installed the package you can include it in your own python code using the import statement. After the package is installed you use it as follows:


from mortgage import Loan
loan = Loan(principal=30000, interest=.925, term=6)
loan.summarize

If you run the above program in python you will get a very nice summary of a loan with the given parameters.

Because of the overwhelming popularity of python for both commercial and open source projects, there are a ton of projects written in python that you can take advantage of. This is one of the biggest selling points for me when deciding on a good programming language for a project. If there is a package available and I don't have to write one or extend and existing package then I can save a-lot of time and effort.

The Dirty Way To Include External Python Code

Many compiled languages and even interpreted languages don't have a package management system or they are so difficult to use that it takes as much time to include someone else's source code as it would to re-write it yourself.

C and C++ programs and even Java and C# programs for that matter are littered with copy and paste code. If a programmer can't figure out how to reference a library or include code the "correct" way they will use whatever means necessary to get the program working.

Copy and paste code is a huge burden for both commercial and open source projects. Source code always carries with it a maintenance cost.

How to Find Packages

You can find packages easily online by navigating to The Python Package Index. When you find the package you are looking for you can use the pip install <<package> just like we did the the Mortgage example above.

Another good way to find a package is to search for open source projects on SourceForge or GitHub. Most python projects have instructions on how to install the package using pip. Many commercial vendors such as Microsoft and Amazon have open source packages to support their applications.

If you prefer the command line then you can use pip search <<package>>  . It will also tell you if the package is already installed, what version is installed and if there is a later version available.

You can see from the below command that we have several other packages to choose from. Packages that have a version below 1.0.0, py-mortgage 0.1.0 for example, are generally less mature. Sometimes they can be useful but if you are doing serious work then I would stick with 1.0 and above.


$ pip search Mortgage
mortgage (1.0.3)             - Mortgage Calculator
  INSTALLED: 1.0.2
  LATEST:    1.0.3
py-mortgage (0.1.0)          - A package to handle mortgage calculations
pymortgage (0.1.4)           - Python Mortgage utilities
py-mortgagekit (1.0.3b1)     - Python library for mortgage calculations.
Mortgages-and-Loans (0.1.2)  - Simple Python Scripts for Complex Mortgages
pandicake (0.3.3)            - Utilities for mortgage backed securities cash flow analytics

Pip has a-lot of functionality and can get complicated if you get into advanced features. Most of the time you just need a simple pip install to get your python program to work. However, if you are working on a larger project or in a strict programming shop then you may need to dig deeper into how pip works.

The official user guide for pip can be found here:
PIP - User Guide

Monday, June 10, 2019

The While Loop in Python

In order to perform operations over and over again in a programming language you need to have syntax that allows you to execute the same lines of code without having to re-write each individual line. In python there are a few ways to do this and one way is the while command.

The indentation is important in python. All statements under the while command must be indented by at least one space, and by convention some programmers use 4 spaces. If you use an editor the tab key must be setup in the editor so that when you press tab it really insert spaces instead of tabs.


The below example will print Hello n until the end of time or you press CTRL-C.

i = 0
while True:
 i += 1
 print("Hello "+ str(i))


This is what we refer to as an infinite loop and when I learned programming 20 years ago we were told to avoid them at all cost because it would hang your computer and cause you to lose your work. But python, and modern operating systems for that matter, are very good and handling this situation.

The output for the above program is as follows:


Hello 2548067
Hello 2548068
Hello 2548069
Hello 2548070
Hello 2548071
Hello 2548072
Hello 2548073
Hello 2548074
Hello 2548075
Hello 2548076
Hello 2548077
Hello 2548078
^CHello 2548079
Hello 2548080
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
KeyboardInterrupt

You will notice ˆC and the KeyboardInterrupt in the output. This is expected behavior because we never gave a command to end the loop.

Also understand that inside of the parenthesis we used "Hello " + str(i). This is called string concatenation. We use the plus operator, which you would expect to be used for math operations, to join two strings together. The str(i) function is used to convert i, which is an integer, to a string instead.

For a dynamic language python can be strict on using types. Many dynamic languages would automatically assume that you wanted i to be converted to a string. Python simply wants to make sure that you really intended on converting i to a string before it joins it to "Hello".


https://www.w3schools.com/python/python_while_loops.asp - More information about while loops and for loops.

I am writing this blog and testing my examples using an iMac very similar to this one. You don't really need a powerful computer but its nice to have one that has a nice display and is very quite. Macintoshes are very elegant and the unix base that OS X is built on helps me to access unix command line tools which I find more powerful than Windows command prompts.




Reading User Input in Python

Reading Input From The Keyboard

In order to process user input you are going to need to retrieve keyboard data from the user. One of the most easy and straightforward ways of getting user input is to use the input function.


>>> a = input("Enter a number:")
Enter a number:23
>>> print(a)
23
>>> 

In the above example we use the input command to retrieve a number and store it in the variable a. We could have first used the print command and then called input() but it is more compact and easier to read to display the user input and retrieve it at the same time.

A common mistake would be to call input(a) expecting the input command to store the value in a.

>>> input(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> 

NOTE: The variable a is automatically declared in python and we don't even have to give it an initial value before retrieving it from the user.



Sunday, June 9, 2019

More About Variables

Python is a strongly typed dynamic programming language. This means that when you create a variable in python it is assigned a type and can be used as that type. Because python is extremely flexible it has the ability, without any extra syntax, to change one type to another type.

Because typing is dynamic in python it does not check if the type is correct until the program runs. So if you for instance try to check the absolute value, which is a numeric operation, you will get a syntax error when the program runs but the program will run up until that point. This is a little bit of a double edge sword because on one hand your program has the flexibility to handle this situation and even have dynamic code that relies on it but on the other hand your program could be more stable if this was checked ahead of time.

This is an example of a type error when we try to get the absolute value of "Hello".

>>> abs("Hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
>>> 

Python understands types as classes:

<class 'int'>
<class 'float'>
<class 'bool'>
<class 'str'>

The simplest way to declare or create one of these variables is to use the correct literal syntax when assigning a variable. In the below example I create one of each type.

1
2
3
4
a = 1
b = 1.0
c = True
d = "Hello"

Python allows you to check the type ( class name) of these variables by using the type function. We consider this method built-in and available to use. You can create user-defined functions in python as well but it is not necessary to learn that at this point.

In the below example I can print out each of the above type names for each variable:

1
2
3
4
5
6
7
8
9
a = 1
b = 1.0
c = True
d = "Hello"

type(a)
type(b)
type(c)
type(d)

The output of the above example will be:

1
2
3
4
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'str'>

If you just want the name of the type you can use type(variable).__name__. This will just print out the name and not the extra syntax that you see above. This is very useful for some programs that need to ensure they are really getting an integer and not a string for instance.

Use the below example to get the variable types.

1
2
type(3.14).__name__
type("Hello").__name__
Notice I just pass values, called literals, into the function instead of first assigning them to a variable.

The above program will output:
'int'
'str'

You can learn more about built-in types on the below link:
https://docs.python.org/3.7/library/stdtypes.html

What Does It Mean To Be Pythonic?

As you go out into the community and seek help from the python developers
you will encounter various, well meaning and experienced python developers that will accuse you of not being "pythonic". This simply means that you are not writing code the way the python community believes is efficient, straightforward and follows python guidelines.

The above T-Shirt could help you if you are feeling a little bit code shamed. To be honest I know too many programming languages to really be cleansed by the python community.

To protect me from such religious zealots I would like to reference the below definition of pythonic until I write better code :)




pythonic

1
[ pahy-thon-ik, pi- ]





adjective

of or relating to pythons.
similar to a python; pythonlike.
gigantic or monstrous.

https://www.dictionary.com/browse/pythonic





Variables in Python

In order to do anything with your output it is useful, and it can be argued absolutely necessary to store information somehow in order to manipulate that information. Python has a very straight forward and easy syntax ( set of rules ) in order to do this called variables.

They are called variables because the value can be changed.

To declare a variable in python is simple:
1
2
a = 1
b = 100

In the above example I have declared two variables named a and b. I store the value 1 in a and the value 100 in b. To avoid confusion I like to say that a becomes 1 and b becomes 100. This is to avoid confusion between equality and assignment. To say that a is equal 1 and/or b = 100 could be true for this portion of the code but the variable could later be manipulated to become another value.

Now that these values are stored in variables I can output them using the print command we learned earlier:

1
2
3
4
5
a = 1
b = 100

print(a)
print(b)

If you type this in to a python shell or in a python program you will get the output:

 1
 100


Continued On -> More About Variables in Python


https://www.w3schools.com/python/python_variables.asp - More about Variables

Python Programming: A Practical Introduction To Python Programming For Total Beginners

Note: I use the term declare to describe how to create a variable in python. If I am honest I have told a white lie. To declare a variable usually means that it has to be predefined before it is used. In python its first use automatically declares it. For someone learning python I think this is splitting hairs and it is inevitable that programmers are going to ask "How do I declare a variable in python" and the python zealots will respond with "One does not declare variables in python".

What is Python

Python is a powerful dynamic, interpreted ( actually byte compiled ) programming language that is extremely useful and widely used. Because of its popularity there is a large amount of amount of documentation, example programs, books, blogs and open source libraries available.

Python is also widely used in both commercial and open source projects, and it has the ability to compile to native executable, java, and .NET

Although I believe python has a very strong object oriented syntax, my favorite aspect of the language is its ability not to force you in to an object oriented solution. Instead, you can write very simple and very powerful python programs without understanding or needing to use classes, dependency injection or other advanced object oriented concepts.

Having programmed for many years, I wanted to get back to basic programming and engineering concepts that are fundamental and did not rely on modern design patterns and practices.

https://www.python.org/


One of the best tells of an easy programming language is how easy it is to get started. If you have an internet browser you can navigate to https://www.python.org/shell/ and type in the provided python shell and immediately begin to learn python.

Hello World

The traditional way to begin learning a programming language is with the Hello World program and in python it is very simple:



print 'hello world!'

The above program simply prints the words "Hello World" to the given terminal. It is an output only program and that is very easy to write and maintain!

Useful Links


https://www.w3schools.com/python/ - One of the best to the point python tutorials available.


https://realpython.com/installing-python/ - Guide to installing python




O'Rielly books are really good and I would suggest purchasing one if you want to learn more about python.

nonlocal keyword

The nonlocal keyword is used to allow an inner function to access variables defined in an outer function. Without using the nonlocal keywo...