In [45]:
print 'Hello Alex'
In [46]:
1024*98
Out[46]:
In [47]:
x='Hello '
In [48]:
print x
In [49]:
x*4
Out[49]:
In [50]:
listone=[]
In [51]:
newlist
In [52]:
listone.appendx
In [53]:
listone.append(x)
In [54]:
print x
In [55]:
listone.insert[Alex Grace Paul]
In [56]:
listone.append('Alex','Grace','Paul')
In [57]:
listone=['Alex', 'Grace', 'Paul']
In [58]:
print listone
In [59]:
print x listone
In [60]:
print x+listone
In [61]:
print x
In [62]:
print x + listone
In [63]:
beg=['Hello']
In [64]:
print beg
In [65]:
print beg+listone
In [66]:
for i in listone:
print i
In [67]:
print beg+i
In [68]:
print beg + i
In [69]:
print Hello i
In [70]:
print x+i
In [71]:
print x + i
In [72]:
for i in listone:
print x+i
In [73]:
for i in listone:
print x+i
In [74]:
def message(y):
return x+i+y
In [75]:
message('As we are all learning we should all be having fun.')
Out[75]:
In [76]:
def message1(y):
for i in listone:
return x+i+y
In [77]:
message1('Are we all having fun?')
Out[77]:
In [78]:
print message1
In [79]:
return message1
In [80]:
h=' Are we all having fun?'
In [81]:
print h
In [82]:
def letter(y):
return x+i+h
In [83]:
for i in listone:
print x+i+h
In [84]:
name=grace
In [85]:
name='grace'
In [86]:
print name
In [87]:
print name.title()
In [88]:
listtwo=['paul','grace','alex','stephanie']
In [89]:
print listtwo
In [90]:
for i in listtwo:
print i
In [91]:
for i in listtwo:
print i.title()
In [92]:
quote="Ken Thompson once said, 'One of my most productive days was throwing away 1000 lines of code'"
In [93]:
print quote
note from william: Check this out for dealing with excel files in python - http://www.python-excel.org/ If you can bring along an example excel file that you would like to take into Python
I mentioned dictionaries. Here's an example:
In [172]:
newdict = {'william': 25, 'joe': 50}
print newdict
In [173]:
newdict.keys()
In [174]:
newdict.items()[0]
Hope that helps and have fun. I'll look into the python excel modules and add some example code. The following is an example of reading data from a xls doc. It's all lists - im sure you can do some super cool stuff with this data. Python makes it easy to sort and develop this further.
In [175]:
import xlrd
In [176]:
workbook = xlrd.open_workbook('address.xls')
In [177]:
print workbook.sheet_names()
In [178]:
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
curr_row = -1
while curr_row < num_rows:
curr_row += 1
row = worksheet.row(curr_row)
print row
In [179]:
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
num_cells = worksheet.ncols - 1
curr_row = -1
while curr_row < num_rows:
curr_row += 1
row = worksheet.row(curr_row)
print 'Row:', curr_row
curr_cell = -1
while curr_cell < num_cells:
curr_cell += 1
# Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank
cell_type = worksheet.cell_type(curr_row, curr_cell)
cell_value = worksheet.cell_value(curr_row, curr_cell)
print ' ', cell_type, ':', cell_value
In [180]:
import xlwt
workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('My Worksheet')
worksheet.write(0, 0, label = 'omg im writing python code to excel. perfect.')
worksheet.write(1, 0, label = 'who needs excel when you have python?')
worksheet.write(2, 0, label = listone)
In [181]:
for n in range(2, 10):
for x in range(1, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
In [182]:
workbook.save('Excel_Workbook.xls')
In [183]:
numb = 6
In [184]:
for wr in range(5):
worksheet.write(numb, 0, label = 'testing one two thee')
numb = numb + 1
In [185]:
writebook = xlrd.open_workbook('Excel_Workbook.xls')
In [186]:
print writebook.sheet_names()
In [187]:
shelist = []
In [188]:
firstsheet = writebook.sheet_names()[0]
In [189]:
writesheet = writebook.sheet_by_name(firstsheet)
num_rows = writesheet.nrows - 1
curr_row = -1
while curr_row < num_rows:
curr_row += 1
row = writesheet.row(curr_row)
print row
shelist.append(row)
In [190]:
print shelist
In [191]:
print row[0]
In [192]:
import random
In [193]:
ran = random.randint(5,25)
dran = ran * 2
In [194]:
shetdict = {row[0]: ran, 'blah': dran}
print shetdict
In [ ]: