An Introduction to Python
by K. Yue
1. Resources
2. Basics
2.1 Python programs: some basic concepts
2.2 Running Python in Windows
set path=%path%;C:\Python311
python
python helloworld.py
2.3 Concepts to learn in a new language
2.4 Some basics to get you interested (hopefully):
2.5 Examples of concepts that may be new to you:
Example:
Print first 100 Fibonacci numbers: fib.py.txt (remove .txt when saving)
# print first 100 Fibonacci numbers
a, b, count = 0, 1, 1
while count <= 100:
print(b, ' ', end='')
a, b, count = b, a+b, count+1
print()
Note the use of multiple assignment statements. In other languages, such as Java, you may need to replace:
a, b, count = b, a+b, count+1
by
temp=b;
b=a+b;
a=temp;
count++;
Other examples will be discussed in the class.
3. A Non-trivial Example
Example:
Consider the following weather information file: 201401daily_sample.csv. Write a Python program to read the 'codeSum' column (#23) and output the count of each codeSum. Note that the codeSum column may contain more than one codeSum separated by white spaces. Example:
SN FG+ FZFG BR UP
has five codeSum:
import sys, getopt
import re
from operator import itemgetter
# getopt: C-style parser for command line options.
# sys: System-specific parameters and functions.
# re: regular expression operation
# operator: methods for built-in operators.
# (useful when passing the function as a parameter.)
# Read and process a weather information file.
# It parse the codeSum column (#23) in the CSV file
# and show the count of each codeSum.
f = open(sys.argv[1], 'r')
result = [];
heading = f.readline().split(',')
num_line = 0;
for line in f:
line = line.rstrip() # strip trailing white spaces
# result is an array of arrays:
# Add the array contains columns of the current line
# to result.
result.append(line.split(','))
num_line = num_line + 1
f.close()
# Debug:
# for i in range(len(result)):
# print (str(i) + ":" + str(result[i][22]))
# count is a dictionary with the key being the individual CodeSummary
count = {};
for i in range(len(result)):
# process one reading.
line = result[i][22].strip()
if line:
# Get all codeSummary and update their counts.
summary = re.split('\s+', line)
for j in range(len(summary)):
# Debug:
# print (str(j) + ":" + str(summary[j]) + "---")
if summary[j] in count.keys():
count[summary[j]] += 1
else:
count[summary[j]] = 1
# Print result in the sorted order of codeSum.
for key, value in sorted(count.items(), key=itemgetter(0)):
print (key + ": " + str(value))
Running the program:
>...python codeSum.py 201401daily_sample.csv
BR: 18
FG: 2
FG+: 5
FZFG: 8
HZ: 10
RA: 2
SN: 14
UP: 4
Version 2: more Python-style with some interesting Python features:
from collections import defaultdict
import sys, getopt
import re
from operator import itemgetter
# Read and process a weather information file.
# It parse the codeSum column (#23) in the CSV file
# and show the count of each codeSum.
f = open(sys.argv[1], 'r')
heading = f.readline().split(',')
result = [line.strip().split(',') for line in f.readlines()]
f.close()
# count is a dictionary with the key being the individual CodeSummary
count = defaultdict(int)
for field in filter(lambda a: a, map(lambda w: w[22].strip(), result)):
for sym in re.split('\s+', field):
count[sym] += 1
# Print result in the sorted order of codeSum.
for key, value in sorted(count.items(), key=itemgetter(0)):
print (key + ": " + str(value))
4. Some useful tools
4.1 Logging
Example:
import logging
# logging
logLevel = logging.DEBUG
logPath = "."
logger = logging.getLogger()
logger.setLevel(logLevel)
logFormatter = logging.Formatter("%(asctime)s::%(threadName)-12.12s::[%(levelname)-5.5s] %(message)s")
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
logger.addHandler(consoleHandler)
logger.debug("Program starts.")
logger.debug("Program continues.")
4.2 Jupyter Notebook (especially for DASC 5333)
Example:
Try out: CodeSumNB.zip. Unzipp to get CodeSumNB.ipynb. Put it in your notebook working directory and open it with juypter-lab.