





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























Basic Scripting
Thursday's (2/14) Task Untar & uncompress unp code build, test basic datetime client (^) Thursday (2/21) Task (^) Project version 0. definitive project.txt description Add some design/features/issue discussions A prototype or at least mock up of main function, some functionality (^) Lab 03 Discussion Introduction to Scripting using Python
Make Variables & Relative Directories install: cp -a libpalindrome.a /home/csci553/harry/harryrepo/lab03/lib cp -a *.h /home/csci553/harry/harryrepo/lab03/include
Make Variables & Relative Directories OUT_DIR=/home/csci553/harry/harryrepo/lab install: cp -a libpalindrome.a $(OUT_DIR)/lib cp -a *.h $(OUT_DIR)/include
(^) How long it takes to write a program (human time) How long it takes that program to run (machine time)
(^) High-level languages let programmers express their thoughts more quickly… …but the more abstract the language, the more slowly it runs
This series of lectures introduces a versatile high-level language called Python (^) A good way to build tools and crunch data
Nimble languages like Python are slower than sturdy languages like Fortran, C/C++, or Java (^) Factor of 20 is common But it's relatively easy to call libraries in those other languages from Python
But its Numeric package isn't bad
But it's catching up
(^) It's about solving software engineering problems
Might as well choose something useful And it puts everyone on an equal footing
[Lutz & Ascher 2003 ] is the standard introduction [Martelli 2005 ] is a collection of useful tips and tricks See Python Cookbook for the on-line version
Fig 6.2: Sturdy vs. Nimble Execution
And as we'll see later, it permits some powerful high-level programming tricks
(^) On Unix, make #!/usr/bin/python the first line of the program (^) This tells Unix to execute /usr/bin/python with the rest of the file as its input (^) Of course, this doesn't work if Python is installed somewhere else (^) Use which python to find out (^) Better: use #!/usr/bin/env python as the first line (^) This tells Unix to use /usr/bin/env to find Python, then run the script with it $ cat hashbang.py #!/usr/bin/env python print 124/ print 124.0/28. $ hashbang.py 4
(^) On Windows, associate .py files with Python (^) Happens automatically when you run the Python Windows installer (^) Double-clicking on anything ending in .py will then run it
(^) Variables are names for values (^) No declarations: variables are created when something is assigned to them planet = "Pluto" moon = "Charon" p = planet Fig 6.3: Variables Refer to Values
(^) Must give a variable a value before using it planet = "Sedna" print plant # note the misspelling Traceback (most recent call last): File "lec/inc/py01/undefined_var.py", line 2, in? print plant # note the misspelling NameError: name 'plant' is not defined (^) Unlike some languages, Python doesn't provide a default value (^) Because doing so can hide a lot of errors (^) Note: anything from "#" to the end of the line is a comment (^) Variables don't have types, but values do (^) Python complains if you try to operate on incompatible values x = "two" # "two" is a string y = 2 # 2 is an integer print x * y # multiplying a string concatenates it repeatedly twotwo print x + y # but you can't add an integer and a string Traceback (most recent call last): File "lec/inc/py01/add_int_str.py", line 4, in? print x + y # but you can't add an integer and a string TypeError: cannot concatenate 'str' and 'int' objects
The print statement prints zero or more values to standard output (^) Separated by spaces (^) Automatically puts a newline at the end (^) So print on its own just prints a blank line (^) Putting a comma at the end of the line suppresses the newline planet = "Pluto" num_moons = 1 moon = "Charon" print planet, "has", num_moons, "satellite", print "and its name is", moon Pluto has 1 satellite and its name is Charon