In [26]:
def fahrenheit(T):
return (9.0/5)*T + 32
In [27]:
fahrenheit(0)
Out[27]:
In [28]:
temp = [0, 22, 52, 79, 100]
In [29]:
map(fahrenheit, temp)
Out[29]:
In [30]:
map(lambda T: (9.0/5)*T + 32, temp)
Out[30]:
In [31]:
lambda x,y: x+y
Out[31]:
In [32]:
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]
In [33]:
map(lambda x,y:x+y, a, b)
Out[33]:
In [34]:
map(lambda x,y,z: x+y+z, a,b,c)
Out[34]:
In [35]:
map(lambda x:x*-1, a)
Out[35]:
In [36]:
m_list = [12,56,79,45]
In [37]:
reduce(lambda x,y:x+y, m_list)
Out[37]:
In [38]:
max(m_list)
Out[38]:
In [39]:
find_max = lambda x,y: x if x > y else y
In [40]:
reduce(find_max, m_list)
Out[40]:
In [41]:
m_list2 = ['a', 'z', 'c', 'g']
In [42]:
max(m_list2)
Out[42]:
In [43]:
reduce(find_max, m_list2)
Out[43]:
In [44]:
m_list3 = ['a1', '2z', 'aw', 'a']
In [45]:
reduce(find_max, m_list3)
Out[45]:
In [46]:
def even_check(num):
if num % 2 == 0:
return True
else:
return False
In [47]:
even_check(56)
Out[47]:
In [48]:
even_check(5)
Out[48]:
In [49]:
m_list4 = [0,1,2,3,4,5]
In [50]:
filter(even_check, m_list4)
Out[50]:
In [51]:
filter(lambda num: num%2 == 0, m_list4)
Out[51]:
In [52]:
x = [1,2,3]
In [53]:
y = [4,5,6]
In [54]:
zip(x,y)
Out[54]:
In [55]:
zip(x,x)
Out[55]:
In [56]:
zip(x,x,y)
Out[56]:
In [57]:
z = ['a', 'b']
In [58]:
zip(x,y,z)
Out[58]:
In [59]:
zip(z,x,y)
Out[59]:
In [60]:
for (count, item) in enumerate(m_list3):
print count, item
In [61]:
for (count, item) in enumerate(m_list2):
if count >= 2:
break
else:
print count, item
In [62]:
m_list5 = [False, True, False, False, True]
In [63]:
any(m_list5)
Out[63]:
In [64]:
all(m_list5)
Out[64]:
In [65]:
complex(2,3)
Out[65]:
In [66]:
complex(10,0.5)
Out[66]:
In [67]:
complex('5+2j')
Out[67]:
In [68]:
complex('58-52j')
Out[68]:
In [69]:
complex('-5j')
Out[69]:
In [ ]:
In [70]:
from collections import defaultdict
In [71]:
# without using defaultdict
m_dict = {'k1': 1}
In [72]:
m_dict['k1']
Out[72]:
In [73]:
m_dict['k2']
In [74]:
#using defaultdict
m_dict2 = defaultdict(object)
In [75]:
m_dict2['k1213']
Out[75]:
In [76]:
for key in m_dict2:
print key
In [77]:
m_dict3 = defaultdict(lambda : 0)
In [78]:
m_dict3['k1']
Out[78]:
In [79]:
m_dict3['k2'] = 2
In [80]:
m_dict3
Out[80]:
In [ ]:
In [81]:
# un-ordered dictionary
m_dict4 = {}
m_dict4['k1'] = 1
m_dict4['k2'] = 2
m_dict4['k3'] = 3
m_dict4['k4'] = 4
m_dict4['k5'] = 5
In [82]:
m_dict4
Out[82]:
In [83]:
for k,v in m_dict4:
print k, v
In [84]:
for k,v in m_dict4.items():
print k, v
In [85]:
m_dict5 = {}
m_dict5['k4'] = 4
m_dict5['k2'] = 2
m_dict5['k5'] = 5
m_dict5['k1'] = 1
m_dict5['k3'] = 3
In [86]:
m_dict4 == m_dict5
Out[86]:
In [87]:
# ordered dict
from collections import OrderedDict
m_dict6 = OrderedDict()
m_dict6['k4'] = 4
m_dict6['k2'] = 2
m_dict6['k5'] = 5
m_dict6['k1'] = 1
m_dict6['k3'] = 3
m_dict7 = OrderedDict()
m_dict7['k1'] = 1
m_dict7['k2'] = 2
m_dict7['k3'] = 3
m_dict7['k4'] = 4
m_dict7['k5'] = 5
In [88]:
m_dict6 == m_dict7 # comparing two ORDERED dict
Out[88]:
In [89]:
m_dict5 == m_dict6 # comparing ORDERED & UN-ORDERED dict
Out[89]:
In [90]:
m_dict5 == m_dict7 # comparing ORDERED & UN-ORDERED dict
Out[90]:
In [91]:
m_dict4 == m_dict5 # comparing two UN-ORDERED dict
Out[91]:
In [92]:
m_dict7.values()
Out[92]:
In [93]:
# namedtuple() assigns names to every element of tuple
In [94]:
m_tuple = (1,2,3,4,5,6,7,8,9)
In [95]:
m_tuple[0]
Out[95]:
In [96]:
m_tuple[5]
Out[96]:
In [97]:
from collections import namedtuple
In [98]:
Dog = namedtuple('DOG', 'age breed name')
In [99]:
m_dog = Dog(2, 'Lab', 'Tommy')
In [100]:
m_dog
Out[100]:
In [101]:
m_dog.age
Out[101]:
In [102]:
m_dog.breed
Out[102]:
In [103]:
m_dog.name
Out[103]:
In [104]:
m_dog[0]
Out[104]:
In [105]:
m_dog[1]
Out[105]:
In [106]:
m_dog[2]
Out[106]:
In [107]:
import datetime
In [108]:
m_time = datetime.time(5,25,1)
In [109]:
print m_time
In [110]:
print datetime.time.max
In [111]:
print datetime.time.min
In [112]:
print datetime.time.resolution
In [113]:
m_today = datetime.date.today()
In [114]:
print m_today
In [115]:
m_today.timetuple()
Out[115]:
In [116]:
m_today.day
Out[116]:
In [3]:
import pdb
In [5]:
x = [1,23,45]
y = 85
z = 20
result = y*z
print result
pdb.set_trace()
result2 = y+z
print result2
In [6]:
## Advanced numbers
In [7]:
hex(12)
Out[7]:
In [9]:
hex(512)
Out[9]:
In [10]:
bin(1234)
Out[10]:
In [11]:
bin(128)
Out[11]:
In [12]:
bin(512)
Out[12]:
In [13]:
2**4
Out[13]:
In [14]:
pow(2,4)
Out[14]:
In [15]:
pow(2,4,3)
Out[15]:
In [16]:
abs(-56)
Out[16]:
In [17]:
round(2.154)
Out[17]:
In [18]:
round(3.845)
Out[18]:
In [19]:
round(3.8451258, 3)
Out[19]:
In [20]:
round(3.21548, 2)
Out[20]:
In [21]:
## Advanced Strings
In [22]:
m_str = 'hello world'
In [23]:
m_str.capitalize()
Out[23]:
In [24]:
m_str.upper()
Out[24]:
In [25]:
m_str
Out[25]:
In [26]:
m_str.lower()
Out[26]:
In [27]:
m_str.count('e')
Out[27]:
In [28]:
m_str.count('l')
Out[28]:
In [29]:
m_str.find('l')
Out[29]:
In [31]:
m_str.center(20, 'z')
Out[31]:
In [32]:
print 'hello\thi'
In [33]:
'hello\thi'.expandtabs()
Out[33]:
In [34]:
m_str2 = 'hello potter217'
In [35]:
m_str2.isalnum()
Out[35]:
In [36]:
m_str2.isalpha()
Out[36]:
In [37]:
m_str.isalnum()
Out[37]:
In [38]:
m_str.istitle()
Out[38]:
In [41]:
m_listComprehension = [x**3 for x in range(10)]
In [42]:
{str(x):x**3 for x in range(10)}
Out[42]:
In [43]:
m_listComprehension
Out[43]:
In [44]:
m_listComprehension.append([12, 85, 'djdbhsdbhb'])
In [45]:
m_listComprehension
Out[45]:
In [46]:
m_listComprehension.extend(['dsbhffbhs', 52.015, 45])
In [47]:
m_listComprehension
Out[47]:
In [48]:
m_listComprehension.index(216)
Out[48]:
In [49]:
m_listComprehension.index(217)
In [50]:
m_listComprehension.insert(2, 'rustomPotter')
In [51]:
m_listComprehension
Out[51]:
In [52]:
m_listComprehension.pop()
Out[52]:
In [53]:
m_listComprehension
Out[53]:
In [54]:
m_listComprehension.pop(2)
Out[54]:
In [57]:
m_listComprehension.remove(1)
In [58]:
m_listComprehension.reverse()
In [59]:
m_listComprehension
Out[59]:
In [60]:
m_listComprehension.sort()
In [61]:
m_listComprehension
Out[61]:
In [1]:
m_str = "Hariom Vinay"
In [5]:
print(m_str.rjust(5))
In [6]:
print(m_str.rjust(100))
In [7]:
m_str.replace('i', '(II)')
Out[7]:
In [8]:
print(' hdhbsh hshdv '.strip())
In [1]:
d = {'cat': 'cute', 'dog': 'furry'}
In [3]:
print(d.get('cat', 'N/A'))
In [4]:
help(set)
In [16]:
animals = {'cat', '1456', 'dog', 'fish', '412', 217}
for idx, animal in enumerate(animals):
print('#%d: %s' % (idx + 1, animal))
In [17]:
for idx, animal in enumerate(animals):
print('#%d: %s' % (idx + 1, animal))
In [18]:
animals.add(45215565)
In [19]:
for idx, animal in enumerate(animals):
print('#%d: %s' % (idx + 1, animal))
In [ ]: