Python 2.7.7 |Anaconda 2.0.1 (x86_64)| (default, Jun 2 2014, 12:48:16)
Type "copyright", "credits" or "license" for more information.
IPython 2.1.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.
In [1]: 4.5 / 0
Traceback (most recent call last):
File "<ipython-input-1-1349566cb458>", line 1, in <module>
4.5 / 0
ZeroDivisionError: float division by zero
In [2]: 17 / 0
Traceback (most recent call last):
File "<ipython-input-2-3bf37c425175>", line 1, in <module>
17 / 0
ZeroDivisionError: integer division or modulo by zero
In [3]: x
Traceback (most recent call last):
File "<ipython-input-3-401b30e3b8b5>", line 1, in <module>
x
NameError: name 'x' is not defined
In [4]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [5]: read_my_file('this-file-does-not-exist.txt')
Traceback (most recent call last):
File "<ipython-input-5-16f105b97708>", line 1, in <module>
read_my_file('this-file-does-not-exist.txt')
File "/Users/progprim/Desktop/session5.py", line 16, in read_my_file
f = open(filename)
IOError: [Errno 2] No such file or directory: 'this-file-does-not-exist.txt'
In [6]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [7]: read_my_file('this-file-does-not-exist.txt')
Could not open file this-file-does-not-exist.txt
To exit: use 'exit', 'quit', or Ctrl-D.
An exception has occurred, use %tb to see the full traceback.
SystemExit: 1
In [8]: read_my_file('data.txt')
1 2 4 67 -34 340
0 45 3 2
17
In [9]: raise ValueError("Hello")
Traceback (most recent call last):
File "<ipython-input-9-c19336de144b>", line 1, in <module>
raise ValueError("Hello")
ValueError: Hello
In [10]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [11]: compute_sum_of_numbers_in_each_line_for_the_given_file('data.txt')
380
Traceback (most recent call last):
File "<ipython-input-11-7ce02b440646>", line 1, in <module>
compute_sum_of_numbers_in_each_line_for_the_given_file('data.txt')
File "/Users/progprim/Desktop/session5.py", line 12, in compute_sum_of_numbers_in_each_line_for_the_given_file
s = s + int(bit)
ValueError: invalid literal for int() with base 10: 'coffee'
In [12]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [13]: compute_sum_of_numbers_in_each_line_for_the_given_file('data.txt')
380
5
0
In [14]: x=42
In [15]: print("My number x is %d" % x)
My number x is 42
In [16]: print("My number x is %d." % x)
My number x is 42.
In [17]: "My number x is %d." % x
Out[17]: 'My number x is 42.'
In [18]: "My number x is %d, and x^2 is %d." % (x, x**2)
Out[18]: 'My number x is 42, and x^2 is 1764.'
In [19]: "My number x is %5d, and x^2 is %d." % (x, x**2)
Out[19]: 'My number x is 42, and x^2 is 1764.'
In [20]: y = 1.1234e10
In [21]: y
Out[21]: 11234000000.0
In [22]: "My number y is %f" % y
Out[22]: 'My number y is 11234000000.000000'
In [23]: "My number y is %e" % y
Out[23]: 'My number y is 1.123400e+10'
In [24]: "My number y is %g" % y
Out[24]: 'My number y is 1.1234e+10'
In [25]: import math
In [26]: p = math.pi
In [27]: "pi = %f" % pi
Traceback (most recent call last):
File "<ipython-input-27-41b1e3191e74>", line 1, in <module>
"pi = %f" % pi
NameError: name 'pi' is not defined
In [28]: "pi = %f" % p
Out[28]: 'pi = 3.141593'
In [29]: "pi = %10f" % p
Out[29]: 'pi = 3.141593'
In [30]: "pi = %10.2f" % p
Out[30]: 'pi = 3.14'
In [31]: "pi = %12f" % p
Out[31]: 'pi = 3.141593'
In [32]: "pi = %.12f" % p
Out[32]: 'pi = 3.141592653590'
In [33]: "pi = %.12f ." % p
Out[33]: 'pi = 3.141592653590 .'
In [34]: "pi = %-10f ." % p
Out[34]: 'pi = 3.141593 .'
In [35]: "pi = %-10f." % p
Out[35]: 'pi = 3.141593 .'
In [36]: print(4)
4
In [37]: a = 4
In [38]: dir(a)
Out[38]:
['__abs__',
'__add__',
'__and__',
'__class__',
'__cmp__',
'__coerce__',
'__delattr__',
'__div__',
'__divmod__',
'__doc__',
'__float__',
'__floordiv__',
'__format__',
'__getattribute__',
'__getnewargs__',
'__hash__',
'__hex__',
'__index__',
'__init__',
'__int__',
'__invert__',
'__long__',
'__lshift__',
'__mod__',
'__mul__',
'__neg__',
'__new__',
'__nonzero__',
'__oct__',
'__or__',
'__pos__',
'__pow__',
'__radd__',
'__rand__',
'__rdiv__',
'__rdivmod__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rfloordiv__',
'__rlshift__',
'__rmod__',
'__rmul__',
'__ror__',
'__rpow__',
'__rrshift__',
'__rshift__',
'__rsub__',
'__rtruediv__',
'__rxor__',
'__setattr__',
'__sizeof__',
'__str__',
'__sub__',
'__subclasshook__',
'__truediv__',
'__trunc__',
'__xor__',
'bit_length',
'conjugate',
'denominator',
'imag',
'numerator',
'real']
In [39]: a.__str__()
Out[39]: '4'
In [40]: str(a)
Out[40]: '4'
In [41]: p
Out[41]: 3.141592653589793
In [42]: str(p)
Out[42]: '3.14159265359'
In [43]: p.__str__()
Out[43]: '3.14159265359'
In [44]: repr(p)
Out[44]: '3.141592653589793'
In [45]: p.__repr__()
Out[45]: '3.141592653589793'
In [46]: l = [1, 2, 'hello', p]
In [47]: print(l)
[1, 2, 'hello', 3.141592653589793]
In [48]: print(p)
3.14159265359
In [49]: eval("4")
Out[49]: 4
In [50]: type(eval("4"))
Out[50]: int
In [51]: eval("3.14")
Out[51]: 3.14
In [52]: type(eval("3.14"))
Out[52]: float
In [53]: pi
Traceback (most recent call last):
File "<ipython-input-53-68f7b1e53523>", line 1, in <module>
pi
NameError: name 'pi' is not defined
In [54]: p
Out[54]: 3.141592653589793
In [55]: repr(p)
Out[55]: '3.141592653589793'
In [56]: eval(repr(p))
Out[56]: 3.141592653589793
In [57]: eval(repr(p)) - p
Out[57]: 0.0
In [58]: eval(str(p)) - p
Out[58]: 2.0694557179012918e-13
In [59]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [60]: print_x2_table()
0.0
0.5
1.0
1.5
2.0
2.5
In [61]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [62]: print_x2_table()
0.0 0.0
0.5 0.25
1.0 1.0
1.5 2.25
2.0 4.0
2.5 6.25
In [63]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [64]: print_x3_table()
0.0 0.0
0.5 0.125
1.0 1.0
1.5 3.375
2.0 8.0
2.5 15.625
In [65]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [66]: print_xn_table(2)
0.0 0.0
0.5 0.25
1.0 1.0
1.5 2.25
2.0 4.0
2.5 6.25
In [67]: print_xn_table(3)
0.0 0.0
0.5 0.125
1.0 1.0
1.5 3.375
2.0 8.0
2.5 15.625
In [68]: print_xn_table(100)
0.0 0.0
0.5 7.88860905221e-31
1.0 1.0
1.5 4.06561177535e+17
2.0 1.26765060023e+30
2.5 6.22301527786e+39
In [69]: import math
In [70]: math.sin(0)
Out[70]: 0.0
In [71]: math.sin(math.pi)
Out[71]: 1.2246467991473532e-16
In [72]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [73]: print_fx_table(math.sin)
0.0 0.0
0.5 0.479425538604
1.0 0.841470984808
1.5 0.997494986604
2.0 0.909297426826
2.5 0.598472144104
In [74]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [75]: print_fx_table(square)
0.0 0.0
0.5 0.25
1.0 1.0
1.5 2.25
2.0 4.0
2.5 6.25
In [76]: runfile('/Users/progprim/Desktop/session5.py', wdir='/Users/progprim/Desktop')
In [77]: print_fx_table(cubic)
0.0 0.0
0.5 0.125
1.0 1.0
1.5 3.375
2.0 8.0
2.5 15.625
In [78]: print_fx_table(lambda x: x**2)
0.0 0.0
0.5 0.25
1.0 1.0
1.5 2.25
2.0 4.0
2.5 6.25
In [79]: print_fx_table(lambda x: x**3)
0.0 0.0
0.5 0.125
1.0 1.0
1.5 3.375
2.0 8.0
2.5 15.625
In [80]: