Recent Changes
Tuesday, July 13
-
Session1.2
edited
Session 1: Afternoon
Python first steps: Input, Output and Variables
Topics:
Output: print stat…
Session 1: Afternoon(view changes)
Python first steps: Input, Output and Variables
Topics:
Output: print statement
Interlude: two ways to run your script
Variables: integers, floating-point numbers (decimal), strings (text)
Numerical operations
Strings: quotes, single double and triple!
Strings: escapes and special characters; raw strings
Strings: as sequence type, index and slice (substring)
Concatenating (+) strings
Inserting and formatting variables in strings
Input: raw_input() function
Today You Will:
Learn to write, save and run python scripts that manipulate numbers and strings, take input from a user, and display output to the screen.
A First Python Program: hello.py
In our very own personal first Python program, we will see how to make the computer talk to the world at large, including us.
print 'Hello, World!'
This code snippet (blocks of text in gray boxes like this one indicate python code), when typed exactly as it appears above in an otherwise blank plain text file and saved (for instance, as hello.py) can be run from your shell prompt by typing python hello.py and hitting enter. Seriously -- let's try it!
Some notes on the formatting of the lessons for this course
Periodically in the page these lessons, we may stop with an informative interlude outlined with a horizontal line above and below (like the one two lines up!). In this case, we're taking a quick break to discuss this and other aspects of the formatting.
For this and all further examples, a $ represents your shell prompt, and boldface indicates the commands to type at the prompt. Italics will be used for output you should see when you take the described action.
This concludes our first informative interlude.
Let's start with a few simple steps, which will become ... super ... familiar:
1) Open a terminal window.
2) Make a new directory for this afternoon's exercises, and change to that directory:
$ mkdir S1.2
$ cd S1.2
3) Launch Emacs as a background process:
$ emacs hello.py &
By adding the ampersand, you keep control of your shell prompt after your Emacs client launches. If you don't use the ampersand, you'll need to open another terminal window in a moment to run our first program.
4) Code!
print "Hello, World!"
Copy the above code snippet into your new document (or if you like to rock it old-school, actually type it yourself!).
5) Save your new script:
File -> Save As (old-school alternative: type Ctrl-X, then Ctrl-S)
Type in your filename: "hello.py"
Click Save
6) Go back to your terminal window, and run your script:
$ python hello.py
Hello, World!
The meat of this script (in fact, the entirety of this script) is composed of the most basic python tool for communicating with the user, the print command. The print command essentially does exactly what it says: it outputs the quoted statement that follows the word 'print' to the screen.
Unlike some other languages, in python print adds a newline character (a.k.a. "carriage return", a.a.k.a. "endline character") to the end of your print statement.
That means you see this output:
$ python hello.py
hello world
$
instead of:
$ python hello.py
hello world$
So print statements in python default to have the prompt reappear on the line below the output you've directed, as opposed to immediately after the printed statement. In general, this is handy. It does, however, mean that if you want two statements to be printed on the same line, you need to supply them to the same print statement, separated by commas.
A space character will be inserted between the two (or more) statements thus supplied.
If we change our hello.py program:
print 'hello','world'
then saving and running the modified program gives us:
$ python hello.py
hello world
that is, we get exactly what we got last time.
For a little contrast, try:
print 'hello'
print 'world'
Python's print statement can deal with pretty much anything you can cook up, including any of the variables or data types we discuss throughout this class. In a moment, we'll see that in addition to the strings (series of characters between quotes) you've been using, you could just as easily supply print with numbers or variables to display.
Informative Interlude II: There are two ways to run your script
While you have thus far successfully run at least three python programs by typing python followed by the name of the script, it might be nice to be able to run programs by simply typing the name of the program. To do so, we need to make two modifications to our script:
1) Add a line to the script referencing the python interpreter. This line is called a "shebang" because of the pair of characters used at the beginning of the line: the # ('sh' for "sharp") and the ! ('bang' for, well, sorta obvious reasons!).
So, add the following line to the very beginning of your script:
#!/usr/bin/env python
(don't forget to save!)
2) Change the file permissions on the script to include execute ('x'):
$ chmod +x hello.py
The permissions for a file tell the operating system which users are allowed to read, write, or execute a file. The command we used in the line above, chmod, tells the operating system that it should let us execute the file hello.py
If we list the contents of our S1.2 directory the command ls we will now see our script hello.py listed differently.
$ ls
hello.py*
In most Linux environments, the file will be a pleasant shade of green. In Mac OS, a fiercely prepared red with a trailing asterisk indicates the executable status. Either way, there is something different about this file now: it is executable all on its own. We can take advantage of this by starting with the "dot-slash" characters, then typing the name of the file.
$ ./hello.py
hello world
You certainly don't need to add the shebang and change all of your scripts to be executable; typing python to run your scripts is unlikely to materially contribute to your incipient repetitive stress injuries, but many people prefer to save the keystrokes.
This concludes I.I. II.
Variables: integers, floating-point numbers (decimal), strings (text)
Computer programming is useful because it allows the programmer to tell the computer to perform operations that are too boring, tedious, or difficult for the programmer to do by hand without joining the ranks of the faithfully insane. For a computer program to be able to interact with the user, perform operations on changing sets of data, and make decisions about how to proceed based on conditions specific to each instance of its execution, it employs variables. A variable is a datum with a human-readable name which can change values as the logic of the program dictates, as opposed to other types of data which once declared have a constant, unchanging value.
Python programs use variables to store parameters taken in from the user, the execution environment, or the data your program is being called upon to process. These variables are named whatever you like, within the strictures of a few basic rules:
1) Python variable names are case-sensitive, so Var and var are different variables.
2) Though variable names can contain letters, numbers and underscores ( _ ), they names MUST start with a letter (a-z).
3) Variable names, CANNOT contain special characters (+, -.\ etc.), nor can they be any of the following words that already have special meaning in python:
and assert break class continue def del elif
else except exec finally for from global if
import in is lambda not or pass print
raise return try while yield
For the most part, Emacs will remind you that these words are off-limits by coloring these words in helpful ways when you type them.
Here are some invalid python variable names:
1sample
sampleA.1
class
And here are some good alternatives:
sample_1
SampleA1
bootcamp_class
Variable types
Variables can hold all sorts of values, but will only hold one type of value at a time. Today we'll talk about three types of values: integers, floating point (i.e. decimal) numbers and strings.
Run the following example, through which we'll explore a few properties of variables:
#!/usr/bin/env python
# we'll save this file as hello2.py
# by the way, lines starting with pound signs (#)
# are comments, ignored by the interpreter
s = 'hello world'
i = 42
f = 3.14159
print s
print 'is type',type(s)
print i
print 'is type',type(i)
print f
print 'is type',type(f)
And when we save and execute this file (don't forget to chmod +x, unless you want to type python before the program name), we get:
$ hello2.py
hello world
is type ‹type 'str'›
42
is type ‹type 'int'›
3.14159
is type ‹type 'float'›
We begin the file with the shebang, which is optional. Then a few lines starting in #, which are comments. Use commented lines to describe what bits of code do and how they do it (more detail on this later in the week).
Then come our assignments:
s = 'hello world'
i = 42
f = 3.1412
In general, variables are assigned by typing the name you want to use, followed by a single equals sign, then the value you'd like to store. This is the same whether the variable you're assigning is of type str (a character string), int (whole number), float (non-integer real number) or any number of other fancier things you'll be using in the next two weeks.
While (as your program tells you with the handy type() function) that i is currently an integer, that doesn't mean it cannot change. You can easily re-assign i to be anything that takes your fancy, including the value of another variable. You would do this with a statement such as the following:
i = s
print i
print 'is type',type(i)
The output of these statements will exactly mirror that of the statements earlier using the variable s since i was re-assigned (after it was initially printed, etc) to the value of s.
There are plenty of cases where this is exactly what you want to do, but bear in mind that once no more variables are assigned any particular value, that value is lost forever.
As an example, consider the case where (for some reason) you want to swap the values of two variables s and i. The first step might appear to be a line very much like the i = s statement above, but if you do this, the value of i is lost forever, meaning you can never assign it to s. This may seem like a rather abstract problem, (unless you've read ahead to today's exercises) but you'll encounter similar situations more often than you might think.
Numerical operations
Numerical values can be subjected to a wide variety of operations. While the full list is quite extensive, (see http://docs.python.org/lib/typesnumeric.html for the full workup) the most common operations should be familiar. For the most part, we use basic arithmetic operators exactly as you're used to seeing them:
#!/usr/bin/env python
i = 42
f = 3.14159
# addition uses the plus sign (+)
sum = i + f
# subtraction uses the minus sign (-)
diff = i - f
# multiplication uses the asterisk (*)
prod = i * f
# division uses the slash (/)
quo = i / f
# and exponential powers use a double-asterisk (**)
pow = i ** f
print 'sum',sum
print 'diff',diff
print 'prod',prod
print 'quo',quo
print 'pow',pow
And when we save and execute:
$ hello2.py
sum 45.14159
diff 38.85841
prod 131.94678
quo 13.3690265121
pow 125771.933736
Note that standard order of operations applies, but it's far easier and safer to explicitly order compound operations using parentheses.
String quotes: single, double, and triple!
As mentioned, strings are specified by wrapping a series of characters in quotes. These can be quotes of three different flavors. The first two, single (a.k.a. the apostrophe) and double, are familiar (although don't confuse the single quote with the backtick -- the one above the tilde).
Single and double quotes can more or less be used interchangeably, the only exception being which type of quote is allowed to appear inside the string. If the string is double-quoted, single quotes may appear inside the string, and visa-versa:
#!/usr/bin/env python
s = 'hello "world", if that is your real name.'
print s
s2 ="That's World, to you, buddy."
print s2
gives us:
$ hello.py
hello "world", if that is your real name
That's World, to you, buddy.
The key things to notice here are that double quotes are present in the first, and a single quote appears in the second, but the two cannot be combined. The tool to use here is the extra-spiffy triple quote, which is actually just three single quotes:
#!/usr/bin/env python
s = '''hello "world", if that is your real name.
That's World, to you, buddy.'''
print s
This snippet does exactly the same thing as the last snippet.
Note two aspects of the triple quotes:
1) Both single and double quotes can be used inside triple quotes.
2) Triple quoted strings can span multiple lines, and line breaks inside the quoted string are stored and faithfully displayed in the print operation.
Strings: escapes and special characters; raw strings
Try the following code snippet:
#!/usr/bin/env python
s = 'some\thing bad'
print s
s2 = "somethi\ng else bad"
print s2
s3 = '''something \also bad'''
print s3
s4 = r'\a solu\tio\n'
print s4
s5 = '\\another solu\\tio\\n'
print s5
And you'll be treated to the following gibberish:
$ hello.py
some hing bad
somethi
g else bad
something lso bad
\a solu\tio\n
\another solu\tio\n
This ugly (and possibly loud, if your sound is working properly) mess is caused by escape characters. In python strings, there are several special characters (full list here: http://docs.python.org/ref/strings.html) can be preceded by a backslash (\) to produce special output, such as a tab (\t) newline (\n) or even a bell noise (\a).
This is handy, since it means you can liberally pepper your strings with tabs and line breaks, and lots of our data are conveniently stored in files that are delimited by tabs and line breaks. This might also be a problem, say if you wanted to use a backslash in your string. Python offers two ways around this: the safest is to escape your escape, again with a backslash (see s5 above, '\\'). A fancier way involves a special kind of string, the raw string.
Raw strings start with r' and end with ' and will treat every character between as exactly what it looks like, with the exception of the single quote (which ends the string). If you do use raw strings, watch out for two catches:
1) You must still escape single quotes you want to appear inside the string.
2) The last character of the string cannot be a backslash, since this will escape the closing quote.
There are proper times and places for the use of the raw string method, but in general we recommend just escaping your backslashes. As a final point on escapes, \' and \" provide a means to employ single quotes inside a single quoted string, and likewise double quotes in a double quoted string.
Strings: as sequence type, index and slice (substring)
Strings are merely successions of characters, and python stores and operates on them as such. The official python lingo for something that can be operated on as an ordered series of sub-elements is a 'sequence'. While several python data types are sequences, strings are the only one we'll deal with today. In the next couple of days, however, some of the notation you learn today will come back in other contexts.
The first property of sequences we'll look at is indexing.
#!/usr/bin/env python
# we'll save this file as names.py
name = 'Matthew D. Davis'
middle_initial = name[8] # variable[i] is the ith index of variable.
# (Yeah, comments can be on the same line as other stuff, but's it's recommended that you keep them on their own lines.)
print name
print middle_initial
yields:
$ names.py
Matthew D. Davis
D
Here you've stored a string (my full name), then used a property of sequences, indexing, to ask for a particular character in that string. In the code you see that I've asked for the 8th character in the string, which unless you're a bit odd you'd probably say was the space (since space IS a character) between 'w' and 'D'. The reason 'D' is the 8th character lies in a generalism of python sequences:
NUMBERING STARTS AT ZERO
Yeah. This is important. BOLD CAPITALS AND ITALICS kinds of important.
Using indexing, it's possible to pick out any number of individual characters in this manner and stitch them back together as substrings, but sequences can also be operated on in contiguous subsets of indices, called slices. Slices look like indices, except with two numbers separated by a colon. The slice starts at the index of the first number, and ends at the index before the second number, so in the example above name[7:11] would be ' D. ' (middle initial, period and both flanking spaces). This lets us do handy things like:
#!/usr/bin/env python
name = 'Matthew D. Davis''
middle_initial = name[8]
#the start of the sequence to the end of my first name
first = name[0:7]
# omitting the second number (but using the colon)
# goes to the end of the string
last = name[11:]
print name
print last
print middle_initial
print first
leaving us with:
$ hello.py
Matthew D. Davis
Davis
D
Matthew
A key timesaver here is that omission of the first number starts at the beginning of the sequence, and omission of the second number goes all the way to the end. Of course, this only works if the colon is there, otherwise you just get an index.
Concatenating (+) strings
At this point you've merrily sliced away at my good name; time to put me back together again.
Python offers a simple operator for putting strings together: + We will refer to this operation as string concatenation.
#!/usr/bin/env python
name = 'Matthew D. Davis''
middle_initial = name[8]
first = name[0:7]
last = name[11:]
print name
simple_name = first + ' ' + last
last_first = last + ', ' + first + ' ' + middle_initial + '.'
print simple_name
print last_first
prints:
$ names.py
Matthew D. Davis
Matthew Davis
Davis, Matthew D.
+ basically does what you might imagine and its use is fairly straightforward, but it is oddly similar to another operator we've seen. You know…the plus sign. The difference between string concatenation and numerical addition is only whether the values on either side are strings or numbers. Thus, using one of each, like 'string' + 5 will confuse the crap out of python, and should be avoided. If you meant to concatenate 'string' and the string '5' to get 'string5', the section on inserting and formatting variables in strings will allay your woes.
Coercion
If you had instead somehow managed to get a number like '5' stored as a string (for instance, you took it as input from a file or user), then you would need a way to convince python to let you use the number as … well … a number! Your tools for this are coercion functions. You'll see these in more detail tomorrow, but for now just know that if something looks like a number, but has quotes around it, the functions int() and float() will give you back real honest-to-pete numbers to play with. Use them like so:
integer_text = '5'
decimal_text = '3.14'
print integer_text + decimal_text
integer = int(integer_text)
# this next one would be trouble -- uncomment this line to get your very own personal python bug!
# print integer + decimal_text
decimal = float(decimal_text)
print integer + decimal
to get:
$ names.py
53.14
8.14
You're welcome to mess with these a little bit (try type(integer_text) and type(integer) from earlier, for instance), but their use is incredibly straightforward. Just remember, if you see a TypeError when you try to run a script, this should be the first thing you look for!
Inserting and formatting variables in strings
The final string topic we'll discuss is the specific formatting and insertion of variables into strings. In last_first in the previous example, we added additional characters to the variables we were stitching together by a bunch of concatenations, adding characters like space, commas and periods. The other method python offers, called string interpolation, for injecting variables into strings looks like the following:
last_first = '%s, %s %s.' % (last, first, middle_initial)
This handily replaces all those + operations with a very readable string, where %s represents spots where the variables or values you supply next will be inserted, in the order you supply them. After the string comes a solitary %, then a set of values in parentheses. These are the values to interpolate, and there must be as many of these as there are %s elements in your string. This is a nice way of composing a string of other strings, but its a downright blessing when you want to compose a string including number values. In the case of numbers, you can always supply them to strings with %s elements (like we just did with string variables), but there are also special interpolation operators for numbers %d and %f (corresponding to integer and floating point, respectively). For a full workup, see http://docs.python.org/lib/typesseq-strings.html , but here's a start:
#!/usr/bin/env python
# str%dngthing % (1)
i = 42
f = 3.14159265
string = 'variables can be interpolated as strings here: %s and here %s' % (i,f)
print string
print '''2 decimal places: %.2f, or 2 decimal places padded
to a total width of 5: [%5.2f] (note that the '.' counts as a character).
The brackets above don't do anything--they're just there to show the left padding
again, but using 0's: %07.3f''' % (f,f,f)
which gives:
$ names.py
variables can be interpolated as strings here: 42 and here 3.14159265
2 decimal places: 3.14, or 2 decimal places padded
to a total width of 5: [ 3.14] (note that the '.' counts as a character).
The brackets above don't do anything--they're just there to show the left padding
again, but using 0's: 003.142
Practically speaking, the most commonly used formatting tools are %s to shove variables of any and all types into strings, and %.xf where x is the number of decimal places to display for floating point numbers. Most commonly, you will see and employ a lot of '%.2f' string interpolations, and almost never see or use any of the other numerical interpolators.
Input: raw_input() function
Lastly, we need a way to get data into a program. While there are several ways to gather data from the outside world, the simplest is to just ask. In python, a program asks for information from the user with the raw_input() function, as demonstrated here:
#!/usr/bin/env python
# hello whoever you are!
user = raw_input("what's your name? ")
print 'hello %s!' % (user)
I'll let you figure out what this looks like...
The raw_input() function prompts the user by printing to the screen whatever value is given in the parentheses immediately following the raw_input call, (in this case asking "what's your name?") and then waits for the user to type whatever they want, for as long as they feel like, until the user hits enter. raw_input (which is a function, like int() or float() and is something we'll talk a lot more about later) then takes everything up until the user hits enter and returns that as a string. Again, we'll talk more about this idea, but here all you need to know is that raw_input gives back the user's input as a string, and that gets saved to the variable user using the assignment operator (=), this should be easy stuff at this point. After taking this input, we just spit it right back out (using the string interpolation trick we learned a few minutes ago).
Now that you know everything you need to know to accept basic input, manipulate numbers and strings, store them in variables, and generate meticulously formatted output…try these practice exercises!
Exercises:
1: The Greeter
Write a program that asks for the user's name and wishes them a happy new year.
#!/usr/bin/env python
#It's nice to format the prompt and give people directions when
#asking them to input text
name = raw_input("Hey, what's your name?\n [Press return after entering your name]\n")
#If you don't want print to put a space after everything in the series
#of comma-separated printables, then concatenate them with a plus-sign
#like I've done here to get the ! after the name without a space
print "Well, happy New Year", name + "!"
2: Is that rude?
Ask for their year of birth and compute their age.
Print out their age in the following format:
Your age is 25.
#!/usr/bin/env python
year = int( raw_input("Hey, what year were you born?\n [Press return after entering year]\n") )
print "Sooooo, let's see..."
print "Your age is", str((2009 - year)) + "."
3: Ratio
Read two numbers in (user input)
Print out their ratio.
Figure out how to make this program fail.
#!/usr/bin/env python
#if we make them floats, then we avoid integer truncation (one way to break it)
num1 = float(raw_input("Enter a number and press return: "))
num2 = float(raw_input("Now enter another number and press return: "))
print "The ratio of these two numbers is", (num1 / num2), "to 1."
4: Sum and Mean
Read five numbers in.
Print out their sum and mean.
#!/usr/bin/env python
print "Enter five numbers, pressing return after each number:"
num1 = float(raw_input("Number 1: "))
num2 = float(raw_input("Number 2: "))
num3 = float(raw_input("Number 3: "))
num4 = float(raw_input("Number 4: "))
num5 = float(raw_input("Number 5: "))
sum = num1 + num2 + num3 + num4 + num5
mean = sum / 5
print "The sum of these numbers is", str(sum) + "."
print "And the mean is", str(mean) + "."
5: Swap
The user enters two numbers.
Store the numbers in two variables called input1 and input2
Swap the values of the two variables so that input1 has the value of input2 and vice versa.
Print out the two variables.
#!/usr/bin/env python
input1 = float(raw_input("Please enter a number and press return:\n"))
input2 = float(raw_input("Please enter another number and press return:\n"))
tmp = input1
input1 = input2
input2 = tmp
print "Ohhh, so you entered", str(input1), "and", str(input2)
Challenges:
6: Quickswap
perform #5 above in only 1 line of code, and without using any variables other than input1 and input2 (hint: you need tuples; try google!)
#!/usr/bin/env python
input1 = float(raw_input("Please enter a number and press return:\n"))
input2 = float(raw_input("Please enter another number and press return:\n"))
#If you googled for tuples and quick swap, you hopefully read something about "tuple packing"
#This is not available in most programming languages, in that you are assigning input2 to input1 and input1 to input2 simultaneously (i.e. from left to right as ordered pairs across the equals sign)
input1, input2 = input2, input1
print "Ohhh, so you entered", str(input1), "and", str(input2)
7: Index-tastic
ask the user to specify a number of digits (e.g. 1 digit numbers, 2 digit numbers, etc)
ask the user to supply 5 numbers of that many digits separated by spaces
parse these numbers out, and calculate sum and mean as in #4
Try doing this without using any additional variables! (just the one that contains the string you read in)
#!/usr/bin/env python
# get our length
diglen = int(raw_input("Please enter a digit length and press return\n"))
#get our numbers
numstr = raw_input("Please enter 5 integers of the length you specified separating each by a space.\n")
#create an idx variable that can change as we move through the str with slices
idx = diglen
num1 = numstr[0:idx]
#gotta start off by moving one spot
idx += 1
num2 = numstr[idx: idx + diglen]
# then we're going to move by the diglen and one more
# this should work until the last one
idx += diglen + 1
num3 = numstr[idx: idx + diglen]
idx += diglen + 1
num4 = numstr[idx: idx + diglen]
idx += diglen + 1
#And the last one just print to the end of the str
num5 = numstr[idx:]
print num1
print num2
print num3
print num4
print num5
8: Escape!
reproduce the triple-quoted string from earlier:
s = '''hello "world", if that is your real name.
That's World, to you'''
#!/usr/bin/env python
s = "hello \"world\", if that is your real name.\nThat\'s World, to you"
print s
in just one string using single or double quotes. Make sure you have the line break and all the quotes and apostrophes in there!
4:18 pm
Thursday, July 8
-
Session1.2
edited
Session 1: Afternoon
Python first steps: Input, Output and Variables
Topics:
Output: print stat…
(view changes)Session 1: Afternoon
Python first steps: Input, Output and Variables
Topics:
Output: print statement
Interlude: two ways to run your script
Variables: integers, floating-point numbers (decimal), strings (text)
Numerical operations
Strings: quotes, single double and triple!
Strings: escapes and special characters; raw strings
Strings: as sequence type, index and slice (substring)
Concatenating (+) strings
Inserting and formatting variables in strings
Input: raw_input() function
Today You Will:
Learn to write, save and run python scripts that manipulate numbers and strings, take input from a user, and display output to the screen.
A First Python Program: hello.py
In our very own personal first Python program, we will see how to make the computer talk to the world at large, including us.
print 'Hello, World!'
This code snippet (blocks of text in gray boxes like this one indicate python code), when typed exactly as it appears above in an otherwise blank plain text file and saved (for instance, as hello.py) can be run from your shell prompt by typing python hello.py and hitting enter. Seriously -- let's try it!
Some notes on the formatting of the lessons for this course
Periodically in the page these lessons, we may stop with an informative interlude outlined with a horizontal line above and below (like the one two lines up!). In this case, we're taking a quick break to discuss this and other aspects of the formatting.
For this and all further examples, a $ represents your shell prompt, and boldface indicates the commands to type at the prompt. Italics will be used for output you should see when you take the described action.
This concludes our first informative interlude.
Let's start with a few simple steps, which will become ... super ... familiar:
1) Open a terminal window.
2) Make a new directory for this afternoon's exercises, and change to that directory:
$ mkdir S1.2
$ cd S1.2
3) Launch Emacs as a background process:
$ emacs hello.py &
By adding the ampersand, you keep control of your shell prompt after your Emacs client launches. If you don't use the ampersand, you'll need to open another terminal window in a moment to run our first program.
4) Code!
print "Hello, World!"
Copy the above code snippet into your new document (or if you like to rock it old-school, actually type it yourself!).
5) Save your new script:
File -> Save As (old-school alternative: type Ctrl-X, then Ctrl-S)
Type in your filename: "hello.py"
Click Save
6) Go back to your terminal window, and run your script:
$ python hello.py
Hello, World!
The meat of this script (in fact, the entirety of this script) is composed of the most basic python tool for communicating with the user, the print command. The print command essentially does exactly what it says: it outputs the quoted statement that follows the word 'print' to the screen.
Unlike some other languages, in python print adds a newline character (a.k.a. "carriage return", a.a.k.a. "endline character") to the end of your print statement.
That means you see this output:
$ python hello.py
hello world
$
instead of:
$ python hello.py
hello world$
So print statements in python default to have the prompt reappear on the line below the output you've directed, as opposed to immediately after the printed statement. In general, this is handy. It does, however, mean that if you want two statements to be printed on the same line, you need to supply them to the same print statement, separated by commas.
A space character will be inserted between the two (or more) statements thus supplied.
If we change our hello.py program:
print 'hello','world'
then saving and running the modified program gives us:
$ python hello.py
hello world
that is, we get exactly what we got last time.
For a little contrast, try:
print 'hello'
print 'world'
Python's print statement can deal with pretty much anything you can cook up, including any of the variables or data types we discuss throughout this class. In a moment, we'll see that in addition to the strings (series of characters between quotes) you've been using, you could just as easily supply print with numbers or variables to display.
Informative Interlude II: There are two ways to run your script
While you have thus far successfully run at least three python programs by typing python followed by the name of the script, it might be nice to be able to run programs by simply typing the name of the program. To do so, we need to make two modifications to our script:
1) Add a line to the script referencing the python interpreter. This line is called a "shebang" because of the pair of characters used at the beginning of the line: the # ('sh' for "sharp") and the ! ('bang' for, well, sorta obvious reasons!).
So, add the following line to the very beginning of your script:
#!/usr/bin/env python
(don't forget to save!)
2) Change the file permissions on the script to include execute ('x'):
$ chmod +x hello.py
The permissions for a file tell the operating system which users are allowed to read, write, or execute a file. The command we used in the line above, chmod, tells the operating system that it should let us execute the file hello.py
If we list the contents of our S1.2 directory the command ls we will now see our script hello.py listed differently.
$ ls
hello.py*
In most Linux environments, the file will be a pleasant shade of green. In Mac OS, a fiercely prepared red with a trailing asterisk indicates the executable status. Either way, there is something different about this file now: it is executable all on its own. We can take advantage of this by starting with the "dot-slash" characters, then typing the name of the file.
$ ./hello.py
hello world
You certainly don't need to add the shebang and change all of your scripts to be executable; typing python to run your scripts is unlikely to materially contribute to your incipient repetitive stress injuries, but many people prefer to save the keystrokes.
This concludes I.I. II.
Variables: integers, floating-point numbers (decimal), strings (text)
Computer programming is useful because it allows the programmer to tell the computer to perform operations that are too boring, tedious, or difficult for the programmer to do by hand without joining the ranks of the faithfully insane. For a computer program to be able to interact with the user, perform operations on changing sets of data, and make decisions about how to proceed based on conditions specific to each instance of its execution, it employs variables. A variable is a datum with a human-readable name which can change values as the logic of the program dictates, as opposed to other types of data which once declared have a constant, unchanging value.
Python programs use variables to store parameters taken in from the user, the execution environment, or the data your program is being called upon to process. These variables are named whatever you like, within the strictures of a few basic rules:
1) Python variable names are case-sensitive, so Var and var are different variables.
2) Though variable names can contain letters, numbers and underscores ( _ ), they names MUST start with a letter (a-z).
3) Variable names, CANNOT contain special characters (+, -.\ etc.), nor can they be any of the following words that already have special meaning in python:
and assert break class continue def del elif
else except exec finally for from global if
import in is lambda not or pass print
raise return try while yield
For the most part, Emacs will remind you that these words are off-limits by coloring these words in helpful ways when you type them.
Here are some invalid python variable names:
1sample
sampleA.1
class
And here are some good alternatives:
sample_1
SampleA1
bootcamp_class
Variable types
Variables can hold all sorts of values, but will only hold one type of value at a time. Today we'll talk about three types of values: integers, floating point (i.e. decimal) numbers and strings.
Run the following example, through which we'll explore a few properties of variables:
#!/usr/bin/env python
# we'll save this file as hello2.py
# by the way, lines starting with pound signs (#)
# are comments, ignored by the interpreter
s = 'hello world'
i = 42
f = 3.14159
print s
print 'is type',type(s)
print i
print 'is type',type(i)
print f
print 'is type',type(f)
And when we save and execute this file (don't forget to chmod +x, unless you want to type python before the program name), we get:
$ hello2.py
hello world
is type ‹type 'str'›
42
is type ‹type 'int'›
3.14159
is type ‹type 'float'›
We begin the file with the shebang, which is optional. Then a few lines starting in #, which are comments. Use commented lines to describe what bits of code do and how they do it (more detail on this later in the week).
Then come our assignments:
s = 'hello world'
i = 42
f = 3.1412
In general, variables are assigned by typing the name you want to use, followed by a single equals sign, then the value you'd like to store. This is the same whether the variable you're assigning is of type str (a character string), int (whole number), float (non-integer real number) or any number of other fancier things you'll be using in the next two weeks.
While (as your program tells you with the handy type() function) that i is currently an integer, that doesn't mean it cannot change. You can easily re-assign i to be anything that takes your fancy, including the value of another variable. You would do this with a statement such as the following:
i = s
print i
print 'is type',type(i)
The output of these statements will exactly mirror that of the statements earlier using the variable s since i was re-assigned (after it was initially printed, etc) to the value of s.
There are plenty of cases where this is exactly what you want to do, but bear in mind that once no more variables are assigned any particular value, that value is lost forever.
As an example, consider the case where (for some reason) you want to swap the values of two variables s and i. The first step might appear to be a line very much like the i = s statement above, but if you do this, the value of i is lost forever, meaning you can never assign it to s. This may seem like a rather abstract problem, (unless you've read ahead to today's exercises) but you'll encounter similar situations more often than you might think.
Numerical operations
Numerical values can be subjected to a wide variety of operations. While the full list is quite extensive, (see http://docs.python.org/lib/typesnumeric.html for the full workup) the most common operations should be familiar. For the most part, we use basic arithmetic operators exactly as you're used to seeing them:
#!/usr/bin/env python
i = 42
f = 3.14159
# addition uses the plus sign (+)
sum = i + f
# subtraction uses the minus sign (-)
diff = i - f
# multiplication uses the asterisk (*)
prod = i * f
# division uses the slash (/)
quo = i / f
# and exponential powers use a double-asterisk (**)
pow = i ** f
print 'sum',sum
print 'diff',diff
print 'prod',prod
print 'quo',quo
print 'pow',pow
And when we save and execute:
$ hello2.py
sum 45.14159
diff 38.85841
prod 131.94678
quo 13.3690265121
pow 125771.933736
Note that standard order of operations applies, but it's far easier and safer to explicitly order compound operations using parentheses.
String quotes: single, double, and triple!
As mentioned, strings are specified by wrapping a series of characters in quotes. These can be quotes of three different flavors. The first two, single (a.k.a. the apostrophe) and double, are familiar (although don't confuse the single quote with the backtick -- the one above the tilde).
Single and double quotes can more or less be used interchangeably, the only exception being which type of quote is allowed to appear inside the string. If the string is double-quoted, single quotes may appear inside the string, and visa-versa:
#!/usr/bin/env python
s = 'hello "world", if that is your real name.'
print s
s2 ="That's World, to you, buddy."
print s2
gives us:
$ hello.py
hello "world", if that is your real name
That's World, to you, buddy.
The key things to notice here are that double quotes are present in the first, and a single quote appears in the second, but the two cannot be combined. The tool to use here is the extra-spiffy triple quote, which is actually just three single quotes:
#!/usr/bin/env python
s = '''hello "world", if that is your real name.
That's World, to you, buddy.'''
print s
This snippet does exactly the same thing as the last snippet.
Note two aspects of the triple quotes:
1) Both single and double quotes can be used inside triple quotes.
2) Triple quoted strings can span multiple lines, and line breaks inside the quoted string are stored and faithfully displayed in the print operation.
Strings: escapes and special characters; raw strings
Try the following code snippet:
#!/usr/bin/env python
s = 'some\thing bad'
print s
s2 = "somethi\ng else bad"
print s2
s3 = '''something \also bad'''
print s3
s4 = r'\a solu\tio\n'
print s4
s5 = '\\another solu\\tio\\n'
print s5
And you'll be treated to the following gibberish:
$ hello.py
some hing bad
somethi
g else bad
something lso bad
\a solu\tio\n
\another solu\tio\n
This ugly (and possibly loud, if your sound is working properly) mess is caused by escape characters. In python strings, there are several special characters (full list here: http://docs.python.org/ref/strings.html) can be preceded by a backslash (\) to produce special output, such as a tab (\t) newline (\n) or even a bell noise (\a).
This is handy, since it means you can liberally pepper your strings with tabs and line breaks, and lots of our data are conveniently stored in files that are delimited by tabs and line breaks. This might also be a problem, say if you wanted to use a backslash in your string. Python offers two ways around this: the safest is to escape your escape, again with a backslash (see s5 above, '\\'). A fancier way involves a special kind of string, the raw string.
Raw strings start with r' and end with ' and will treat every character between as exactly what it looks like, with the exception of the single quote (which ends the string). If you do use raw strings, watch out for two catches:
1) You must still escape single quotes you want to appear inside the string.
2) The last character of the string cannot be a backslash, since this will escape the closing quote.
There are proper times and places for the use of the raw string method, but in general we recommend just escaping your backslashes. As a final point on escapes, \' and \" provide a means to employ single quotes inside a single quoted string, and likewise double quotes in a double quoted string.
Strings: as sequence type, index and slice (substring)
Strings are merely successions of characters, and python stores and operates on them as such. The official python lingo for something that can be operated on as an ordered series of sub-elements is a 'sequence'. While several python data types are sequences, strings are the only one we'll deal with today. In the next couple of days, however, some of the notation you learn today will come back in other contexts.
The first property of sequences we'll look at is indexing.
#!/usr/bin/env python
# we'll save this file as names.py
name = 'Matthew D. Davis'
middle_initial = name[8] # variable[i] is the ith index of variable.
# (Yeah, comments can be on the same line as other stuff, but's it's recommended that you keep them on their own lines.)
print name
print middle_initial
yields:
$ names.py
Matthew D. Davis
D
Here you've stored a string (my full name), then used a property of sequences, indexing, to ask for a particular character in that string. In the code you see that I've asked for the 8th character in the string, which unless you're a bit odd you'd probably say was the space (since space IS a character) between 'w' and 'D'. The reason 'D' is the 8th character lies in a generalism of python sequences:
NUMBERING STARTS AT ZERO
Yeah. This is important. BOLD CAPITALS AND ITALICS kinds of important.
Using indexing, it's possible to pick out any number of individual characters in this manner and stitch them back together as substrings, but sequences can also be operated on in contiguous subsets of indices, called slices. Slices look like indices, except with two numbers separated by a colon. The slice starts at the index of the first number, and ends at the index before the second number, so in the example above name[7:11] would be ' D. ' (middle initial, period and both flanking spaces). This lets us do handy things like:
#!/usr/bin/env python
name = 'Matthew D. Davis''
middle_initial = name[8]
#the start of the sequence to the end of my first name
first = name[0:7]
# omitting the second number (but using the colon)
# goes to the end of the string
last = name[11:]
print name
print last
print middle_initial
print first
leaving us with:
$ hello.py
Matthew D. Davis
Davis
D
Matthew
A key timesaver here is that omission of the first number starts at the beginning of the sequence, and omission of the second number goes all the way to the end. Of course, this only works if the colon is there, otherwise you just get an index.
Concatenating (+) strings
At this point you've merrily sliced away at my good name; time to put me back together again.
Python offers a simple operator for putting strings together: + We will refer to this operation as string concatenation.
#!/usr/bin/env python
name = 'Matthew D. Davis''
middle_initial = name[8]
first = name[0:7]
last = name[11:]
print name
simple_name = first + ' ' + last
last_first = last + ', ' + first + ' ' + middle_initial + '.'
print simple_name
print last_first
prints:
$ names.py
Matthew D. Davis
Matthew Davis
Davis, Matthew D.
+ basically does what you might imagine and its use is fairly straightforward, but it is oddly similar to another operator we've seen. You know…the plus sign. The difference between string concatenation and numerical addition is only whether the values on either side are strings or numbers. Thus, using one of each, like 'string' + 5 will confuse the crap out of python, and should be avoided. If you meant to concatenate 'string' and the string '5' to get 'string5', the section on inserting and formatting variables in strings will allay your woes.
Coercion
If you had instead somehow managed to get a number like '5' stored as a string (for instance, you took it as input from a file or user), then you would need a way to convince python to let you use the number as … well … a number! Your tools for this are coercion functions. You'll see these in more detail tomorrow, but for now just know that if something looks like a number, but has quotes around it, the functions int() and float() will give you back real honest-to-pete numbers to play with. Use them like so:
integer_text = '5'
decimal_text = '3.14'
print integer_text + decimal_text
integer = int(integer_text)
# this next one would be trouble -- uncomment this line to get your very own personal python bug!
# print integer + decimal_text
decimal = float(decimal_text)
print integer + decimal
to get:
$ names.py
53.14
8.14
You're welcome to mess with these a little bit (try type(integer_text) and type(integer) from earlier, for instance), but their use is incredibly straightforward. Just remember, if you see a TypeError when you try to run a script, this should be the first thing you look for!
Inserting and formatting variables in strings
The final string topic we'll discuss is the specific formatting and insertion of variables into strings. In last_first in the previous example, we added additional characters to the variables we were stitching together by a bunch of concatenations, adding characters like space, commas and periods. The other method python offers, called string interpolation, for injecting variables into strings looks like the following:
last_first = '%s, %s %s.' % (last, first, middle_initial)
This handily replaces all those + operations with a very readable string, where %s represents spots where the variables or values you supply next will be inserted, in the order you supply them. After the string comes a solitary %, then a set of values in parentheses. These are the values to interpolate, and there must be as many of these as there are %s elements in your string. This is a nice way of composing a string of other strings, but its a downright blessing when you want to compose a string including number values. In the case of numbers, you can always supply them to strings with %s elements (like we just did with string variables), but there are also special interpolation operators for numbers %d and %f (corresponding to integer and floating point, respectively). For a full workup, see http://docs.python.org/lib/typesseq-strings.html , but here's a start:
#!/usr/bin/env python
# str%dngthing % (1)
i = 42
f = 3.14159265
string = 'variables can be interpolated as strings here: %s and here %s' % (i,f)
print string
print '''2 decimal places: %.2f, or 2 decimal places padded
to a total width of 5: [%5.2f] (note that the '.' counts as a character).
The brackets above don't do anything--they're just there to show the left padding
again, but using 0's: %07.3f''' % (f,f,f)
which gives:
$ names.py
variables can be interpolated as strings here: 42 and here 3.14159265
2 decimal places: 3.14, or 2 decimal places padded
to a total width of 5: [ 3.14] (note that the '.' counts as a character).
The brackets above don't do anything--they're just there to show the left padding
again, but using 0's: 003.142
Practically speaking, the most commonly used formatting tools are %s to shove variables of any and all types into strings, and %.xf where x is the number of decimal places to display for floating point numbers. Most commonly, you will see and employ a lot of '%.2f' string interpolations, and almost never see or use any of the other numerical interpolators.
Input: raw_input() function
Lastly, we need a way to get data into a program. While there are several ways to gather data from the outside world, the simplest is to just ask. In python, a program asks for information from the user with the raw_input() function, as demonstrated here:
#!/usr/bin/env python
# hello whoever you are!
user = raw_input("what's your name? ")
print 'hello %s!' % (user)
I'll let you figure out what this looks like...
The raw_input() function prompts the user by printing to the screen whatever value is given in the parentheses immediately following the raw_input call, (in this case asking "what's your name?") and then waits for the user to type whatever they want, for as long as they feel like, until the user hits enter. raw_input (which is a function, like int() or float() and is something we'll talk a lot more about later) then takes everything up until the user hits enter and returns that as a string. Again, we'll talk more about this idea, but here all you need to know is that raw_input gives back the user's input as a string, and that gets saved to the variable user using the assignment operator (=), this should be easy stuff at this point. After taking this input, we just spit it right back out (using the string interpolation trick we learned a few minutes ago).
Now that you know everything you need to know to accept basic input, manipulate numbers and strings, store them in variables, and generate meticulously formatted output…try these practice exercises!
Exercises:
1: The Greeter
Write a program that asks for the user's name and wishes them a happy new year.
#!/usr/bin/env python
#It's nice to format the prompt and give people directions when
#asking them to input text
name = raw_input("Hey, what's your name?\n [Press return after entering your name]\n")
#If you don't want print to put a space after everything in the series
#of comma-separated printables, then concatenate them with a plus-sign
#like I've done here to get the ! after the name without a space
print "Well, happy New Year", name + "!"
2: Is that rude?
Ask for their year of birth and compute their age.
Print out their age in the following format:
Your age is 25.
#!/usr/bin/env python
year = int( raw_input("Hey, what year were you born?\n [Press return after entering year]\n") )
print "Sooooo, let's see..."
print "Your age is", str((2009 - year)) + "."
3: Ratio
Read two numbers in (user input)
Print out their ratio.
Figure out how to make this program fail.
#!/usr/bin/env python
#if we make them floats, then we avoid integer truncation (one way to break it)
num1 = float(raw_input("Enter a number and press return: "))
num2 = float(raw_input("Now enter another number and press return: "))
print "The ratio of these two numbers is", (num1 / num2), "to 1."
4: Sum and Mean
Read five numbers in.
Print out their sum and mean.
#!/usr/bin/env python
print "Enter five numbers, pressing return after each number:"
num1 = float(raw_input("Number 1: "))
num2 = float(raw_input("Number 2: "))
num3 = float(raw_input("Number 3: "))
num4 = float(raw_input("Number 4: "))
num5 = float(raw_input("Number 5: "))
sum = num1 + num2 + num3 + num4 + num5
mean = sum / 5
print "The sum of these numbers is", str(sum) + "."
print "And the mean is", str(mean) + "."
5: Swap
The user enters two numbers.
Store the numbers in two variables called input1 and input2
Swap the values of the two variables so that input1 has the value of input2 and vice versa.
Print out the two variables.
#!/usr/bin/env python
input1 = float(raw_input("Please enter a number and press return:\n"))
input2 = float(raw_input("Please enter another number and press return:\n"))
tmp = input1
input1 = input2
input2 = tmp
print "Ohhh, so you entered", str(input1), "and", str(input2)
Challenges:
6: Quickswap
perform #5 above in only 1 line of code, and without using any variables other than input1 and input2 (hint: you need tuples; try google!)
#!/usr/bin/env python
input1 = float(raw_input("Please enter a number and press return:\n"))
input2 = float(raw_input("Please enter another number and press return:\n"))
#If you googled for tuples and quick swap, you hopefully read something about "tuple packing"
#This is not available in most programming languages, in that you are assigning input2 to input1 and input1 to input2 simultaneously (i.e. from left to right as ordered pairs across the equals sign)
input1, input2 = input2, input1
print "Ohhh, so you entered", str(input1), "and", str(input2)
7: Index-tastic
ask the user to specify a number of digits (e.g. 1 digit numbers, 2 digit numbers, etc)
ask the user to supply 5 numbers of that many digits separated by spaces
parse these numbers out, and calculate sum and mean as in #4
Try doing this without using any additional variables! (just the one that contains the string you read in)
#!/usr/bin/env python
# get our length
diglen = int(raw_input("Please enter a digit length and press return\n"))
#get our numbers
numstr = raw_input("Please enter 5 integers of the length you specified separating each by a space.\n")
#create an idx variable that can change as we move through the str with slices
idx = diglen
num1 = numstr[0:idx]
#gotta start off by moving one spot
idx += 1
num2 = numstr[idx: idx + diglen]
# then we're going to move by the diglen and one more
# this should work until the last one
idx += diglen + 1
num3 = numstr[idx: idx + diglen]
idx += diglen + 1
num4 = numstr[idx: idx + diglen]
idx += diglen + 1
#And the last one just print to the end of the str
num5 = numstr[idx:]
print num1
print num2
print num3
print num4
print num5
8: Escape!
reproduce the triple-quoted string from earlier:
s = '''hello "world", if that is your real name.
That's World, to you'''
#!/usr/bin/env python
s = "hello \"world\", if that is your real name.\nThat\'s World, to you"
print s
in just one string using single or double quotes. Make sure you have the line break and all the quotes and apostrophes in there!
11:45 am -
Session1.2
edited
... Learn to write, save and run python scripts that manipulate numbers and strings, take input fr…
(view changes)...Learn to write, save and run python scripts that manipulate numbers and strings, take input from a user, and display output to the screen.
A First Python Program: hello.py
print 'hello world'In our very own personal first Python program, we will see how to make the computer talk to the world at large, including us.
print 'Hello, World!'
This code...like this one indicate python...hitting enter.Seriously--trySeriously -- let's try it!
Some notes on the formatting of the lessons for this course
Periodically in the page these lessons, we may stop with an informative interlude outlined with a horizontal line above and below (like the one two lines up!). In this case, we're taking a quick break to discuss this and other aspects of the formatting.
For this...boldface indicates the commandsyou should type.to type at the prompt. Italics will...described action.
This concludes our first informative interlude.
Let's start...super ...familiar.familiar:
1) Open a terminal window.
2) Make a new directory for this afternoon's exercises, and change to that directory:
...3) Launch Emacs as a background process:
$ emacs hello.py &
...prompt after your Emacs client launches. If
4) Code!
print"hello world""Hello, World!"
Copy the...type ityourself!)yourself!).
5) Save your newscriptscript:
File->Save
File -> Save As(old-school alternative: type Ctrl-X, then Ctrl-S)
type
Type in your filename: "hello.py"
click
Click Save
6) Go back to your terminal window, and run your script:
$ python hello.py
hello worldHello, World!
The meat...outputs the quoted statement that
Unlike some other languages, in python print adds a newline character (a.k.a. "carriage return", a.a.k.a. "endline character") to the end of your print statement.
That means you see this output:
...So print statements in python default to have the prompt reappear on the line below the output you've directed, as opposed to immediately after the printed statement. In general, this is handy. It does, however, mean that if you want two statements to be printed on the same line, you need to supply them to the same print statement, separated by commas.
A space character will be inserted between the two (or more) statements thus supplied.
...change ourprogram hello.py:hello.py program:
print 'hello','world'
then saving and running the modified program gives us:
$ python hello.py
hello world
or, in other words,that is, we get exactly what
For a little contrast, try:
print 'hello'
print 'world'
Python's print statement can deal with pretty much anything you can cook up, including any of the variables or data types we discuss throughout this class. In a moment, we'll see that in addition to the strings (series of characters between quotes) you've been using, you could just as easily supply print with numbers or variables to display.
Interlude:Informative Interlude II: There are two ways
While you have thus far successfully run at least three python programs by typing python followed by the name of the script, it might be nice to be able to run programs by simply typing the name of the program. To do so, we need to make two modifications to our script:
...referencing the python interpreter. This...the #('sh')('sh' for "sharp") and the !('bang').('bang' for, well, sorta obvious reasons!).
So, add the following line to the very beginning of your script:
#!/usr/bin/env python
(don't forget to save!)
...Change the file permissions on
$ chmod +x hello.py
...permissions forany givena file tell
If we list the contents of our S1.2 directory the command ls we will now see our script hello.py listed differently.
$ ls
hello.py*
...green. InOSX,Mac OS, a fiercely...trailing asteriskto indicateindicates the executable
$ ./hello.py
hello world
You certainly don't need to add the shebang and change all of your scripts to be executable; typing python to run your scripts is unlikely to materially contribute to your incipient repetitive stress injuries, but many people prefer to save the keystrokes.
This concludes I.I. II.
Variables: integers, floating-point numbers (decimal), strings (text)
...hand withoutgoing crazy.joining the ranks of the faithfully insane. For a...employs variables.Variables are asA variable is a datum with a human-readablenames that refername which can change values as the logic of the program dictates, as opposed to other types of data which once declared have avalue stored somewhere in the computer's memory.constant, unchanging value.
Python programs...These variables are named whatever
1) Python variable names are case-sensitive, so Var and var are different variables.
...and underscores(_),( _ ), they names
3) Variable names, CANNOT contain special characters (+, -.\ etc.), nor can they be any of the following words that already have special meaning in python:
and assert break class continue def del elif
...import in is lambda not or pass print
raise return try while yield
...most part,emacsEmacs will remind
Here are some invalid python variable names:
1sample
...bootcamp_class
Variable types
...floating point(or(i.e. decimal) numbers
Run the following example, through which we'll explore a few properties of variables:
#!/usr/bin/env python
#variable madnesswe'll save this file as hello2.py
# by the way, lines starting with pound signs (#)
# are comments, ignored by the interpreter
...print f
print 'is type',type(f)
...execute thisfile,file (don't forget to chmod +x, unless you want to type python before the program name), we get:
$hello.pyhello2.py
hello world
is type ‹type 'str'›
...i = 42
f = 3.1412
...variable you'restoringassigning is of...you'll bemeetingusing in the...type() function) that i is
i = s
print i
...As an example, consider the case where (for some reason) you want to swap the values of two variables s and i. The first step might appear to be a line very much like the i = s statement above, but if you do this, the value of i is lost forever, meaning you can never assign it to s. This may seem like a rather abstract problem, (unless you've read ahead to today's exercises) but you'll encounter similar situations more often than you might think.
Numerical operations
...variety ofhandy numericaloperations. While
#!/usr/bin/env python
i = 42
...# addition uses the plus sign (+)
sum = i + f
#subtraction# subtraction uses the
diff = i - f
#multiplication# multiplication uses the
prod = i * f
#division# division uses the
quo = i / f
#and# and exponential powers
pow = i ** f
print 'sum',sum
...print 'pow',pow
And when we save and execute:
$hello.pyhello2.py
sum 45.14159
diff 38.85841
...quo 13.3690265121
pow 125771.933736
This is all pretty straightforward. NoteNote that standard...using parentheses.
Strings: quotes, single double
String quotes: single, double, and triple!
As mentioned, strings are specified by wrapping a series of characters in quotes. These can be quotes of three different flavors. The first two, single (a.k.a. the apostrophe) and double, are familiar (although don't confuse the single quote with the backtick -- the one above the tilde).
Single and double quotes can more or less be used interchangeably, the only exception being which type of quote is allowed to appear inside the string. If the string is double-quoted, single quotes may appear inside the string, and visa-versa:
...This snippet does exactly the same thing as the last snippet.
Note two aspects of the triple quotes:
Both1) Both single and...triple quotes.
Triple
2) Triple quoted strings
Strings: escapes and special characters; raw strings
Try the following code snippet:
...\a solu\tio\n
\another solu\tio\n
...python strings, there are several special characters (full...of our data are conveniently
Raw strings start with r' and end with ' and will treat every character between as exactly what it looks like, with the exception of the single quote (which ends the string). If you do use raw strings, watch out for two catches:
You1) You must still...inside thestringstring.
The
2) The last character
There are proper times and places for the use of the raw string method, but in general we recommend just escaping your backslashes. As a final point on escapes, \' and \" provide a means to employ single quotes inside a single quoted string, and likewise double quotes in a double quoted string.
Strings: as sequence type, index and slice (substring)
Strings areclearly a successionmerely successions of characters,
The first property of sequences we'll look at is indexing.
#!/usr/bin/env python
# we'll save this file as names.py
name = 'Matthew D. Davis'
middle_initial = name[8] # variable[i] is the ith index of variable.
...print middle_initial
yields:
$hello.pynames.py
Matthew D. Davis
D
...Concatenating (+) strings
At this point you've merrily sliced away at my good name; time to put me back together again.
...together: + We will refer to this operation as string concatenation.
#!/usr/bin/env python
name = 'Matthew D. Davis''
...print last_first
prints:
$hello.pynames.py
Matthew D. Davis
Matthew Davis
Davis, Matthew D.
...strings willsolveallay your woes.
Coercion
...the numberas…well…aas … well … a number! Your...see theseagain andin more
integer_text = '5'
decimal_text = '3.14'
print integer_text + decimal_text
integer = int(integer_text)
#this# this next one would betrouble--uncommenttrouble -- uncomment this line to get...very own personal python bug!
#print
# print integer +
decimal = float(decimal_text)
print integer + decimal
to get:
$hello.pynames.py
53.14
8.14
you'reYou're welcome to
Inserting and formatting variables in strings
...method pythonoffersoffers, called string interpolation, for injecting
last_first = '%s, %s %s.' % (last, first, middle_initial)
thisThis handily replaces...%s elements(just like(like we just...workup, seehttp://docs.python.org/lib/typesseq-strings.html,http://docs.python.org/lib/typesseq-strings.html , but here's
#!/usr/bin/env python
# str%dngthing % (1)
...again, but using 0's: %07.3f''' % (f,f,f)
which gives:
$hello.pynames.py
variables can be interpolated as strings here: 42 and here 3.14159265
2 decimal places: 3.14, or 2 decimal places padded
...print 'hello %s!' % (user)
I'll let you figure out what this looks like...
...raw_input call,(In(in this case..."what's yourname? ")name?") and then...the userhithits enter and...few minutesago)ago).
Now that you know everything you need to know to accept basic input, manipulate numbers and strings, store them in variables, and generate meticulously formatted output…try these practice exercises!
Exercises:
1:12 am
Wednesday, June 23
-
Session2.1
edited
... 2) The list operator pop() removes the last item from the list and returns the variable
Chang…
(view changes)...2) The list operator pop() removes the last item from the list and returns the variable
Changing lists in place
...in place.
#and now for some numbers
#create list of zeros
6:22 pm
Thursday, July 23
-
Session10.1
edited
... 0. This is the fourth iteration of this course, and I believe that we've made it better each t…
(view changes)...0. This is the fourth iteration of this course, and I believe that we've made it better each time (pity those in the first). However, I bet we can make it better the fifth time around, too-- and we'd like your help. Could you fill out this course evaluation?
{eval.txt}
In the spirit of the course, just open it up in your favorite text editor and save it from there.
In order to preserve anonymity, I've set up a separate gmail account. The username is 'ipb.evaluations' and the password is 'evaluation'. Please complete the form above, log in to that account, and send an email to intro.prog.bioinformatics@gmail.com with the form attached. Then log out-- I'm not sure how high gmail's tolerance for multiple-users-logged-in is.
After you've done this, your first priority should be completing the project. If you are finished, you can explore the following exercises.
5:02 pm -
Session10.1
edited
... Exercises:
0. This is the fourth iteration of this course, and I believe that we've made it b…
(view changes)...Exercises:
0. This is the fourth iteration of this course, and I believe that we've made it better each time (pity those in the first). However, I bet we can make it better the fifth time around, too-- and we'd like your help. Could you fill out this course evaluation?
{eval.txt}
In order to preserve anonymity, I've set up a separate gmail account. The username is 'ipb.evaluations' and the password is 'evaluation'. Please complete the form above, log in to that account, and send an email to intro.prog.bioinformatics@gmail.com with the form attached. Then log out-- I'm not sure how high gmail's tolerance for multiple-users-logged-in is.
After you've done this, your first priority should be completing the project. If you are finished, you can explore the following exercises.
4:58 pm -
eval.txt (deleted)
uploaded
4:58 pm -
Session10.1
edited
PYTHONPATH
HTTP SLURP
Evaluations
Mention sqlite, mpi4py
Exercises:
Not SQL, not MPI
Do Py…
(view changes)
PYTHONPATH
HTTP SLURP
Evaluations
Mention sqlite, mpi4py
Exercises:
Not SQL, not MPI
Do Pythonpath
And let's do HTTP first, yeah.
Let's really do regex + evaluations
What else is there?
Introduction
...else:
print name,': change your email, hippie!'
You can also capture the output of a match.
sentence = 'I am a good sentence, and I dependably have punctuation.'
nearComma = re.search('[a-z]+,',sentence)
print nearComma.group(0)
# you can also specify subsets using parentheses
nearComma = re.search('([a-z]+) ([a-z]+)(,)',sentence)
print nearComma.group(0) # the complete match
print nearComma.group(1) # what's in the first set of parens
print nearComma.group(2) # second set
print nearComma.group(3) # third set
And that's the basics of regular expressions! In previous versions of the course, when we were teaching in perl, we spent hours and hours and hours of class time on them, because they were such a fundamental part of the language-- again, imagine not having 'find' or 'startswith.' Frankly, I'm happy to only have to use them once every few months.
SQL
...This easy interaction comes from a module called sqlite3, which contains a small and tidy implementation of SQL. With it, you can create small-scale databases on your own computer. This can come in handy if you find yourself generating large pieces of data that become unwieldy to store in basic 'flat' text files, and a number of python modules and programs use sqlite3 to store all of their data.
If you find yourself generating lots of data, it's worth exploring-- SQL, while another language, is small and primitive enough that you can learn the basics quickly.
Here's an example of how it might be used:
# make a database file and connect
conn = sqlite3.connect('/tmp/example')
# create a tool 'c' to interface with the database
c = conn.cursor()
# Create table
c.execute('''create table sequences
(id text, organism text, sequence text)''')
# Insert a row of data
c.execute("""insert into sequences
values ('FuzzynessGene','Mouse','ATAGGTACGA')""")
# Save (commit) the changes
conn.commit()
# We can also close the cursor if we are done with it
c.close()
I wont assign any exercises about this (if you're desperate, see last year's equivalent lecture at http://intro-prog-bioinfo-2008.wikispaces.com/Session8.2), as, well, it's not something that I use myself. However, this will likely come in handy for some of you, and you should check out Lenny Teytelman's excellent 'BioSQL' tutorial at http://biosql.wikispaces.com/.
Exercises:
Your project should take0. This is the fourth iteration of this course, and I believe that we've made it better each time (pity those in thehighest priority.first). However,if you have completedI bet we can make it better theproject, here arefifth time around, too-- and we'd like your help. Could you fill out this course evaluation?
In order to preserve anonymity, I've set up afew thingsseparate gmail account. The username is 'ipb.evaluations' and the password is 'evaluation'. Please complete the form above, log in to that account, and send an email to intro.prog.bioinformatics@gmail.com with the form attached. Then log out-- I'm not sure how high gmail's tolerance for multiple-users-logged-in is.
After you've done this, your first priority should be completing the project. If you are finished, you canwork on:explore the following exercises.
1. Let's do some regularexpr oneexpression drills.
a) Create a regular expression that matches any line that begins with the character '>'.
b) Modify your regular expression from part (a) so that it also matches the first word in the ID line. Print the first word of the ID line, only using regular expressions-- don't use slices.
c) Modify your regular expression from part (b) so that it expects ID lines to be in the following format.
>ID|organism
ATTACAGGCACGCGACGTAGCGACGGATGA
GGTAGACGCAGTTGACGCAGGACCCGACAT
ATGACGCAGTTTTCCCC
>ID2|organism
ATAGAC...
That is, ID lines should begin with a '>', then have a identification string, then have a '|', then have the name of an organism. Create a regular expression that captures the identification string in a variable 'id' and the organism in a variable 'org.'
2. Make a primitive ORF finder using regularexpr twoexpressions. That is, you should use the template:
sequence = sys.argv[1]
regex = <your code here>
x = re.match(regex,sequence)
print x.group(0)
The match should begin at 'ATG' and end with 'TAA.' The number of nucleotides in between should be a multiple of three.
3.re compile?Use python's online documentation to find out how to use the re.sub function. Modify your script from (2) so that it deletes the orf sequence.
4.subprocessMake a 'hello, <name>' function, like we defined in the lecture introducing functions. The name should be on the command line. Put it in a separate module from the script that calls it, and put that module in a different directory. Run the script.
5. Pat yourself on the back for a job well done.
4:53 pm -
Session10.1
edited
... Transitioning to faster code -> cython
Checking your email so that you don't have to ->…
(view changes)...Transitioning to faster code -> cython
Checking your email so that you don't have to -> libgmail
...talk abouttwo modulesa few things thatmany ofyou will probably finduseful, dealinguseful at some point in your career.
The Python Path
This is less of a 'probably find useful' than a 'certainly find useful', at least if you continue to use python (as you all will, of course). Remember when we talked about functions and modules? One of my central points was that modules can help you centralize your code, allowing you to share functions between many different programs. My trusty 'parseFasta(file)' function has been used in at least a hundred of my programs in graduate school. However-- do I keep all of these hundred-plus programs in a single directory? Of course not-- that wouldn't be organized at all. But then how do I let all of those programs access the same (again, trusty) seqTools.py module?
The answer comes in telling python a directory or set of directories to look in when it's tasked withregular expressionsfinding a module. By default it looks in the directory that the script is in (and inside python's internals) for modules-- it turns out it's easy to tell it to look somewhere else, too. This involves setting the environment variable PYTHONPATH.
$ export PYTHONPATH=/home/lusk/PythonModules # make the directory appropriate, of course
You can add this line to your .bashrc anddatabases..bash_profile scripts in your home directory to make sure that python always looks in that directory for scripts. Now I can and do keep all (as in, no exceptions) of my modules in one place.
Regular expressions
Regular expressions are, in the most basic sense, an extension to the string find, count, startswith, etc methods that we've seen before. However, instead of dealing only in perfectly matching strings, they have a lot more flexibility, and with that, a lot more power.
...And that's the basics of regular expressions! In previous versions of the course, when we were teaching in perl, we spent hours and hours and hours of class time on them, because they were such a fundamental part of the language-- again, imagine not having 'find' or 'startswith.' Frankly, I'm happy to only have to use them once every few months.
SQL
...over time? And finally, and perhaps most importantly-- parsing and loading gigabytes of data can take a whole pile of time and effort. How can we access it without going through that trouble?
These questions have been around for a long time, and as you can imagine, the solution (or at least a solution) has been around for almost as long. Large pieces of data that large numbers of people need access to are stored in databases. These databases having been around for a long time, the means to access and interface with them have become largely standardized in a primitive programming language called Structured Query Language, or SQL for short. While I'm not going to go into any detail about it here-- SQL is, after all, a completely different programming language-- something that many people find neat about python is its ability to very, very easily interact with databases.
This easy interaction comes from a module called sqlite3, which contains a small and tidy implementation of SQL. With it, you can create small-scale databases on your own computer. This can come in handy if you find yourself generating large pieces of data that become unwieldy to store in basic 'flat' text files, and a number of python modules and programs use sqlite3 to store all of their data.
If you find yourself generating lots of data, it's worth exploring-- SQL, while another language, is small and primitive enough that you can learn the basics quickly.
Subprocesses
We already spent some time during this course covering some basic steps you can take to make things faster. However, what if your code is already as fast as it can be? worse yet, what if you're lazy? That's when subprocesses comes in handy.
Most computers that have been sold in the last few years come with more than one processor-- this is what all that dual-core, quad-core, etc marketing is all about. Chip manufacturers have found it harder and harder to speed single processors up, and the route to speed has more and more become about increasing the number of processors in the computer.
However, when you run your python programs, they're running on only one processor, while the other one/three/seven of them are likely sitting idle. While there are relatively fancy ways of solving this problem (such as mpi4py), here we'll deal with a simpler way. We will show you how to make programs that boss other programs around. By telling the computer to run several programs at once, you will be using much more of the computing power in front of you than you normally do.
< use of subprocess/examples >
List comprehensions
Exercises:
Your project should take the highest priority. However, if you have completed the project, here are a few things that you can work on:
2:43 pm