File: class/Extras/Other/PriorClasses/schwab-sep12/other-INTERACTIVE-LOG.txt

# this is the first morning's iinteractive code, taken from
# another class's interaction log; see INTERACTIVE-LOG.txt
# for the continuation of this log for the rest of the class.


Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
>>> 
>>> S = 'spam'
>>> len(S)
4
>>> S + 'NI'
'spamNI'
>>> S * 8
'spamspamspamspamspamspamspamspam'
>>> 
>>> S[0]
's'
>>> S[1]
'p'
>>> S[-1]
'm'
>>> S[-2]
'a'
>>> S[len(S) - 1]
'm'
>>> S[I:J]  
KeyboardInterrupt
>>> 
>>> S
'spam'
>>> S[1:3]
'pa'
>>> S[1:]
'pam'
>>> S[:4]
'spam'
>>> 
>>> S[0:3]
'spa'
>>> S[:-1]
'spa'
>>> S[:]
'spam'
>>> S[I:J:K]  
KeyboardInterrupt
>>> 
>>> X ='abcdefghijklmnop'
>>> S[::2]
'sa'
>>> X[::2]
'acegikmo'
>>> X[::-1]
'ponmlkjihgfedcba'
>>> 
>>> S
'spam'
>>> S.find('pa')
1
>>> S.upper()
'SPAM'
>>> 
>>> S.replace('pa', 'XYZ')
'sXYZm'
>>> 
>>> line = 'aaa,bbb,ccc\n'
>>> 
>>> line[:-1]
'aaa,bbb,ccc'
>>> line.rstrip()
'aaa,bbb,ccc'
>>> 
>>> line
'aaa,bbb,ccc\n'
>>> line.split(',')
['aaa', 'bbb', 'ccc\n']
>>> 
>>> line.rstrip().split(',')
['aaa', 'bbb', 'ccc']
>>> 
>>> line
'aaa,bbb,ccc\n'
>>> cols = line.rstrip().split(',')
>>> cols
['aaa', 'bbb', 'ccc']
>>> '++'.join(cols)
'aaa++bbb++ccc'
>>> 
>>> line
'aaa,bbb,ccc\n'
>>> line.replace(',', '++')
'aaa++bbb++ccc\n'
>>> 
>>> line
'aaa,bbb,ccc\n'
>>> 
>>> line + 'xxxxx'
'aaa,bbb,ccc\nxxxxx'
>>> 
>>> line
'aaa,bbb,ccc\n'
>>> 
>>> line = line + 'xxxxx'
>>> line
'aaa,bbb,ccc\nxxxxx'
>>> 
>>> line[0] = 'x'

Traceback (most recent call last):
  File "<pyshell#60>", line 1, in <module>
    line[0] = 'x'
TypeError: 'str' object does not support item assignment
>>> 
>>> X = 1
>>> X = X + 1
>>> X
2
>>> 
>>> L = [123, 'abc', 1.23]
>>> 
>>> len(L)
3
>>> L + [3, 4, 5]
[123, 'abc', 1.23, 3, 4, 5]
>>> L
[123, 'abc', 1.23]
>>> L * 3
[123, 'abc', 1.23, 123, 'abc', 1.23, 123, 'abc', 1.23]
>>> 

>>> L
[123, 'abc', 1.23]
>>> L[0]
123
>>> {-1]
SyntaxError: invalid syntax
>>> L[-1]
1.23
>>> 
>>> L[1:]
['abc', 1.23]
>>> L
[123, 'abc', 1.23]
>>> 
>>> L
[123, 'abc', 1.23]
>>> L.append(99)
>>> L
[123, 'abc', 1.23, 99]
>>> L.extend([5, 6, 7])
>>> L
[123, 'abc', 1.23, 99, 5, 6, 7]
>>> 
>>> L.remove(1.23)
>>> L
[123, 'abc', 99, 5, 6, 7]
>>> 
>>> L.pop()
7
>>> L
[123, 'abc', 99, 5, 6]
>>> 
>>> M = [[1, 2, 3],
         [4, 5, 6],
         [7, 8, 9]]
>>> 
>>> M
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> 
>>> M[1]
[4, 5, 6]
>>> M[1][2]
6
>>> col2 = [row[1] for row in M]
>>> col2
[2, 5, 8]
>>> 
>>> [x ** 2 for x in [1, 2, 3, 4, 5]]
[1, 4, 9, 16, 25]
>>> 
>>> D = {'food':'spam', 'taste':'yum'}
>>> D['food']
'spam'
>>> D.keys()
['food', 'taste']
>>> D.values()
['spam', 'yum']
>>> D.items()
[('food', 'spam'), ('taste', 'yum')]
>>> 
>>> D = {'f' + 'ood': S}
>>> D
{'food': 'spam'}
>>> S
'spam'
>>> D - {'food': [1, 2, 3, 4, 5]}

Traceback (most recent call last):
  File "<pyshell#115>", line 1, in <module>
    D - {'food': [1, 2, 3, 4, 5]}
TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
>>> 
>>> D = {'food': [1, 2, 3, 4, 5]}
>>> D
{'food': [1, 2, 3, 4, 5]}
>>> 
>>> bday= (8, 6, 20000

       )
>>> 
>>> 
>>> bday= (8, 6, 2000)
>>> bday
(8, 6, 2000)
>>> 
>>> D = {bday: 'Bob'}
>>> D
{(8, 6, 2000): 'Bob'}
>>> 
>>> D = {}
>>> D['name'] = 'Bob'
>>> D['age']  = 40
>>> D['job']  = 'dev'
>>> D
{'job': 'dev', 'age': 40, 'name': 'Bob'}
>>> 
>>> rec = {'name': {'first': 'Bob', 'last': 'Smith'},
           'job':  ['dev', 'mgr'],
           'age':  40.5}
>>> 
>>> rec['name']
{'last': 'Smith', 'first': 'Bob'}
>>> rec['name']['last']
'Smith'
>>> 
>>> rec.job

Traceback (most recent call last):
  File "<pyshell#145>", line 1, in <module>
    rec.job
AttributeError: 'dict' object has no attribute 'job'
>>> rec['job']
['dev', 'mgr']
>>> rec['job'].append('janitor')
>>> 
>>> 
>>> rec
{'age': 40.5, 'job': ['dev', 'mgr', 'janitor'], 'name': {'last': 'Smith', 'first': 'Bob'}}
>>> 
>>> rec = 0
>>> 
>>> T = (1, 2, 3, 4)
>>> T + (6, 7, 8)
(1, 2, 3, 4, 6, 7, 8)
>>> T * 3
(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
>>> T]-1]
SyntaxError: invalid syntax
>>> T[-1]
4
>>> T[1:]
(2, 3, 4)
>>> T
(1, 2, 3, 4)
>>> 
>>> T.count(3)
1
>>> T.append(9)

Traceback (most recent call last):
  File "<pyshell#163>", line 1, in <module>
    T.append(9)
AttributeError: 'tuple' object has no attribute 'append'
>>> T[0] = 1

Traceback (most recent call last):
  File "<pyshell#164>", line 1, in <module>
    T[0] = 1
TypeError: 'tuple' object does not support item assignment
>>> 
>>> F = open('data.txt', 'w')
>>> F.write('spam\n')
>>> F.write('world\n')
>>> F.close()
>>> 
>>> F = open('data.txt')
>>> bytes = F.read()
>>> bytes
'spam\nworld\n'
>>> print bytes
spam
world

>>> F = open(r'C:\mydata\data.txt', 'w')
KeyboardInterrupt
>>> import os
>>> 
>>> 
>>> F
<open file 'data.txt', mode 'r' at 0x00000000028E24B0>
>>> dir(F)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']
>>> 
>>> help(F.readline)
Help on built-in function readline:

readline(...)
    readline([size]) -> next line from the file, as a string.
    
    Retain newline.  A non-negative size argument limits the maximum
    number of bytes to return (an incomplete line may be returned then).
    Return an empty string at EOF.


>>> import math
>>> math.sqrt(1000)
31.622776601683793
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>> help(math.sin)
Help on built-in function sin in module math:

sin(...)
    sin(x)
    
    Return the sine of x (measured in radians).

>>> x = 0b10101010
>>> x
170
>>> x = 0o777
>>> x
511
>>> x = 0xFFF
>>> x
4095
>>> x = 0777
>>> x
511
>>> bin(170)
'0b10101010'
>>> oct(511)
'0777'
>>> hex(4095)
'0xfff'
>>> 
>>> int('123')
123
>>> 
>>> int('170', 2)

Traceback (most recent call last):
  File "<pyshell#204>", line 1, in <module>
    int('170', 2)
ValueError: invalid literal for int() with base 2: '170'
>>> int('1010101010', 2)
682
>>> eval('0b10101010')
170
>>> 
>>> '23' + 1

Traceback (most recent call last):
  File "<pyshell#208>", line 1, in <module>
    '23' + 1
TypeError: cannot concatenate 'str' and 'int' objects
>>> 
>>> int('23')
23
>>> str(1)
'1'
>>> int('23') + 1
24
>>> '23' + str(1)
'231'
>>> 
>>> 
>>> X = 'spam'
>>> X = 99
>>> X = [1, 2, 3]
>>> 
>>> 
>>> L = [1, 2, 3]
>>> M = L
>>> L == M
True
>>> L is M
True
>>> 
>>> L = [1, 2, 3]
>>> M = [1, 2, 3]
>>> L == M
True
>>> L is M
False
>>> 
>>> X = 24
>>> Y = 24
>>> X == Y
True
>>> X is Y
True
>>> 
>>> import sys
>>> sys.getrefcount(1)
725
>>> 
>>> msg = '''
aaaaaaaaa
bbbbb''bbb"""bb
cccccccc'''
>>> 
>>> msg
'\naaaaaaaaa\nbbbbb\'\'bbb"""bb\ncccccccc'
>>> 
>>> 
>>> D = {}
>>> D['a'] = 1
>>> D
{'a': 1}
>>> D['b']

Traceback (most recent call last):
  File "<pyshell#250>", line 1, in <module>
    D['b']
KeyError: 'b'
>>> 
>>> D.has_key('a')
True
>>> D.has_key('b')
False
>>> 
>>> if not D.has_key('b'):
	print 'missing'

	
missing
>>> 
>>> 'b' in D
False
>>> 
>>> D = {'a':1, 'b':2, 'c':3}
>>> D
{'a': 1, 'c': 3, 'b': 2}
>>> 
>>> Ks = D.keys()
>>> Ks
['a', 'c', 'b']
>>> Ks.sort()
>>> Ks
['a', 'b', 'c']
>>> 
>>> for key in Ks:
	print key, '=>', D[key]

	
a => 1
b => 2
c => 3
>>> dir(D)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
>>> 1, 2, 3
(1, 2, 3)
>>> 
>>> 
>>> import struct
>>> 
>>> F = open('data.bin', 'wb')
>>> data = struct.pack('>i4sh', 8, 'spam', 9)
>>> data
'\x00\x00\x00\x08spam\x00\t'
>>> 
>>> F.write(data)
>>> F.close()
>>> 
>>> F = open('data.bin', 'rb')
>>> byte = F.read()
>>> byte
'\x00\x00\x00\x08spam\x00\t'
>>> 
>>> vals = struct.unpack('>i4sh', byte)
>>> vals
(8, 'spam', 9)
>>> x, y, z = struct.unpack('>i4sh', byte)
>>> x
8
>>> y
'spam'
>>> z
9
>>> X = None
>>> L = []
>>> L[99] = 1

Traceback (most recent call last):
  File "<pyshell#297>", line 1, in <module>
    L[99] = 1
IndexError: list assignment index out of range
>>> 
>>> L = [None] * 100
>>> L
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
>>> 
>>> L = [0] * 100
>>> L
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> 
>>> type(L)
<type 'list'>
>>> 
>>> 
>>> S = {1, 2, 3, 4, 5}
>>> S
set([1, 2, 3, 4, 5])
>>> ================================ RESTART ================================
>>> 
1267650600228229401496703205376
spam!spam!spam!spam!spam!spam!spam!spam!
C:\pyclass
spamNI
>>> 
>>>



#
# see INTERACTIVE-LOG.txt for parts that show up here
#



>>> 
>>> 
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>> 
>>> 



[Home page] Books Code Blog Python Author Train Find ©M.Lutz