LING 388: Computers and Language Lecture 12
For those using Python via an IDLE If you're wondering what directory you are in (by default): import os os.getcwd() If you want to change the working directory,use: os.chdir(path)
Last time: Homework 5 revisited http://www.bbc.com/eart h/story/20160826-why- our-ancestors-drilled- holes-in-each-others- skulls >>> import re >>> from collections import Counter >>> f = open("trepanation.txt") >>> s = f.read() >>> c = Counter([re.sub("[,.?;:\"'!]","",w) for w in s.split()]) >>> c.most_common(20) [('the', 114), ('of', 95), ('in', 38), ('a', 34), ('and', 32), ('to', 30), ('trepanation', 29), ('The', 24), ('been', 23), ('that', 23), ('skulls', 23), ('had', 22), ('have', 20), ('was', 19), ('trepanned', 16), ('skull', 16), ('all', 16), ('is', 16), ('trepanations', 15), ('were', 15)] >>> c['ritual'] 7 >>> c['rituals'] 0
Python numbers revisited page 60: see factorial.py Python 3.x Python 2.7 ( )
Python: Files Like all other programming languages, uses a file handle, called file variable: open() infile = open("file.txt","r") outfile = open("results.txt,"w")
Python: Files https://docs.python.org/3/tutorial/inputoutput.html#reading-and- writing-files
Sample text file Course webpage: falconheavylaunch.txt http://www.bbc.com/news/science- environment-42969020 Course webpage: falconheavylaunch.txt
Class Exercise Download falconheavylaunch .txt to your computer. Answer the following questions. How many characters are there in the article? How many lines of text are there in the article? How many lines excluding blank lines? How many words are there in the article? How many characters per word? What are the most common words?
Python eval() Think of eval(input()) as being the same as the user typing directly at the Python console …
Python numbers revisited factorial2.py
argv[1] and argv[2] ignored Python argv List of arguments from the command line: what's argv[0] then? test.py argv[1] and argv[2] ignored
Formatted Output: floating point (f) $100 principal for 10 years at 4% (0.04) compound interest = $148.02
Formatted Output Lots of ways: (Old way) notation comes originally from the C Programming Language: sprintf function http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm
Now use instead: <template-string>.format(<values>) Formatted Output Now use instead: <template-string>.format(<values>) %<width>.<precision><type> <type> = d, f, s <width> = minimum number of characters; right-justified by default, -width => left-justified 0 = as wide as needed <precision> = number of places after decimal point e.g. 02d two digits wide, pad with 0 if needed
Formatted Output: examples Function str() coerces integer or float above into type 'str'
Formatted Output Newer way: https://docs.python.org/2/tutorial/inputoutput.html#fancier-output- formatting Use {} for each argument (can be numbered, e.g. {0}, {1},… or referenced by keyword {x}) {:width}
Formatted Output {0:..} and {1:…} index into the format arguments 0 in {0:..} can be omitted but not the colon