In [1]:
m_list = [1,2,3]
In [2]:
m_list
Out[2]:
In [3]:
m_list2 = ['potter', 1.23, 'v', 217]
In [4]:
m_list2
Out[4]:
In [5]:
len(m_list2)
Out[5]:
In [6]:
m_list3 = ['one', 'two', 'three', 4, 5]
In [7]:
m_list3[0]
Out[7]:
In [8]:
m_list3[3]
Out[8]:
In [9]:
m_list3[1:]
Out[9]:
In [10]:
m_list3[:4]
Out[10]:
In [11]:
'hello' + 'world'
Out[11]:
In [12]:
m_list + m_list2 + m_list3
Out[12]:
In [13]:
m_list3 + ['Vinay']
Out[13]:
In [14]:
m_list = m_list + m_list2
In [15]:
m_list
Out[15]:
In [17]:
m_list3 * 2
Out[17]:
In [18]:
m_list3 * 3
Out[18]:
In [19]:
m_list4 = [1,2,3]
In [20]:
m_list4.append('appendMe')
In [21]:
m_list4
Out[21]:
In [22]:
m_list4.append(2.0232)
In [23]:
m_list4
Out[23]:
In [26]:
m_list4.pop()
Out[26]:
In [27]:
m_list4
Out[27]:
In [28]:
m_list4.pop(2)
Out[28]:
In [29]:
m_list4
Out[29]:
In [30]:
m_list4[1]
Out[30]:
In [31]:
m_list4[89]
In [32]:
new_list = ['a', 'e', 'x', 'b', 'c']
In [33]:
new_list
Out[33]:
In [35]:
new_list.reverse()
In [36]:
new_list
Out[36]:
In [38]:
new_list.sort()
In [39]:
new_list
Out[39]:
In [40]:
l_1 = [1,2.3]
In [41]:
l_2 = [4,5,6]
In [42]:
l_3 = [7,8,9]
In [43]:
matrix = [l_1, l_2, l_3]
In [44]:
matrix
Out[44]:
In [45]:
matrix[0]
Out[45]:
In [46]:
matrix[0][0]
Out[46]:
In [47]:
matrix[2][2]
Out[47]:
In [48]:
matrix[1][2]
Out[48]:
In [52]:
# list comprehensions
first_col = [row[0] for row in matrix]
In [53]:
first_col
Out[53]:
In [ ]:
In [54]:
m_dict = {'key1':'value', 'key2':217}
In [55]:
m_dict
Out[55]:
In [56]:
m_dict[0]
In [57]:
m_dict['key1']
Out[57]:
In [58]:
m_dict['key2'] = m_dict['key2'] - 17
In [59]:
m_dict
Out[59]:
In [60]:
# empty dictionary
d = {}
In [61]:
d
Out[61]:
In [62]:
# adding key:value pair to dictionary
d['animal'] = 'Phoenix'
In [63]:
d
Out[63]:
In [64]:
d['wizard'] = 'Potter'
In [65]:
d
Out[65]:
In [67]:
d2 = {'k1':{'nestkey':{'subnestkey': 'value'}}}
In [68]:
d2
Out[68]:
In [72]:
d2['k1']
Out[72]:
In [73]:
d2['k1']['nestkey']
Out[73]:
In [74]:
d2['k1']['nestkey']['subnestkey']
Out[74]:
In [75]:
d2['k1']['nestkey']['subnestkey'].upper()
Out[75]:
In [76]:
d3 = {}
In [77]:
d3['k1'] = 1
In [78]:
d3['k2'] = 2
In [79]:
d3['k3'] = 3
In [80]:
d3
Out[80]:
In [81]:
d3.keys() # returns a list of all keys in the dict
Out[81]:
In [82]:
d3.values() # returns a list of all values in the dict
Out[82]:
In [84]:
d3.items() # returns a list of tuples(key:value) n the dict
Out[84]:
In [85]:
d3['k1'] = 0 # dictionaries are mutable
In [86]:
d3
Out[86]:
In [ ]:
In [2]:
m_tuple = (1, 2, 3)
In [3]:
m_tuple
Out[3]:
In [4]:
len(m_tuple)
Out[4]:
In [5]:
m_tuple2 = ('one', 2)
In [6]:
m_tuple2
Out[6]:
In [8]:
m_tuple2[0]
Out[8]:
In [9]:
m_tuple2[-1]
Out[9]:
In [13]:
# returns the index of the input if that input exists inside the tuple
m_tuple2.index('one')
Out[13]:
In [12]:
m_tuple2.index(2)
Out[12]:
In [14]:
# returns the count of the input inside the tuple
m_tuple2.count('one')
Out[14]:
In [15]:
m_tuple3 = (1, 1, 1, 2, 3, 2, 1, 3, 2, 1)
In [16]:
m_tuple3.count(1)
Out[16]:
In [17]:
m_tuple3.count(2)
Out[17]:
In [18]:
m_tuple3.count(3)
Out[18]:
In [19]:
# tuples are immutable but lists are mutable
m_tuple3[0] = 123
In [ ]:
In [23]:
pwd
Out[23]:
In [26]:
m_file = open('test.txt')
In [27]:
m_file
Out[27]:
In [28]:
m_file.read()
Out[28]:
In [29]:
m_file.read()
Out[29]:
In [31]:
m_file.seek(0)
In [32]:
m_file.read()
Out[32]:
In [33]:
m_file.read()
Out[33]:
In [34]:
m_file.read()
Out[34]:
In [36]:
m_file.seek(0)
In [37]:
m_file.read()
Out[37]:
In [38]:
m_file.readline()
Out[38]:
In [39]:
m_file.seek(0)
In [40]:
m_file.readline()
Out[40]:
In [41]:
m_file.seek(0)
In [42]:
m_file.readlines()
Out[42]:
In [43]:
m_file.seek(1)
In [44]:
m_file.readlines()
Out[44]:
In [46]:
%%writefile newtest.txt
firstLine
second line
In [47]:
m_newtestFile = open('newtest.txt')
In [48]:
m_newtestFile.readlines()
Out[48]:
In [49]:
ls
In [50]:
for line in open('newtest.txt'):
print line
In [ ]:
In [51]:
m_set = set()
In [52]:
m_set
Out[52]:
In [53]:
m_set.add(1)
In [54]:
m_set
Out[54]:
In [55]:
m_set.add('123')
In [56]:
m_set
Out[56]:
In [58]:
m_set.add(1)
In [59]:
m_set
Out[59]:
In [60]:
m_listSET = [1,1,1,1,1,1,1, 2,2,2,2, 3,2,21,1]
In [61]:
m_listSET
Out[61]:
In [64]:
# contains only UNIQUE elements
set(m_listSET)
Out[64]:
In [65]:
# BOOLEANS
In [66]:
m_bool = True
In [67]:
m_bool
Out[67]:
In [68]:
1 > 2
Out[68]:
In [69]:
1 > -1
Out[69]:
In [70]:
0 > 0
Out[70]:
In [71]:
m_boolNone = None
In [72]:
# a Placeholder
m_none = None
In [73]:
m_none
In [74]:
m_none = 'Rustom'
In [75]:
m_none
Out[75]:
In [76]:
2**38
Out[76]:
In [ ]: