In [2]:
ls
In [3]:
# Simple arithmetic
3./5
Out[3]:
In [4]:
# Print to stdout
print 'We are learning python!'
In [5]:
print 1+1
In [6]:
id(1)
Out[6]:
In [7]:
id(1)
Out[7]:
In [8]:
type(1)
Out[8]:
In [9]:
type('We are learning...')
Out[9]:
In [10]:
x = 1
In [11]:
x.conjugate()
Out[11]:
In [12]:
x.numerator
Out[12]:
In [13]:
x = 1./2.
In [14]:
x.is_integer()
Out[14]:
In [15]:
# Use dir to see associated methods for an object
In [16]:
# Use the help method
x
Out[16]:
In [17]:
# Python variables: names that point to objects
# strings, floats, integers
In [18]:
y = 'x'
In [19]:
print y
In [20]:
y = '1'
In [21]:
float(y)
Out[21]:
In [22]:
int(y)
Out[22]:
In [23]:
y
Out[23]:
In [24]:
x.is_integer()
Out[24]:
In [25]:
# Assert statement
assert( x <1)
assert(not x.is_integer())
In [26]:
x > 1
Out[26]:
In [27]:
x == .5
Out[27]:
In [28]:
z = None
In [29]:
# Control flow
x = 100
while x > 1 :
print x
x -= 10
In [30]:
if x > 1 :
print x
In [31]:
# Break statement
x = 2
while x > 1 :
print x
x += 1
if x > 10 :
break
In [32]:
# For loop
for x in range(1, 10, 2) :
print x
In [33]:
# if/ elif / else statements
x = 1
if x > 1 :
print 'x is bigger!'
elif x < 1 :
print 'x is smaller!'
else :
print x
In [34]:
# Data types
list1 = [x,'Not an integer',y]
In [35]:
for i in list1 :
print i
In [36]:
list2 = [range(1,3),0.5]
In [37]:
majorlist = list1+list2
In [38]:
# Can iterate over items in a list
for list in majorlist :
print list
In [39]:
type(majorlist)
Out[39]:
In [40]:
# List slicing
majorlist[1:3]
tailofmajorlist = majorlist[3:]
In [41]:
tailofmajorlist
Out[41]:
In [42]:
tailofmajorlist[0][1]
Out[42]:
In [43]:
# Dictionaries
dict1 = {'key1':tailofmajorlist,'key2':majorlist}
In [44]:
print dict1['key1'], dict1['key2']
In [45]:
dict1.keys()
Out[45]:
In [46]:
for key in dict1.keys() :
print key
print dict1[key]
In [47]:
for key, value in dict1.iteritems() :
print key
print value
In [48]:
# Tuples
tup1 = (1,0)
In [49]:
type(tup1)
Out[49]:
In [50]:
tup2 = (2,3)
In [51]:
tup1 + tup2
Out[51]:
In [52]:
tup1*2
Out[52]:
In [53]:
list1*2
Out[53]:
In [54]:
str2 = 'I am a string'*2
In [55]:
# Strings are iterable too!
for s in str2 :
print s
In [56]:
str2[:5]
Out[56]:
In [57]:
# Access tuple elements
tup1[1]
Out[57]:
In [58]:
list1.append(5)
In [59]:
list1
Out[59]:
In [60]:
len(list1)
Out[60]:
In [61]:
tup1.index(5)
In [62]:
tup1
Out[62]:
In [67]:
dir(tuple)
Out[67]:
In [68]:
help(tuple.count)
In [70]:
tup1.count(1)
Out[70]:
In [71]:
x=[1,2,3]
In [72]:
x[0] = 3
In [73]:
y = x
In [74]:
y[1] = 7
In [75]:
y
Out[75]:
In [79]:
x
Out[79]:
In [89]:
y = list(x)
In [94]:
x[0] = 56
In [95]:
x
Out[95]:
In [96]:
y
Out[96]:
In [97]:
x = (1,2,3)
In [98]:
y = x
In [99]:
x[0] = 56
In [100]:
x = (56,) + x[1:]
In [101]:
x
Out[101]:
In [102]:
x = [1,2,3]
In [103]:
x
Out[103]:
In [104]:
x.append(6)
In [105]:
x
Out[105]:
In [106]:
for i in x:
print i
In [107]:
len(x)
Out[107]:
In [108]:
x = 'abcde'
In [110]:
for y in x:
print y
In [111]:
'a' in x
Out[111]:
In [112]:
x[2]
Out[112]:
In [113]:
x[2] = 'k'
In [114]:
x = 'abcd'
In [118]:
x[:2] + 'k' + x[-1]
Out[118]:
In [121]:
def adder(val1, val2):
return val1 + val2
In [124]:
adder(4,5)
Out[124]:
In [125]:
type(x)
Out[125]:
In [126]:
adder('ab','cd')
Out[126]:
In [127]:
adder(1.2,3)
Out[127]:
In [177]:
def summer(l):
"""
This function sums a sequence.
"""
assert len(l) > 0
print sum(l)
In [178]:
x = [1,2,3,5]
In [179]:
summer([])
In [153]:
x
Out[153]:
In [154]:
help(summer)
In [165]:
def hello(s1, s2 = 'hello world'):
print s1, s2
In [170]:
hello(s1 = 'yo', s2 = 5)
In [180]:
pwd
Out[180]:
In [181]:
ls
In [182]:
cd ..
In [183]:
ls
In [184]:
cd scripts/
In [185]:
ls
In [193]:
f = open('data1.txt')
In [194]:
file_lines = []
In [195]:
for line in f:
file_lines.append(line)
In [196]:
file_lines
Out[196]:
In [197]:
print file_lines[0]
In [207]:
for line in file_lines:
print line.rstrip('\n')
In [199]:
help(str.rstrip)
In [204]:
file_lines[0].rstrip('\n')
Out[204]:
In [206]:
help(str.format)
In [209]:
f.close()
In [210]:
f
Out[210]:
In [211]:
fout = open('data2.txt', 'w')
In [212]:
print >> fout, 'hello world'
In [213]:
fout.close()
In [214]:
cat data2.txt
In [215]:
help(open)
In [216]:
print file.__doc__
In [217]:
cat data1.txt
In [218]:
s = '1 4'
In [219]:
s.split()
Out[219]:
In [222]:
f = open('data1.txt')
In [223]:
fout = open('data2.txt', 'w')
In [224]:
# Exercise: In data2.txt, first column is the same as data 1.txt
# Second column of data 2.txt is the sum of the first
# and second column of data1.txt
for line in f:
# use line.split() to split on whitespace
# get the sum of the first two columns
print >> fout, # col1 col1+col2 from data1.txt
# data2.txt should look like
# 1 5
# 2 11
# ...
In [225]:
s
Out[225]:
In [228]:
help(str.split)
In [229]:
ls
In [241]:
f2 = open('data2.txt','w')
for line in open('data1.txt','r') :
x, y = [int(i) for i in line.split()]
print >> f2, x, x+y
f2.close()
In [245]:
# List comprehension
# new_list = [ expression for element in list ]
print [float(i)+1 for i in line.split()]
In [246]:
pwd
Out[246]:
In [ ]: