Monthly Archives: November 2017

Lets re-learn Python!


OK: here we go. I learned enough Python to write some, and to follow a lot of Jesse & Co’s at VMware. But I didn’t write all that much, I couldn’t check in anything, because there was not way to  test check-in candidates BEFORE going live. Or, at least, I couldn’t find one. And when I asked for help, I didn’t get what I needed.

But now I’m re-learning, since everyone says they want want proficiency in Python in their new hires. Better brush up on it then. .

So step one.  The canonical program in any Python book goes something like:

print (‘Lesson_1.py with single quote’)
print (2 ** 902)

to show off the easy familiarity Python has with very large numbers.

So I expanded on that. More print statements and if else and elif, Adding a demo of indents being isolated – The block for “if” must be all the same indent, the block for  else need to all be the same. But nothing requires the “if” block to match the “else” block. All they have to be is the same within themselves. Parseable.

Next, since we’re always printing things, what does “print()” return? Not-1, according to the if-then. If we print it, its “None”.  And we can test that it equals “None” (string equals is “==”. It does equal “None”.

But not only does it NOT not equal “none”, you can’t ask that question, without declaring/creating a “none”.  But its not a compile time call. The power of late binding is that nobody has checked “none” (or “NoNe”) until the “==” gets it.

And we get a lovely error:

“what comes back when we print one char
None
no char indent
None = print returned 1 or thereabouts
Traceback (most recent call last):
File “lesson_1.txt”, line 40, in <modul
if none == print(” no char indent”):
NameError: name ‘none’ is not defined”

And now our canonical program has an error, so we can canonically use the “try”, “except”, “finally”  statents.

And if we’re  really lucky, the response will have an error and we’ll get a SECOND TRIP through the error handler!

C:\Users\wabbott\python\Lesson_1>python lesson_1.txt
Lesson_1.py with double quote
Lesson_1.py with single quote
3381084999268257576654974623465706281720622886631177741618948537770712976363039
one char indent
else four char indent
elif six char indent
no char indent
print returned not-1 or thereabouts
what comes back when we print one char indent
None
None == print()
None == print returned 1 or thereabouts
we always do this, but don’t make any mistakes!
Traceback (most recent call last):
File “lesson_1.txt”, line 57, in <module>
if none == print(“none == print()”):
NameError: name ‘none’ is not defined

and there we go.

 

# Lesson_1.py
# picking-up the Python thread again, 5 years later.
# All the recruiters hope I know it, better look into that and perhaps I can find a job.
#

#!/usr/bin/python – ha!

try:

print (“Lesson_1.py with double quote”)
print (‘Lesson_1.py with single quote’)
print (2 ** 902)

# Python uses indentation instead of curly braces to identify blocks. Kind of a nice idea.

if 1:
print( ” one char indent”) # this one prints
else:
print( ” two char indent”)
if 0:
print( ” if three char indent”)
else:
print( ” else four char indent”) # this one prints

 

if 0:
print( ” if five char indent”)
elif 1:
print( ” elif six char indent”) # This one prints
elif 0:
print( ” elif seven char indent”)
elif 1:
print( ” elif eight char indent”)

 

 

if print(“no char indent”):
print(” print returned 1 or thereabouts”)
else:
print(”  print returned not-1 or thereabouts”)

print ( print (” what comes back when we print one char indent”))

 

if (None == print(“None == print()”)):
print(” None == print returned 1 or thereabouts”)
else:
print(” None != print returned not-1 or thereabouts”)

 

if none == print(“none == print()”):
print(” none == print returned 1 or thereabouts”)
else:
print(” none != print returned not-1 or thereabouts”)

 

if NoNe == print(“NoNe == print()”):
print(” NoNe ==44 print returned 1 or thereabouts”)
else:
print(” NoNe != print returned not-1 or thereabouts”)

#  except Argument:
# print(“The argument is>”, Argument, “< ” )

print(“And look, now it fell through!”)

finally:

print(“we always do this, but don’t make any mistakes!”)

# ———-X———-X———-X———-X———-X———-X———-X———-X———-X———-X

Advertisements