Talk About Quality

Tom Harris

Learning a New Programming Language, with Life

with 2 comments

For an upcoming course to help science students master the computer as a tool for their work, by learning to code, I need to learn Python. I reviewed discussions on Python 2 vs Python 3, and chose the latter.

To operate in a new language, I only need to learn and practice four things:

  1. Input
  2. Storage and Retrieval
  3. Processing
  4. Output

(Wish I could program functionally, and avoid #2. But Python isn’t designed for functional programming.)

My traditional project for learning a new language is to code Conway’s Game of Life.

After my first 2 hours in Python, with Python 3.4.3 installed, and Google for answering my questions, I’ve covered command line arguments (though still without argparse for user error handling), input from text file, output to console, line-by-line storage, and character-by-character retrieval from a Python list.

I’ve also imported one Python module (sys), and gotten used to code blocks via indentation.

Here’s my code so far. Wish me luck as I continue learning!

#ReadTextFromFileAndDisplay.py

#Import libraries needed
import sys #for command line argument support

#Get pathname of file to open from first command line argument
filenameToOpen = sys.argv[1]

#Open the file
myFile = open(filenameToOpen)
print("Pathname was: ", filenameToOpen)
print("Result code for opening the file was: \n", myFile)

#Read and display entire file
#Also store it in a list

myList = [] #declare list, initially empty

print("\nHere is the file as it's being read in: \n")
#Read from the file, line by line:
for line in myFile:
....print(line, end='') #display each line
....myList.append(line) #add the line to the list

#Close the file
myFile.close

print("\nFile should be closed now.\n")

print("Here's the contents of the list... \n")

#Display the contents of the list
for row in myList:
....print(row, end='') #entire rows
....print()
....for iCharacterIndex in range(len(row)):
........print(row[iCharacterIndex]) #character by character

p.s. I’ve put periods in to show the indentation, since it’s hard to get WordPress to preserve whitespace.

Advertisement

Written by Tom Harris

July 20, 2015 at 7:44 pm

2 Responses

Subscribe to comments with RSS.

  1. […] I started learning Python two days ago, as described here. […]

  2. […] started learning Python three days ago, as described here, and […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: