Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)]

Type "copyright", "credits" or "license" for more information.


IPython 3.2.0 -- An enhanced Interactive Python.

Anaconda is brought to you by Continuum Analytics.

Please check out: http://continuum.io/thanks and https://anaconda.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]: a=10

   ...: type(a)

   ...:

Out[1]: int


In [2]: a=100000

   ...: a.bit_length()

   ...:

Out[2]: 17


In [3]: googol=10**100

   ...: googol

   ...:

Out[3]: 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L


In [4]: googol.bit_length()

Out[4]: 333


In [5]: 1+4

Out[5]: 5


In [6]: 1/4

Out[6]: 0


In [7]: type(1/4)

Out[7]: int


In [8]: 1./4

Out[8]: 0.25


In [9]: type(1./4)

Out[9]: float


In [10]: b=0.35

    ...: type(b)

    ...:

Out[10]: float


In [11]: b+0.1

Out[11]: 0.44999999999999996


In [12]: c=0.5


In [13]: c.as_integer_ratio()

Out[13]: (1, 2)


In [14]: b.as_integer_ratio()

Out[14]: (3152519739159347L, 9007199254740992L)


In [15]: import decimal


In [16]: from decimal import Decimal


In [17]: decimal.getcontext()

Out[17]: Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])


In [18]: decimal.getcontext().prec=4


In [19]: decimal.getcontext()

Out[19]: Context(prec=4, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])


In [20]: e=Decimal(1)/Decimal(11)


In [21]: e

Out[21]: Decimal('0.09091')


In [22]: decimal.getcontext().prec=50


In [23]: e=Decimal(1)/Decimal(11)


In [24]: e

Out[24]: Decimal('0.090909090909090909090909090909090909090909090909091')


In [25]: g=e+e


In [26]: g

Out[26]: Decimal('0.18181818181818181818181818181818181818181818181818')


In [27]: t='this is a sring object'


In [28]: t.capitalize()

Out[28]: 'This is a sring object'


In [29]: t.split()

Out[29]: ['this', 'is', 'a', 'sring', 'object']


In [30]: t='this is a string object'


In [31]: t.find('string')

Out[31]: 10


In [32]: t.find('Python')

Out[32]: -1


In [33]: t.replace(' ','|')

Out[33]: 'this|is|a|string|object'


In [34]: 'http://www.python.org'.strip('htp:/')

Out[34]: 'www.python.org'


In [35]: import re


In [36]: series="""

    ...: '01/18/2014 13:00:00',100,'1st';

    ...: '01/18/2014 13:30:00',110,'2nd';

    ...: '01/18/2014 14:00:00',120.'3rd'

    ...: """


In [37]: series

Out[37]: "\n'01/18/2014 13:00:00',100,'1st';\n'01/18/2014 13:30:00',110,'2nd';\n'01/18/2014 14:00:00',120.'3rd'\n"


In [38]: dt=re.compile("'[0-9/:\s]+'")


In [39]: dt

Out[39]: re.compile(r"'[0-9/:\s]+'")


In [40]: result=dt.findall(series)

    ...: result

    ...:

Out[40]: ["'01/18/2014 13:00:00'", "'01/18/2014 13:30:00'", "'01/18/2014 14:00:00'"]


In [41]: from datetime import datetime

    ...: pydt=datetime.strptime(result[0].replace("'",""),'%m/%d/%Y %H:%M:%S')

    ...: pydt

    ...:

Out[41]: datetime.datetime(2014, 1, 18, 13, 0)


In [42]: print type(pydt)

<type 'datetime.datetime'>


In [43]: t=(1,2.5,'data')


In [44]: type(t)

Out[44]: tuple


In [45]: t=1,2.5,'data'


In [46]: type(t)

Out[46]: tuple


In [47]: t[2]

Out[47]: 'data'


In [48]: type(t[2])

Out[48]: str


In [49]: t.count('data')

Out[49]: 1


In [50]: t.index(1)

Out[50]: 0


In [51]: l=[1,2.5,'data']


In [52]: l[2]

Out[52]: 'data'


In [53]: t

Out[53]: (1, 2.5, 'data')


In [54]: l=list(t)


In [55]: l

Out[55]: [1, 2.5, 'data']


In [56]: type(l)

Out[56]: list


In [57]: l.append([4,3])


In [58]: l

Out[58]: [1, 2.5, 'data', [4, 3]]


In [59]: l.extend([1.0,1.5,2.0])


In [60]: l

Out[60]: [1, 2.5, 'data', [4, 3], 1.0, 1.5, 2.0]


In [61]: l.insert(1,'insert')


In [62]: l

Out[62]: [1, 'insert', 2.5, 'data', [4, 3], 1.0, 1.5, 2.0]


In [63]: l.remove('data')


In [64]: l

Out[64]: [1, 'insert', 2.5, [4, 3], 1.0, 1.5, 2.0]


In [65]: p=l.pop(3)


In [66]: print l,p

[1, 'insert', 2.5, 1.0, 1.5, 2.0] [4, 3]


In [67]: l[2:5]

Out[67]: [2.5, 1.0, 1.5]


In [68]: for element in l[2:5]:

    ...: print element**2

    ...:

6.25

1.0

2.25


In [69]: r=range(0,8,1)


In [70]: r

Out[70]: [0, 1, 2, 3, 4, 5, 6, 7]


In [71]: type(r)

Out[71]: list


In [72]: for i in range(2,5):

    ...: print l[i]**2

    ...:

6.25

1.0

2.25


In [73]: for i in range(1,10):

    ...: if i%2==0:

    ...: print "%d is even" % i

    ...: elif i%3=0:

    ...: print "%d is multiple of 3" % i

    ...: else:

    ...: print "%d is odd" %i

    ...:

File "<ipython-input-73-df11156a03a6>", line 4

elif i%3=0:

^

SyntaxError: invalid syntax



In [74]: for i in range(1,10):

    ...: if i%2==0:

    ...: print "%d is even" % i

    ...: elif i%3==0:

    ...: print "%d is multiple of 3" % i

    ...: else:

    ...: print "%d is odd" %i\

    ...:

    ...:

1 is odd

2 is even

3 is multiple of 3

4 is even

5 is odd

6 is even

7 is odd

8 is even

9 is multiple of 3


In [75]: total=0

    ...: while total<100:

    ...: total+=1

    ...: print total

    ...:

100


In [76]: m=[i**2 for i in range(5)]


In [77]: m

Out[77]: [0, 1, 4, 9, 16]


In [78]: def f(x):

    ...: return x**2

    ...:


In [79]: f(2)

Out[79]: 4


In [80]: def even(x):

    ...: return x % 2==0

    ...:


In [81]: even(3)

Out[81]: False


In [82]: map(even,range(10))

Out[82]: [True, False, True, False, True, False, True, False, True, False]


In [83]: map(lambda x:x**2,range(10))

Out[83]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]


In [84]: filter(even,range(15))

Out[84]: [0, 2, 4, 6, 8, 10, 12, 14]


In [85]: reduce(lambda x,y:x+y,range(10))

Out[85]: 45


In [86]: def cumsum(l):

    ...: total=0

    ...: for elem in l:

    ...: total+=elem

    ...: return total

    ...:


In [87]: cumsum(range(10))

Out[87]: 45


In [88]: