2+2

(50-5*6)/4

"""
Explanation: 1. Python Basics
This notebook will cover the basics of the Python language, including syntax, data structures, and control flow.
Using Python as a Calculator
Many of the things I used to use a calculator for, I now use Python for:
End of explanation
"""


7/3

"""
Explanation: (If you're typing this into an IPython notebook, or otherwise using notebook file, you hit shift-Enter to evaluate a cell.)
There are some gotchas compared to using a normal calculator.
End of explanation
"""


7/3.

7/float(3)

"""
Explanation: Python integer division, like C or Fortran integer division, truncates the remainder and returns an integer. At least it does in version 2. In version 3, Python returns a floating point number. You can get a sneak preview of this feature in Python 2 by importing the module from the future features:
from __future__ import division

Alternatively, you can convert one of the integers to a floating point number, in which case the division function returns another floating point number.
End of explanation
"""


import math
math.sqrt(81)

"""
Explanation: In the last few lines, we have sped by a lot of things that we should stop for a moment and explore a little more fully. We've seen, however briefly, two different data types: integers, also known as whole numbers to the non-programming world, and floating point numbers, also known (incorrectly) as decimal numbers to the rest of the world.
We've also seen the first instance of an import statement. Python has a huge number of libraries included with the distribution. To keep things simple, most of these variables and functions are not accessible from a normal Python interactive session. Instead, you have to import the name. For example, there is a math module containing many useful functions. To access, say, the square root function, you can either first
from math import sqrt

and then call sqrt(81)
or you can simply import the math library itself
End of explanation
"""


width = 20
length = 30
area = length*width
area

"""
Explanation: You can define variables using the equals (=) sign:
End of explanation
"""


volume

"""
Explanation: If you try to access a variable that you haven't yet defined, you get an error:
End of explanation
"""


depth = 10
volume = area*depth
volume

"""
Explanation: and you need to define it:
End of explanation
"""


return = 0

"""
Explanation: You can name a variable almost anything you want. It needs to start with an alphabetical character or "_", can contain alphanumeric charcters plus underscores ("_"). Certain words, however, are reserved for the language:
and, as, 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, with, yield

Trying to define a variable using one of these will result in a syntax error:
End of explanation
"""


'Hello, World!'

"""
Explanation: The Python Tutorial has more on using Python as an interactive shell. The IPython tutorial makes a nice complement to this, since IPython has a much more sophisticated iteractive shell.
Strings
Strings are lists of printable characters, and can be defined using either single quotes
End of explanation
"""


"Hello, World!"

"""
Explanation: or double quotes
End of explanation
"""


"He's a Rebel"

'She asked, "How are you today?"'

"""
Explanation: But not both at the same time, unless you want one of the symbols to be part of the string.
End of explanation
"""


greeting = "Hello, World!"

"""
Explanation: Just like the other two data objects we're familiar with (ints and floats), you can assign a string to a variable
End of explanation
"""


print greeting

"""
Explanation: The print statement is often used for printing character strings:
End of explanation
"""


print "The area is ",area

"""
Explanation: But it can also print data types other than strings:
End of explanation
"""


statement = "Hello," + "World!"
print statement

"""
Explanation: In the above snipped, the number 600 (stored in the variable "area") is converted into a string before being printed out.
You can use the + operator to concatenate strings together:
End of explanation
"""


statement = "Hello, " + "World!"
print statement

"""
Explanation: Don't forget the space between the strings, if you want one there.
End of explanation
"""


print "This " + "is " + "a " + "longer " + "statement."

"""
Explanation: You can use + to concatenate multiple strings in a single statement:
End of explanation
"""


days_of_the_week = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

"""
Explanation: If you have a lot of words to concatenate together, there are other, more efficient ways to do this. But this is fine for linking a few strings together.
Lists
Very often in a programming language, one wants to keep a group of similar items together. Python does this using a data type called lists.
End of explanation
"""


days_of_the_week[2]

"""
Explanation: You can access members of the list using the index of that item:
End of explanation
"""


days_of_the_week[-1]

"""
Explanation: Python lists, like C, but unlike Fortran, use 0 as the index of the first element of a list. Thus, in this example, the 0 element is "Sunday", 1 is "Monday", and so on. If you need to access the nth element from the end of the list, you can use a negative index. For example, the -1 element of a list is the last element:
End of explanation
"""


languages = ["Fortran","C","C++"]
languages.append("Python")
print languages

"""
Explanation: You can add additional items to the list using the .append() command:
End of explanation
"""


range(10)

"""
Explanation: The range() command is a convenient way to make sequential lists of numbers:
End of explanation
"""


range(2,8)

"""
Explanation: Note that range(n) starts at 0 and gives the sequential list of integers less than n. If you want to start at a different number, use range(start,stop)
End of explanation
"""


evens = range(0,20,2)
evens

evens[3]

"""
Explanation: The lists created above with range have a step of 1 between elements. You can also give a fixed step size via a third command:
End of explanation
"""


["Today",7,99.3,""]

"""
Explanation: Lists do not have to hold the same data type. For example,
End of explanation
"""


help(len)

len(evens)

"""
Explanation: However, it's good (but not essential) to use lists for similar objects that are somehow logically connected. If you want to group different data types together into a composite data object, it's best to use tuples, which we will learn about below.
You can find out how long a list is using the len() command:
End of explanation
"""


for day in days_of_the_week:
    print day

"""
Explanation: Iteration, Indentation, and Blocks
One of the most useful things you can do with lists is to iterate through them, i.e. to go through each element one at a time. To do this in Python, we use the for statement:
End of explanation
"""


for day in days_of_the_week:
    statement = "Today is " + day
    print statement

"""
Explanation: This code snippet goes through each element of the list called days_of_the_week and assigns it to the variable day. It then executes everything in the indented block (in this case only one line of code, the print statement) using those variable assignments. When the program has gone through every element of the list, it exists the block.
(Almost) every programming language defines blocks of code in some way. In Fortran, one uses END statements (ENDDO, ENDIF, etc.) to define code blocks. In C, C++, and Perl, one uses curly braces {} to define these blocks.
Python uses a colon (":"), followed by indentation level to define code blocks. Everything at a higher level of indentation is taken to be in the same block. In the above example the block was only a single line, but we could have had longer blocks as well:
End of explanation
"""


for i in range(20):
    print "The square of ",i," is ",i*i

"""
Explanation: The range() command is particularly useful with the for statement to execute loops of a specified length:
End of explanation
"""


for letter in "Sunday":
    print letter

"""
Explanation: Slicing
Lists and strings have something in common that you might not suspect: they can both be treated as sequences. You already know that you can iterate through the elements of a list. You can also iterate through the letters in a string:
End of explanation
"""


days_of_the_week[0]

"""
Explanation: This is only occasionally useful. Slightly more useful is the slicing operation, which you can also use on any sequence. We already know that we can use indexing to get the first element of a list:
End of explanation
"""


days_of_the_week[0:2]

"""
Explanation: If we want the list containing the first two elements of a list, we can do this via
End of explanation
"""


days_of_the_week[:2]

"""
Explanation: or simply
End of explanation
"""


days_of_the_week[-2:]

"""
Explanation: If we want the last items of the list, we can do this with negative slicing:
End of explanation
"""


workdays = days_of_the_week[1:6]
print workdays

"""
Explanation: which is somewhat logically consistent with negative indices accessing the last elements of the list.
You can do:
End of explanation
"""


day = "Sunday"
abbreviation = day[:3]
print abbreviation

"""
Explanation: Since strings are sequences, you can also do this to them:
End of explanation
"""


numbers = range(0,40)
evens = numbers[2::2]
evens

"""
Explanation: If we really want to get fancy, we can pass a third element into the slice, which specifies a step length (just like a third argument to the range() function specifies the step):
End of explanation
"""


if day == "Sunday":
    print "Sleep in"
else:
    print "Go to work"

"""
Explanation: Note that in this example I was even able to omit the second argument, so that the slice started at 2, went to the end of the list, and took every second element, to generate the list of even numbers less that 40.
Booleans and Truth Testing
We have now learned a few data types. We have integers and floating point numbers, strings, and lists to contain them. We have also learned about lists, a container that can hold any data type. We have learned to print things out, and to iterate over items in lists. We will now learn about boolean variables that can be either True or False.
We invariably need some concept of conditions in programming to control branching behavior, to allow a program to react differently to different situations. If it's Monday, I'll go to work, but if it's Sunday, I'll sleep in. To do this in Python, we use a combination of boolean variables, which evaluate to either True or False, and if statements, that control branching based on boolean values.
For example:
End of explanation
"""


day == "Sunday"

"""
Explanation: (Quick quiz: why did the snippet print "Go to work" here? What is the variable "day" set to?)
Let's take the snippet apart to see what happened. First, note the statement
End of explanation
"""


1 == 2

50 == 2*25

3 < 3.14159

1 == 1.0

1 != 0

1 <= 2

1 >= 1

"""
Explanation: If we evaluate it by itself, as we just did, we see that it returns a boolean value, False. The "==" operator performs equality testing. If the two items are equal, it returns True, otherwise it returns False. In this case, it is comparing two variables, the string "Sunday", and whatever is stored in the variable "day", which, in this case, is the other string "Saturday". Since the two strings are not equal to each other, the truth test has the false value.
The if statement that contains the truth test is followed by a code block (a colon followed by an indented block of code). If the boolean is true, it executes the code in that block. Since it is false in the above example, we don't see that code executed.
The first block of code is followed by an else statement, which is executed if nothing else in the above if statement is true. Since the value was false, this code is executed, which is why we see "Go to work".
You can compare any data types in Python:
End of explanation
"""


1 is 1.0

"""
Explanation: We see a few other boolean operators here, all of which which should be self-explanatory. Less than, equality, non-equality, and so on.
Particularly interesting is the 1 == 1.0 test, which is true, since even though the two objects are different data types (integer and floating point number), they have the same value. There is another boolean operator is, that tests whether two objects are the same object:
End of explanation
"""


[1,2,3] == [1,2,4]

[1,2,3] < [1,2,4]

"""
Explanation: We can do boolean tests on lists as well:
End of explanation
"""


hours = 5
0 < hours < 24

"""
Explanation: Finally, note that you can also string multiple comparisons together, which can result in very intuitive tests:
End of explanation
"""


if day == "Sunday":
    print "Sleep in"
elif day == "Saturday":
    print "Do chores"
else:
    print "Go to work"

"""
Explanation: If statements can have elif parts ("else if"), in addition to if/else parts. For example:
End of explanation
"""


for day in days_of_the_week:
    statement = "Today is " + day
    print statement
    if day == "Sunday":
        print "   Sleep in"
    elif day == "Saturday":
        print "   Do chores"
    else:
        print "   Go to work"

"""
Explanation: Of course we can combine if statements with for loops, to make a snippet that is almost interesting:
End of explanation
"""


bool(1)

bool(0)

bool(["This "," is "," a "," list"])

"""
Explanation: This is something of an advanced topic, but ordinary data types have boolean values associated with them, and, indeed, in early versions of Python there was not a separate boolean object. Essentially, anything that was a 0 value (the integer or floating point 0, an empty string "", or an empty list []) was False, and everything else was true. You can see the boolean value of any data object using the bool() function.
End of explanation
"""


n = 10
sequence = [0,1]
for i in range(2,n): # This is going to be a problem if we ever set n <= 2!
    sequence.append(sequence[i-1]+sequence[i-2])
print sequence

"""
Explanation: Code Example: The Fibonacci Sequence
The Fibonacci sequence is a sequence in math that starts with 0 and 1, and then each successive entry is the sum of the previous two. Thus, the sequence goes 0,1,1,2,3,5,8,13,21,34,55,89,...
A very common exercise in programming books is to compute the Fibonacci sequence up to some number n. First I'll show the code, then I'll discuss what it is doing.
End of explanation
"""


def fibonacci(sequence_length):
    "Return the Fibonacci sequence of length *sequence_length*"
    sequence = [0,1]
    if sequence_length < 1:
        print "Fibonacci sequence only defined for length 1 or greater"
        return
    if 0 < sequence_length < 3:
        return sequence[:sequence_length]
    for i in range(2,sequence_length): 
        sequence.append(sequence[i-1]+sequence[i-2])
    return sequence

"""
Explanation: Let's go through this line by line. First, we define the variable n, and set it to the integer 20. n is the length of the sequence we're going to form, and should probably have a better variable name. We then create a variable called sequence, and initialize it to the list with the integers 0 and 1 in it, the first two elements of the Fibonacci sequence. We have to create these elements "by hand", since the iterative part of the sequence requires two previous elements.
We then have a for loop over the list of integers from 2 (the next element of the list) to n (the length of the sequence). After the colon, we see a hash tag "#", and then a comment that if we had set n to some number less than 2 we would have a problem. Comments in Python start with #, and are good ways to make notes to yourself or to a user of your code explaining why you did what you did. Better than the comment here would be to test to make sure the value of n is valid, and to complain if it isn't; we'll try this later.
In the body of the loop, we append to the list an integer equal to the sum of the two previous elements of the list.
After exiting the loop (ending the indentation) we then print out the whole list. That's it!
Functions
We might want to use the Fibonacci snippet with different sequence lengths. We could cut an paste the code into another cell, changing the value of n, but it's easier and more useful to make a function out of the code. We do this with the def statement in Python:
End of explanation
"""


fibonacci(2)

fibonacci(12)

"""
Explanation: We can now call fibonacci() for different sequence_lengths:
End of explanation
"""


help(fibonacci)

"""
Explanation: We've introduced a several new features here. First, note that the function itself is defined as a code block (a colon followed by an indented block). This is the standard way that Python delimits things. Next, note that the first line of the function is a single string. This is called a docstring, and is a special kind of comment that is often available to people using the function through the python command line:
End of explanation
"""


from math import factorial
help(factorial)

"""
Explanation: If you define a docstring for all of your functions, it makes it easier for other people to use them, since they can get help on the arguments and return values of the function.
Next, note that rather than putting a comment in about what input values lead to errors, we have some testing of these values, followed by a warning if the value is invalid, and some conditional code to handle special cases.
Recursion and Factorials
Functions can also call themselves, something that is often called recursion. We're going to experiment with recursion by computing the factorial function. The factorial is defined for a positive integer n as
$$ n! = n(n-1)(n-2)\cdots 1 $$
First, note that we don't need to write a function at all, since this is a function built into the standard math library. Let's use the help function to find out about it:
End of explanation
"""


factorial(20)

"""
Explanation: This is clearly what we want.
End of explanation
"""


def fact(n):
    if n <= 0:
        return 1
    return n*fact(n-1)

fact(20)

"""
Explanation: However, if we did want to write a function ourselves, we could do recursively by noting that
$$ n! = n(n-1)!$$
The program then looks something like:
End of explanation
"""


t = (1,2,'hi',9.0)
t

"""
Explanation: Recursion can be very elegant, and can lead to very simple programs.
Two More Data Structures: Tuples and Dictionaries
Before we end the Python overview, I wanted to touch on two more data structures that are very useful (and thus very common) in Python programs.
A tuple is a sequence object like a list or a string. It's constructed by grouping a sequence of objects together with commas, either without brackets, or with parentheses:
End of explanation
"""


t[1]

"""
Explanation: Tuples are like lists, in that you can access the elements using indices:
End of explanation
"""


t.append(7)

t[1]=77

"""
Explanation: However, tuples are immutable, you can't append to them or change the elements of them:
End of explanation
"""


('Bob',0.0,21.0)

"""
Explanation: Tuples are useful anytime you want to group different pieces of data together in an object, but don't want to create a full-fledged class (see below) for them. For example, let's say you want the Cartesian coordinates of some objects in your program. Tuples are a good way to do this:
End of explanation
"""


positions = [
             ('Bob',0.0,21.0),
             ('Cat',2.5,13.1),
             ('Dog',33.0,1.2)
             ]

"""
Explanation: Again, it's not a necessary distinction, but one way to distinguish tuples and lists is that tuples are a collection of different things, here a name, and x and y coordinates, whereas a list is a collection of similar things, like if we wanted a list of those coordinates:
End of explanation
"""


def minmax(objects):
    minx = 1e20 # These are set to really big numbers
    miny = 1e20
    for obj in objects:
        name,x,y = obj
        if x < minx: 
            minx = x
        if y < miny:
            miny = y
    return minx,miny

x,y = minmax(positions)
print x,y

"""
Explanation: Tuples can be used when functions return more than one value. Say we wanted to compute the smallest x- and y-coordinates of the above list of objects. We could write:
End of explanation
"""


x,y = 1,2
y,x = x,y
x,y

"""
Explanation: Here we did two things with tuples you haven't seen before. First, we unpacked an object into a set of named variables using tuple assignment:
&gt;&gt;&gt; name,x,y = obj

We also returned multiple values (minx,miny), which were then assigned to two other variables (x,y), again by tuple assignment. This makes what would have been complicated code in C++ rather simple.
Tuple assignment is also a convenient way to swap variables:
End of explanation
"""


mylist = [1,2,9,21]

"""
Explanation: Dictionaries are an object called "mappings" or "associative arrays" in other languages. Whereas a list associates an integer index with a set of objects:
End of explanation
"""


ages = {"Rick": 46, "Bob": 86, "Fred": 21}
print "Rick's age is ",ages["Rick"]

"""
Explanation: The index in a dictionary is called the key, and the corresponding dictionary entry is the value. A dictionary can use (almost) anything as the key. Whereas lists are formed with square brackets [], dictionaries use curly brackets {}:
End of explanation
"""


dict(Rick=46,Bob=86,Fred=20)

"""
Explanation: There's also a convenient way to create dictionaries without having to quote the keys.
End of explanation
"""


len(t)

len(ages)

"""
Explanation: The len() command works on both tuples and dictionaries:
End of explanation
"""


fibs = fibonacci(10)

"""
Explanation: Plotting with Matplotlib
We can generally understand trends in data by using a plotting program to chart it. Python has a wonderful plotting library called Matplotlib. The IPython notebook interface we are using for these notes has that functionality built in.
As an example, we have looked at two different functions, the Fibonacci function, and the factorial function, both of which grow faster than polynomially. Which one grows the fastest? Let's plot them. First, let's generate the Fibonacci sequence of length 20:
End of explanation
"""


facts = []
for i in range(10):
    facts.append(factorial(i))

"""
Explanation: Next lets generate the factorials.
End of explanation
"""


figsize(8,6)
plot(facts,label="factorial")
plot(fibs,label="Fibonacci")
xlabel("n")
legend()

"""
Explanation: Now we use the Matplotlib function plot to compare the two.
End of explanation
"""


semilogy(facts,label="factorial")
semilogy(fibs,label="Fibonacci")
xlabel("n")
legend()

"""
Explanation: The factorial function grows much faster. In fact, you can't even see the Fibonacci sequence. It's not entirely surprising: a function where we multiply by n each iteration is bound to grow faster than one where we add (roughly) n each iteration.
Let's plot these on a semilog plot so we can see them both a little more clearly:
End of explanation
"""


import this

"""
Explanation: There are many more things you can do with Matplotlib. We'll be looking at some of them in the sections to come. In the meantime, if you want an idea of the different things you can do, look at the Matplotlib Gallery. Rob Johansson's IPython notebook Introduction to Matplotlib is also particularly good.
Conclusion of the Python Overview
There is, of course, much more to the language than I've covered here. I've tried to keep this brief enough so that you can jump in and start using Python to simplify your life and work. My own experience in learning new things is that the information doesn't "stick" unless you try and use it for something in real life.
You will no doubt need to learn more as you go. I've listed several other good references, including the Python Tutorial and Learn Python the Hard Way. Additionally, now is a good time to start familiarizing yourself with the Python Documentation, and, in particular, the Python Language Reference.
Tim Peters, one of the earliest and most prolific Python contributors, wrote the "Zen of Python", which can be accessed via the "import this" command:
End of explanation
"""


