print "hello, world"
Commands that start with "%" are IPython "magics", which are not part of the python language and do not get sent directly to the interpreter.
You can get a list of available magics with:
%magic
%pwd
Commands that start with "!" are sent directly to the operating system shell rather than to the python interpreter.
On OS X and Linux, the shell is typically bash, which will interpret !ls as list the files in the current directory.
On Windows, !dir should give the equivalent result in the DOS shell (if you start ipython from cygwin, you should be able to use bash commands).
!ls
Here is a platform independent way to have python create a list of the filenames in the current directory
import glob
glob.glob("*")
The %logstart magic retro-actively starts logging this IPython session (i.e., it captures even commands typed before the log was started).
The -o option turns on logging for outputs in addition to inputs.
%logstart -o notebook1.log
Use matched single or double quotes for simple strings, triple quotes for multi-line strings.
"this is a string"
'i can single quote'
"""this won't do what I want
multiple
lines
"""
print """this won't do what I want
multiple
lines
"""
Use a decimal point to distinguish integer (int) and double-precision floating point (float) values.
5
5.
5+3
5-3
5*2
10/2
Dividing two integers gives an integer result, rounded down.
10/3
Make sure to include at least one float if you want floating point division
10./3.
10/3.
String substitution works via the mod (%) operator
"This is a %s mad lib" % "neat"
Use parenthesis to group multiple arguments for string substitution
Technically, the parenthesis are defining a tuple, which is an immutable version of a list
"Now %s and %s" % ("one","two")
Most python objects can be converted to strings, which is what the %s format string is asking for
"Now %s and %s" % (1,2)
"%s" % (10./3)
The %f format string lets us specify special formatting for floating point values; e.g., the number of decimal places:
"%.5f" % (10./3)
"%2.5f" % (10./3)
The %e format string gives scientific notation:
"%e" % (10./3)
2**3
The assignment operator (=) lets a name "point at" (reference) a piece of data.
We can refer to the names as variables (because their value may vary with reassignment)
or as references (to emphasize their pointing nature).
a = 8
b = "hello"
We can then operate on the references as if they were the underlying data
a
b
a + 3
a + b
Addition on strings is concatenation:
b + "hi"
a
a = a + 3
a
a += 3
a
(a+2)*3
Exercise: using variables to evaluate a formula
ngc = 13
L = 25
nmm = 2
# Wrong: gives an unintended rounding error due to integer division
Tm1 = 81.5 + (41*ngc - 100*nmm - 675)/L
# Here are two ways to ensure floating point division:
Tm2 = 81.5 + (41*ngc - 100*nmm - 675.)/L
Tm3 = 81.5 + (41*ngc - 100*nmm - 675)/float(L)
Tm1, Tm2, Tm3
IPython magic for listing just the names that we've explicitly assigned in this session
%who
How to remove a reference:
del Tm1
%who
a
= $\rightarrow$ assignment
== $\rightarrow$ test for equality
a == 5
a = 5
a == 5
b = True
a > 6
a < 5
a != 3
a >= 3
a <= 3
Boolean values can be combined with and, or, and not.
Remember that this type of logical expression will "short circuit" as soon as only one outcome is possible.
(5 > 3) and (3 > 2)
(5 < 3) or (3 > 2)
We can act on boolean values using if/elif/else statements.
Things to remember for this type of block statement:
if(5 > 6):
print "it is"
else:
print "it isn't"
if(5 > 6):
print "it is"
else:
print "it isn't"
print "it really isn't"
if(5 > 6):
print "hi"
elif(5> 3):
print "hello"
else:
print "the end"
if(5 > 3):
if(4 > 7):
print "first"
else:
print "second"
else:
print "oops"
Lists are:
mylist = [1,2,"apple","orange",2**5]
mylist
mylist += ["first","second"]
mylist
mylist.append(5)
mylist
list2 = []
list2.append(1)
list2
mylist[3]
mylist[3:6]
We can find the length of a sequence or collection with len
When defining your own classes, you can add this property with a __len__ method
len(mylist)
len("string")
len(6)
mylist[len(mylist)-1]
mylist[-1]
mylist[:3]
mylist[3:]
mylist[:]
The "pointing at" nature of python references can give unexpected behavior for lists.
Use x[:] to make a "shallow" copy of a list for independent modification.
otherlist = mylist
print otherlist
print mylist
otherlist[3] = "thing"
otherlist
mylist
otherlist = mylist[:]
otherlist[3] = "new"
print mylist
print otherlist
c = mylist[:3]
c[2] = 5
print c
print mylist
We can create a (possibly ragged) multi-dimensional array with a list of lists:
L = [[5,6],[3,4]]
L
L[0][1]
We can also create multidimensional arrays with the numpy array class.
Restrictions on arrays:
Benifits of arrays (due to underlying C implementation):
For starting out, you'll usually want python lists
A = array(L)
A
A.dtype
What is the largest signed or unsigned integer that can be represented by a given number of bits?
2**64-1
2**32-1
2**(32-1)-1
2**(16-1)-1
mylist
for i in mylist:
print i