LING 408/508: Computational Techniques for Linguists Lecture 22
Administrivia Homework 9 graded Python: cgi-bin: bash scripting requires attention to detail (unfortunately finicky) that shell script "glue" could access any programming language, e.g. Python Python: from a command line calculator to the Natural Language Toolkit (nltk)
Homework 9 Example Use the cgi-bin methods to implement a BMI bash shell script on the webserver.
Homework 9 Example
Homework 9 Example
Next Programming Language So far, in this course you've been exposed to: Binary language of the CPU: encoding of numbers and characters (unicode) Linux (Ubuntu under VirtualBox) command line: bash shell bash shell scripting: #!/bin/bash awk and regular expressions (regex) html/css Javascript + DOM Web client/server model Now we switch for the rest of the semester to a regular programming course: Python
Term Programming Project It's time to think about the end of the semester … Propose a programming project half of your grade Send your proposal to me If I give up the go-ahead, you can start working on it …
Why Python? Installed by default on OSX but you should install and use python3
www.python.org
Python on Ubuntu sudo apt install python3-pip
Python on Ubuntu
Python resources Many (free) online resources Through UA: 1.5hrs (18 Lessons) QuickStart! - Python https://www.vtc.com/products/Q uickStart!-Python-Tutorials.htm 8hrs (64 Lessons) Practical Python for Beginners Course https://www.vtc.com/products/py thon.htm Pythonbook (based on Python 2.7): On course website: pythonbook.pdf
Python resources python.org
Python At the interpreter:
Python: Numbers pythonbook page 53 pythonbook page 54
Python: Numbers Python: Javascript: Number data types: int, float Matters for things like division: / Javascript: everything is a floating point number
Python: Numbers import math math.pi complex numbers: import cmath
Python range(): produces a list (sequence) range(n) [0,1,..,n-1] range(start,n) [start,start+1,..,n-1] range(start,n,step) [start,start+step,…,last] last=start+k*step < n range(n,stop,-step) counts down (pythonbook page 40)
Python How about to 2 decimal places? use raw_input() instead of input() for strings eval(raw_input()))is equivalent to input()
Formatted Output Lots of ways: (Old way) Notation comes originally from C : sprintf function http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm
Formatted Output %<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 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}
Python pythonbook page 60: see factorial.py Python automatically converts to type long int from int (32 bit 2's complement), (page 64–65, section 3.5) explicit type coercion: float() int() long() complex(r,i) complex numbers complex(string)