In [1]:
print "Hello World!"


Hello World!

Datatype

every variable is object

list

dictionay

json

%timeit


In [2]:
x=1

In [3]:
dir(x)


Out[3]:
['__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 [4]:
x=3+4j

In [5]:
x


Out[5]:
(3+4j)

In [6]:
x.real


Out[6]:
3.0

In [7]:
x.imag


Out[7]:
4.0

In [8]:
x=["Hello","Wasit", 1981]

In [9]:
x.append(21)

In [10]:
print x


['Hello', 'Wasit', 1981, 21]

In [11]:
x.pop()


Out[11]:
21

In [12]:
x


Out[12]:
['Hello', 'Wasit', 1981]

In [13]:
x[0]


Out[13]:
'Hello'

In [14]:
x[1]


Out[14]:
'Wasit'

In [15]:
x[2]


Out[15]:
1981

In [16]:
x[-1]


Out[16]:
1981

In [21]:
x=range(2,10,2)

In [22]:
x


Out[22]:
[2, 4, 6, 8]

list slicing


In [23]:
x=range(20)

In [24]:
x


Out[24]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

In [25]:
x[:]


Out[25]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

In [26]:
x[:5]


Out[26]:
[0, 1, 2, 3, 4]

In [27]:
x[-5:]


Out[27]:
[15, 16, 17, 18, 19]

In [28]:
x[::-1]


Out[28]:
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In [29]:
x[::-2]


Out[29]:
[19, 17, 15, 13, 11, 9, 7, 5, 3, 1]

Tuple


In [34]:
x=(1,)

In [35]:
type(x)


Out[35]:
tuple

In [36]:
x=(5,7,8)

In [37]:
type(x)


Out[37]:
tuple

In [40]:
len(x)


Out[40]:
3

Dictionary


In [41]:
x={}

In [42]:
type(x)


Out[42]:
dict

In [45]:
x={'key':'value', 'key2':'vlue2'}

In [46]:
x['key']


Out[46]:
'value'

In [48]:
x['new key']='new value'

In [49]:
x


Out[49]:
{'key': 'value', 'key2': 'vlue2', 'new key': 'new value'}

for loop


In [47]:
for i in range(10):
    print i


0
1
2
3
4
5
6
7
8
9

In [52]:
d={}
for i in range(1,13):
    d[i]=i*12
print d


{1: 12, 2: 24, 3: 36, 4: 48, 5: 60, 6: 72, 7: 84, 8: 96, 9: 108, 10: 120, 11: 132, 12: 144}

In [55]:
%%timeit -n3
for i in range(100):
    pass


3 loops, best of 3: 2.8 µs per loop

In [3]:
%%timeit -n100
d={}
for i in range(1,13000):
    d[i]=i*12


100 loops, best of 3: 1.42 ms per loop

list comprehension


In [4]:
%%timeit -n100
x=[]
for i in range(10000):
    x.append(i*12)


100 loops, best of 3: 1.15 ms per loop

In [5]:
%%timeit -n100
x=[]
for i in xrange(10000):
    x.append(i*12)


100 loops, best of 3: 942 µs per loop

In [7]:
%%timeit -n100
x=[ i*12 for i in xrange(10000)]


100 loops, best of 3: 465 µs per loop

dictionary comprehension


In [8]:
%%timeit -n100
d={}
for i in range(10000):
    d[i]=i*12


100 loops, best of 3: 1.17 ms per loop

In [22]:
%%timeit -n100
d={i:i*12 for i in xrange(10000)}


100 loops, best of 3: 845 µs per loop

json


In [23]:
import json

In [39]:
my_json='''
    {
        "customer":"Eienstein",
        "telephone":
            [
                "021234567",
                "021234568"
            ]
    }'''

In [40]:
print my_json


    {
        "customer":"Eienstein",
        "telephone":
            [
                "021234567",
                "021234568"
            ]
    }

In [44]:
contact_detail=json.loads(my_json)

In [46]:
contact_detail['telephone']


Out[46]:
[u'021234567', u'021234568']

In [42]:
my_obj={
            'customer':'Eienstein',
            'telephone':[
                    '021234567',
                    '021234568',
                ]
        }

In [43]:
json.dumps(my_obj)


Out[43]:
'{"customer": "Eienstein", "telephone": ["021234567", "021234568"]}'

In [ ]: