In [1]:
cd "/home/bakuda/pandas-book/ch02/"


/home/bakuda/pandas-book/ch02

In [2]:
ls


movielens/  names/  usagov_bitly_data2012-03-16-1331923249.txt

In [3]:
a = "this is a string"

In [7]:
#String and tuple are immutable
b =a.replace("string", "long string")
print a
print b


this is a string
this is a long string

In [8]:
x=5.6
str(x)


Out[8]:
'5.6'

In [10]:
s ='python'
list(s)


Out[10]:
['p', 'y', 't', 'h', 'o', 'n']

In [11]:
s


Out[11]:
'python'

In [12]:
s+str(x)


Out[12]:
'python5.6'

In [15]:
template = "%2.2f %s are worth $%d"

In [16]:
template % (60.5, "INR", 1)


Out[16]:
'60.50 INR are worth $1'

In [17]:
True and False


Out[17]:
False

In [18]:
a = range(1,5)

In [19]:
a


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

In [20]:
type(a)


Out[20]:
list

In [21]:
for i in a:
    if i:
        print "%d is True" %(i)


1 is True
2 is True
3 is True
4 is True

In [22]:
b =[]
if b:
    print "b is not empty"
else:
    print "b is empty"


b is empty

In [25]:
[bool(i) for i in a]


Out[25]:
[True, True, True, True]

In [26]:
s = '3.1412'
type(s)


Out[26]:
str

In [29]:
fs = float(s)
float(s)


Out[29]:
3.1412

In [30]:
int(fs)


Out[30]:
3

In [31]:
bool(fs)


Out[31]:
True

In [32]:
from datetime import datetime, date, time

In [33]:
dt = datetime(2011, 10, 29, 20, 30, 21)

In [37]:
dt.day


Out[37]:
29

In [38]:
dt.hour


Out[38]:
20

In [39]:
dt.second


Out[39]:
21

In [40]:
range(0,20,3)


Out[40]:
[0, 3, 6, 9, 12, 15, 18]

In [41]:
xrange(0,20,3)


Out[41]:
xrange(0, 21, 3)

In [42]:
for i in range(0,10,1):
    print i*i


0
1
4
9
16
25
36
49
64
81

In [44]:
for i  in xrange(10):
    print i*i


0
1
4
9
16
25
36
49
64
81

In [47]:
count=0
for x in xrange(100):
    if x%3==0 and x%5==0:
        count +=1
        print x


0
15
30
45
60
75
90

In [48]:
count


Out[48]:
7

In [51]:
t=tuple(xrange(10))

In [52]:
type(t)


Out[52]:
tuple

In [53]:
t


Out[53]:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

In [54]:
t[0]


Out[54]:
0

In [55]:
t.append(xrange(20,25,1))


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-55-075fd3536ac6> in <module>()
----> 1 t.append(xrange(20,25,1))

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

In [56]:
a=xrange(10)
b=xrange(20,30,1)
c=xrange(40,50,1)

In [59]:
seq = [a,b,c]
#for i,j,k in [a,b,c]:
#    print i*j*k

In [60]:
seq


Out[60]:
[xrange(10), xrange(20, 30), xrange(40, 50)]

In [64]:
print list(a)
print list(b)
print list(c)


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]

In [70]:
l= list(a)
l.append(11)

In [71]:
l


Out[71]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]

In [72]:
11 in l


Out[72]:
True

In [73]:
list(a) + list(b)


Out[73]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

In [75]:
d =list(a).extend(list(c))

In [78]:
type(d)


Out[78]:
NoneType

In [79]:
t = list(a)

In [80]:
t


Out[80]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [81]:
[p*p-2 for p in t]


Out[81]:
[-2, -1, 2, 7, 14, 23, 34, 47, 62, 79]

In [87]:
t.sort()
t


Out[87]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [84]:
w=[4,1,2,9,20,10]

In [85]:
w.sort()

In [86]:
w


Out[86]:
[1, 2, 4, 9, 10, 20]

In [91]:
b=['he','a', 'hello','howdy','thereis','she','neighbour']

In [92]:
b.sort(key=len)

In [93]:
b


Out[93]:
['a', 'he', 'she', 'hello', 'howdy', 'thereis', 'neighbour']

In [94]:
w


Out[94]:
[1, 2, 4, 9, 10, 20]

In [95]:
w[1:5]


Out[95]:
[2, 4, 9, 10]

In [99]:
w[-2:]


Out[99]:
[10, 20]

In [100]:
w[:-2]


Out[100]:
[1, 2, 4, 9]

In [101]:
w[:4]


Out[101]:
[1, 2, 4, 9]

In [102]:
len(w)


Out[102]:
6

In [103]:
w[6:8]=[11,22,22]

In [104]:
w


Out[104]:
[1, 2, 4, 9, 10, 20, 11, 22, 22]

In [105]:
w[-6:-2]


Out[105]:
[9, 10, 20, 11]

In [106]:
w[::2]


Out[106]:
[1, 4, 10, 11, 22]

In [107]:
w[::]


Out[107]:
[1, 2, 4, 9, 10, 20, 11, 22, 22]

In [109]:
w[::3]


Out[109]:
[1, 9, 11]

In [110]:
w[::-1]


Out[110]:
[22, 22, 11, 20, 10, 9, 4, 2, 1]

In [111]:
w


Out[111]:
[1, 2, 4, 9, 10, 20, 11, 22, 22]

In [113]:
for i,val in enumerate(w):
    print "%d th element is=%d" %(i,val)


0 th element is=1
1 th element is=2
2 th element is=4
3 th element is=9
4 th element is=10
5 th element is=20
6 th element is=11
7 th element is=22
8 th element is=22

In [120]:
some_list=['aaa','bar','zoo','aba']

In [121]:
dict((v,i*i) for i,v in enumerate(some_list))


Out[121]:
{'aaa': 0, 'aba': 9, 'bar': 1, 'zoo': 4}

In [122]:
new_list=['bb','tar','poo','bab']

In [123]:
zip(some_list, new_list)


Out[123]:
[('aaa', 'bb'), ('bar', 'tar'), ('zoo', 'poo'), ('aba', 'bab')]

In [124]:
empty_dict = {'a':11, 'b':22, 'c':33}

In [125]:
empty_dict


Out[125]:
{'a': 11, 'b': 22, 'c': 33}

In [127]:
empty_dict['a']


Out[127]:
11

In [128]:
empty_dict['z']=2626

In [129]:
empty_dict


Out[129]:
{'a': 11, 'b': 22, 'c': 33, 'z': 2626}

In [131]:
'b' in empty_dict


Out[131]:
True

In [132]:
empty_dict.keys()


Out[132]:
['a', 'c', 'b', 'z']

In [133]:
empty_dict.values()


Out[133]:
[11, 33, 22, 2626]

In [134]:
dict(zip(range(5), range(50,55,1)))


Out[134]:
{0: 50, 1: 51, 2: 52, 3: 53, 4: 54}

In [135]:
strings = ['a', 'as', 'bat', 'car', 'dove', 'python']

In [136]:
[x for x in strings if len(x)>2]


Out[136]:
['bat', 'car', 'dove', 'python']

In [139]:
[x.upper() for x in strings if x.startswith('b') or x.startswith('a')]


Out[139]:
['A', 'AS', 'BAT']

In [140]:
some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

In [141]:
flattened = [x for tup in some_tuples for x in tup]

In [142]:
flattened


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

In [143]:
[x for tup in some_tuples for x in tup]


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

In [145]:
type(some_tuples[0])


Out[145]:
tuple

In [146]:
some_tuples[0][0]


Out[146]:
1

In [147]:
strings = ['foo', 'card', 'bar', 'aaaa', 'abab']

In [149]:
strings.sort(key=len)
strings


Out[149]:
['foo', 'bar', 'card', 'aaaa', 'abab']

In [150]:
strings.sort(key=lambda x: len(set(list(x))))
strings


Out[150]:
['aaaa', 'foo', 'abab', 'bar', 'card']

In [154]:
len(set(list('foo')))


Out[154]:
2

In [155]:
pwd


Out[155]:
u'/home/bakuda/pandas-book/ch02'

In [ ]: