Introduction to Python
by K. Yue
1. Resources
2. Basics
Python programs
Running Python in Windows
set path=%path%;C:\Python35-32
python
python helloworld.py
How to learn a new language:
Some basics to get you interested (hopefully):
Example of concepts that may be new to you:
Example:
Interpreter:
C:\Users\yue>python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> hello_world = 'hello world, from ITEC 3335'
>>> print(hello_world)
hello world, from ITEC 3335
>>> exit()
Print first 100 Fibonacci numbers:
# 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 as demand in the class.
Example: A Non-trivial program (if needed)
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 columns may contain more than one codeSum separated by white spaces. Example:
SN FG+ FZFG BR UP
has five codeSum:
codeSum.py: more Java-like style
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
If you are really Python curious, a second, more Python-like version:
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))