import operators


ImportError: No module named operators



In [16]: import operator


In [17]: operator.__pow__(2, 0.5)

Out[17]: 1.4142135623730951


In [18]: %timeit operator.__pow__(2, 0.5)

1000000 loops, best of 3: 245 ns per loop


In [19]: %timeit operator.__pow__(2, 0.5)

1000000 loops, best of 3: 247 ns per loop


In [20]: a = "One" ; b = "Two"; c = "Three"


In [21]: d = a + b + c


In [22]: d

Out[22]: 'OneTwoThree'


In [23]: 5 * d

Out[23]: 'OneTwoThreeOneTwoThreeOneTwoThreeOneTwoThreeOneTwoThree'


In [24]: d + d + d + d + d

Out[24]: 'OneTwoThreeOneTwoThreeOneTwoThreeOneTwoThreeOneTwoThree'


In [25]: 5.5 * d

Traceback (most recent call last):


File "<ipython-input-25-5e9b8c970ec4>", line 1, in <module>

5.5 * d


TypeError: can't multiply sequence by non-int of type 'float'



In [26]: d

Out[26]: 'OneTwoThree'


In [27]: d[0]

Out[27]: 'O'


In [28]: d[1]

Out[28]: 'n'


In [29]: d[2]

Out[29]: 'e'


In [30]: len(d)

Out[30]: 11


In [31]: d[len(d)]

Traceback (most recent call last):


File "<ipython-input-31-dc8e4cedf7ce>", line 1, in <module>

d[len(d)]


IndexError: string index out of range



In [32]: d[len(d) - 1]

Out[32]: 'e'


In [33]: d

Out[33]: 'OneTwoThree'


In [34]: d[-1]

Out[34]: 'e'


In [35]: d[-2]

Out[35]: 'e'


In [36]: d[-3]

Out[36]: 'r'


In [37]: d[4:]

Out[37]: 'woThree'


In [38]: d

Out[38]: 'OneTwoThree'


In [39]: d[4:-1]

Out[39]: 'woThre'


In [40]: d[4:6]

Out[40]: 'wo'


In [41]: d[::2]

Out[41]: 'OewTre'


In [42]: a = []


In [43]: a = [42]


In [44]: a = [42, 100]


In [45]: print(a)

[42, 100]


In [46]: a

Out[46]: [42, 100]


In [47]: type(a)

Out[47]: list


In [48]: a = [42, 100, 1000]


In [49]: a

Out[49]: [42, 100, 1000]


In [50]: len(a)

Out[50]: 3


In [51]: a[0]

Out[51]: 42


In [52]: a[1]

Out[52]: 100


In [53]: a[-1]

Out[53]: 1000


In [54]: b = [42, 3.14, "Hello"]


In [55]: type(b)

Out[55]: list


In [56]: type(b[0])

Out[56]: int


In [57]: type(b[1])

Out[57]: float


In [58]: type(b[2])

Out[58]: str


In [59]: c = [42, "hello", [1, 2, 3, 4]]


In [60]: type(c)

Out[60]: list


In [61]: type(c[0])

Out[61]: int


In [62]: type(c[1])

Out[62]: str


In [63]: type(c[2])

Out[63]: list


In [64]: c[2]

Out[64]: [1, 2, 3, 4]


In [65]: c[2][3]

Out[65]: 4


In [66]: c[2]

Out[66]: [1, 2, 3, 4]


In [67]: c[2][-1]

Out[67]: 4


In [68]: dir(c)

Out[68]:

['__add__',

'__class__',

'__contains__',

'__delattr__',

'__delitem__',

'__delslice__',

'__doc__',

'__eq__',

'__format__',

'__ge__',

'__getattribute__',

'__getitem__',

'__getslice__',

'__gt__',

'__hash__',

'__iadd__',

'__imul__',

'__init__',

'__iter__',

'__le__',

'__len__',

'__lt__',

'__mul__',

'__ne__',

'__new__',

'__reduce__',

'__reduce_ex__',

'__repr__',

'__reversed__',

'__rmul__',

'__setattr__',

'__setitem__',

'__setslice__',

'__sizeof__',

'__str__',

'__subclasshook__',

'append',

'count',

'extend',

'index',

'insert',

'pop',

'remove',

'reverse',

'sort']


In [69]: a

Out[69]: [42, 100, 1000]


In [70]: a.append(2014)


In [71]: a

Out[71]: [42, 100, 1000, 2014]


In [72]: help(range)

Help on built-in function range in module __builtin__:


range(...)

range(stop) -> list of integers

range(start, stop[, step]) -> list of integers


Return a list containing an arithmetic progression of integers.

range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.

When step is given, it specifies the increment (or decrement).

For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!

These are exactly the valid indices for a list of 4 elements.



In [73]: range(4)

Out[73]: [0, 1, 2, 3]


In [74]: range(2,10)

Out[74]: [2, 3, 4, 5, 6, 7, 8, 9]


In [75]: range(2, 10, 3)

Out[75]: [2, 5, 8]


In [76]: range?

Type: builtin_function_or_method

String form: <built-in function range>

Namespace: Python builtin

Docstring:

range(stop) -> list of integers

range(start, stop[, step]) -> list of integers


Return a list containing an arithmetic progression of integers.

range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.

When step is given, it specifies the increment (or decrement).

For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!

These are exactly the valid indices for a list of 4 elements.


In [77]: range??

Type: builtin_function_or_method

String form: <built-in function range>

Namespace: Python builtin

Docstring:

range(stop) -> list of integers

range(start, stop[, step]) -> list of integers


Return a list containing an arithmetic progression of integers.

range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.

When step is given, it specifies the increment (or decrement).

For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!

These are exactly the valid indices for a list of 4 elements.


In [78]: a = [10, 20, 'dog', 'cat']


In [79]: max(a)

Out[79]: 'dog'


In [80]: min(a)

Out[80]: 10


In [81]: a = ['dog', 'cat']


In [82]: min(a)

Out[82]: 'cat'


In [83]: max(a)

Out[83]: 'dog'


In [84]: a = ['dog', 'cat']


In [85]: a

Out[85]: ['dog', 'cat']


In [86]: a.append('snake')


In [87]: a

Out[87]: ['dog', 'cat', 'snake']


In [88]: a[1] = 'tiger'


In [89]: a

Out[89]: ['dog', 'tiger', 'snake']


In [90]: t = ('dog', 'cat', 'snake')


In [91]: t.append('lion')

Traceback (most recent call last):


File "<ipython-input-91-536c8be3446c>", line 1, in <module>

t.append('lion')


AttributeError: 'tuple' object has no attribute 'append'



In [92]: t

Out[92]: ('dog', 'cat', 'snake')


In [93]: t[1] = 'tiger'

Traceback (most recent call last):


File "<ipython-input-93-e76fad1bfbc5>", line 1, in <module>

t[1] = 'tiger'


TypeError: 'tuple' object does not support item assignment



In [94]: f

Traceback (most recent call last):


File "<ipython-input-94-9a8ad92c50ca>", line 1, in <module>

f


NameError: name 'f' is not defined



In [95]: runfile('/Users/progprim/Desktop/session3.py', wdir='/Users/progprim/Desktop')


In [96]: f

Out[96]: <function __main__.f>


In [97]: f(2)

Out[97]: (4, 8)


In [98]: a, b = f(2)


In [99]: c = f(2)


In [100]: c

Out[100]: (4, 8)


In [101]: runfile('/Users/progprim/Desktop/session3.py', wdir='/Users/progprim/Desktop')


In [102]: f(2)

Out[102]: (4, 8)


In [103]: (a, b) = f(2)


In [104]: a

Out[104]: 4


In [105]: b

Out[105]: 8


In [106]: a

Out[106]: 4


In [107]: c

Out[107]: (4, 8)


In [108]: 4 in c

Out[108]: True


In [109]: 42 in c

Out[109]: False


In [110]: animals = ['dog', 'cat', 'mouse']


In [111]: for x in animals:

     ...: print(x)

     ...:

dog

cat

mouse


In [112]: for x in animals:

     ...: print("Beginning of body of for-loop")

     ...: print(x)

     ...: print("End of body of for-loop")

     ...:

Beginning of body of for-loop

dog

End of body of for-loop

Beginning of body of for-loop

cat

End of body of for-loop

Beginning of body of for-loop

mouse

End of body of for-loop


In [113]: for animal in animals:

     ...: print(animal)

     ...: print("End of body of for-loop")

     ...:

dog

End of body of for-loop

cat

End of body of for-loop

mouse

End of body of for-loop


In [114]: for i in range(10):

     ...: print(i)

     ...:

0

1

2

3

4

5

6

7

8

9


In [115]: for i in range(6):

     ...: print("the square of {} is {}".format(i, i ** 2))

     ...:

the square of 0 is 0

the square of 1 is 1

the square of 2 is 4

the square of 3 is 9

the square of 4 is 16

the square of 5 is 25


In [116]: for char in "Hello World":

     ...: print(char)

     ...:

H

e

l

l

o


W

o

r

l

d


In [117]: range(3)

Out[117]: [0, 1, 2]


In [118]: range(3) * 2

Out[118]: [0, 1, 2, 0, 1, 2]


In [119]: range(3) + range(3)

Out[119]: [0, 1, 2, 0, 1, 2]


In [120]: runfile('/Users/progprim/Desktop/session3.py', wdir='/Users/progprim/Desktop')


In [121]: range_double(n)

Traceback (most recent call last):


File "<ipython-input-121-15e53fa4afc2>", line 1, in <module>

range_double(n)


NameError: name 'n' is not defined



In [122]: range_double(4)

Out[122]: [0, 2, 4, 6]


In [123]: range(4)

Out[123]: [0, 1, 2, 3]


In [124]: runfile('/Users/progprim/Desktop/session3.py', wdir='/Users/progprim/Desktop')


In [125]: range_double(4)

i=0, and res=[]

i=1, and res=[0]

i=2, and res=[0, 2]

i=3, and res=[0, 2, 4]

Out[125]: [0, 2, 4, 6]


In [126]: range(0, 2*4, 2)

Out[126]: [0, 2, 4, 6]


In [127]: runfile('/Users/progprim/Desktop/session3.py', wdir='/Users/progprim/Desktop')


In [128]: range_double2(4)

Out[128]: [0, 2, 4, 6]


In [129]: runfile('/Users/progprim/Desktop/session3.py', wdir='/Users/progprim/Desktop')


In [130]: %timeit range_double(1000)

10000 loops, best of 3: 177 µs per loop


In [131]: %timeit range_double2(1000)

100000 loops, best of 3: 12.9 µs per loop


In [132]: