Learning a New Programming Language, with Life
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:
- Input
- Storage and Retrieval
- Processing
- 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.
[…] I started learning Python two days ago, as described here. […]
Learning a New Programming Language, with Life (part 2) | Talk About Quality
July 22, 2015 at 8:51 pm
[…] started learning Python three days ago, as described here, and […]
Learning a New Programming Language, with Life (part 3) | Talk About Quality
July 24, 2015 at 12:48 am