BDS Software

Python Programming

Python is a text-based programming language suitable for beginners. It's more compact, but not too much more complex, than Scratch. The Python developers say:

"Experienced programmers in any other language can pick up Python very quickly, and beginners find the clean syntax and indentation structure easy to learn."

Python is available from Python.org


-----

I use Python primarily for quick and dirty utility operations. Here are a couple of examples:

# addtab.py
# MDJ 2021/07/18
# Add two tabs at the front of each line in infile.txt
# Save the result in outfile.txt
# infile.txt is not changed

st = "\t\t"
fi = open("infile.txt", "r")
fo = open("outfile.txt", "w")
while 1==1:
     s = fi.readline()
     if len(s)==0:
         # End of infile.txt
         break
     s = st + s
     fo.write(s)
fi.close()
fo.close()

-----

# fcomp.py
# MDJ 2021/07/19
# Compare two text files (file01.txt and file02.txt) line-by-line
# and report the results
# file01.txt and file02.txt are both unchanged

lc = 0
f1 = open("file01.txt", "r")
f2 = open("file02.txt", "r")
while 1==1:
     lc = lc + 1
     s1 = f1.readline()
     s2 = f2.readline()
     if ((len(s1)==0) and (len(s2)==0)):
         print("Files Match")
         break
     if ((len(s1)==0) and (len(s2)!=0)):
         print("Mismatch at Line ", lc)
         break
     if ((len(s1)!=0) and (len(s2)==0)):
         print("Mismatch at Line ", lc)
         break
     if (s1 != s2):
         print("Mismatch at Line ", lc)
         break
f1.close()
f2.close()

=====

                                                                                                                                                                M.D.J. 2021/07/19