Without further ado, here is Part 2, to the "Quick Python Training by Harlan" series.
Here's the link to part 1... (prerequisite is required): http://tytusblog.blogspot.com/2013/10/why-everyone-should-learn-programming.html
This week you will learn: Intermediate Python... Libraries, Dictionaries, Lists, Time, and more.
You will also learn about files, paths, and such... here we go!
- First, you will need to be able to run python from a command line... on a PC, click Start->run->CMD then in a command shell, type cd c:\python26 <enter> (or the path to where you installed python), then type python.exe (you should be at the shell). type quit() to exit the shell.
- on MAC, just open the terminal, and type python, and it should work.
- Now, we need to learn how to make a file that python can run... from command line type:
- notepad hi.py (PC) or on mac.. do vi hi.py
- in notepad type: print "Hello!" then alt-file-save alt-file-exit.
- on mac, type i then type print "Hello!", then press escape, then type :wq <press enter>
- now, from your command line type python hi.py
- SEE THE OUTPUT! :) you just made a python program !
- Okay, next let's do some FILE INPUT OUTPUT to learn about libraries.
- make a file called helloname.py that looks like this:
import sys, os, glob
from optparse import OptionParser, make_option
import csv
def main():
help = "Read the name of person and print hello persons name"
args = "usage: %prog -n = name"
parser = OptionParser(args)
parser.add_option('--path', '-n', dest='name', help='name of person')
(options, args) = parser.parse_args()
name = options.name
print "Hello", name
main()
#run this with python helloname.py -n Harlan
4. Now, lets do some outputting to a file! make a new file called helloname_to_out.py
import sys, os, glob
from optparse import OptionParser, make_option
import csv
def print_hello_name_to_file(name):
afile=open("namefile.txt", 'wb')
afile.write("Hello %s!" % name)
afile.close()
return afile
def main():
help = "Read the name of person and print hello persons name"
args = "usage: %prog -n = name"
parser = OptionParser(args)
parser.add_option('--path', '-n', dest='name', help='name of person')
(options, args) = parser.parse_args()
name = options.name
file = print_hello_name_to_file(name)
print "done check the file", file
main()
#run this with python helloname_to_out.py -n Harlan
5. Time to play with time... try this file: hitime.py
import sys, os, glob
from datetime import datetime
from optparse import OptionParser, make_option
import csv
def print_hello_name_to_file(name):
afile=open("namefile.txt", 'wb')
afile.write("Hello %s! " % name)
now=datetime.now()
afile.write("It is now: %s" % now)
afile.close()
return afile
def main():
help = "Read the name of person and print hello persons name"
args = "usage: %prog -n = name"
parser = OptionParser(args)
parser.add_option('--path', '-n', dest='name', help='name of person')
(options, args) = parser.parse_args()
name = options.name
file = print_hello_name_to_file(name)
print "done check the file", file
main()
#run this with python hitime.py -n Harlan
6. Okay now time for some lists! Let's learn these in idle! start idle in windows start->run->idle or mac: from terminal type: idle
#okay, now you can type stuff into idle to go along... and learn about lists
firstlist = [1,2,3,4]
print firstlist
print firstlist[0]
print firstlist[2]
print firstlist[4]
#now try this
list2=[1,2,"3","hi",["a","b","c"]]
print list2
print list2[3]
print list2[4]
print list2[4][2]
7. now then, on to dictionaries
dict1={1:'one',2:'two',3:'three'}
print dict1
print dict1[2]
print dict1[0]
#and an advanced one
dict2={'numbers':[1,2,3], 'letters':['a','b','c'], 'name':"Harlan", 5:'Five!'}
print dict2[5]
print dict2['numbers']
print dict2['numbers'][0]
8. finally, lets make our own library and import it!
#in notepad make a file called: namefromfile.py
import os
import os
def getname(afilename):
afile = open(afilename,'r')
namelist = afile.readline()
names_in_list = namelist.split(' ')
if len(names_in_list) > 1:
name=names_in_list[1]
else:
name=names_in_list[0]
return name
#save this, now make a new file called hinamefromfile.py
import sys, os, glob
from optparse import OptionParser, make_option
from namefromfile import getname
import csv
def main():
#help = "Read the name of person and print hello persons name"
#args = "usage: %prog -n = name"
#parser = OptionParser(args)
#parser.add_option('--path', '-n', dest='name', help='name of person')
#(options, args) = parser.parse_args()
#name = options.name
name = getname('namefile.txt')
print "Hello", name
main()
#run this with: python hinamefromfile.py
9. OKAY HOMEWORK TIME!!! This will require research... which is a MAJOR part of any programming language!!! but you have the basics to do this!
first, create this file called "harlansdictionary.csv", it should contain the following
Hungry,Honkin Hungry
Angry,Mad As Hell
Happy,Gleed Myself
Sad,Sobbin Myself
Now, write a program that takes any sentence, and replaces any word found in the first column, with Harlan's version on the right. For fun, feel free to add to the dictionary!
NOTE: you must load the dictionary fresh on each time the program runs!
HINT: you might use import csv (research it!) will help a lot.
HINT2: you might use dictionaries
HINT3: you might use split() function as in today's teaching.
YOU CAN DO IT!