CS520
Fall 2013
Lab 7
Due Wednesday,October 16


Basic Scripting in Python

In this lab, you will be learning the basics of scripting in python, to help you learn how to automate part of your workflow for doing assignment 4.

Detailed instructions

  1. Log on to one of the computers in the lab.
  2. Open up a terminal to the lab7 directory
  3. Make the scripts in this directory
  4. When you are done, submit all of the .class files. In order to submit your file, you will need to be SSH'd in to agate.
    ~cs520/bin/DoSubmission.py lab7 script1.py script2.py ... etc

Submitting

Please submit whatever you have at the end of lab, even if it is incomplete. You have the rest of today to finish this assignment.

Basic Tasks in Python

A few important things to know

Python is a dynamically typed language, meaning you don't declare the type of whatever you are using. In fact, you don't have to declare a variable at all, you just need to say x = whatever and x takes the value of whatever. Variables have types at runtime, and if you try to do something invalid with a variable (like add to a string or write to an int) you just get a runtime error.

Probably the strangest thing about python is that whitespace and indentation matter a whole lot. While C, Java, C++, and a number of other languages get their loop and if levels from curly brackets, python divines this information from the indentation of the file.

Printing stuff in python is incredibly simple, it is quite literally print x. If you are having trouble figuring out what something is, it often helps to print it to see what you are dealing with. MAKE SURE YOU COMMENT OUT DEBUGGING PRINT STATEMENTS WHEN YOU ARE DONE. If I find any uncommented out debugging print statements I will deduct points.

Basic Hello World

Make a python program called "hello.py" that prints out "hello world". There are a number of basic tutorial sites online, this is a fairly concise tutorial that tells you how to do that step by step.

Writing to a file

Make a python program called "hello_write.py" that writes "hello world" into a file called "hello_python". This has the information you need to know how to do this, but it also contains a bunch of stuff that isn't particularly relevant to this task.

Calling a subprocess

Make python program called lsfile.py that runs the command "ls" with the "-al" option on the directory "~/" and writes the output to a file called "lspython". In order to do this, you will need to remember how to write something to a file. This has some useful information about how to get the subprocess module to work.

Basic String operations

Make a string in python as follows:
longstring = "1 2 7.77"
Make a python script called splitstring.py that takes that string, and splits it into 3 numbers: x, y, and z. The python documentation isn't the greatest in my opinion, but it does have useful information, if you know where to look. The string type has a split method that you can use to split the string apart into a list of strings. The first step is to split that string into a list of 3 strings, each containing one number. Use the website above to find the how that command works. Once you have split the string, you should have a list of strings. If you want to convert one type to another type, there is a little built in function to do so. For example, if you want an int from something, you do int(something) and python tries to convert something into an int. The same applies to strings and floats. For whatever reason, python won't convert numbers to strings automatically, which is mildly annoying in my opinion, so if you have a number n and you want to concatenate it with a string, you have to do "mystring + str(n)" instead of "mystring + n". For more information about lists, you can look here. That site has a ton of stuff about how to do things with lists, but the part that is most important is accessing lists, which you need to do to assign the x the number representation of the first thing in the list.

Control Flow

Write a python script called forloop.py that prints out the word "forloop" followed by a space, followed by a number. The program should do this five times, printing the numbers 10 through 14. This has some useful information about how to do that. Remember python figures out loop and if level based upon indentation.

Iterating over a list

Make a python script called stringloop.py that prints out every word in a string on its own line. Make a variable which is a string as follows:
whatever = "Python is my favorite thing ever"
Next, turn whatever into a list of the individual words in the string (delimited by space). On the previously mentioned website, there is a section called "lists as an iterable" which shows how this can be done.

Grading

I will be grading your programs primarily on how well they work, but I will also be considering how the code looks. These programs are extremely simple and should be quite small; if you find yourself writing more than a few lines of code, you are probably doing something wrong, and I would recommend you speak to me.