Lists


In [1]:
m_list = [1,2,3]

In [2]:
m_list


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

In [3]:
m_list2 = ['potter', 1.23, 'v', 217]

In [4]:
m_list2


Out[4]:
['potter', 1.23, 'v', 217]

In [5]:
len(m_list2)


Out[5]:
4

In [6]:
m_list3 = ['one', 'two', 'three', 4, 5]

In [7]:
m_list3[0]


Out[7]:
'one'

In [8]:
m_list3[3]


Out[8]:
4

In [9]:
m_list3[1:]


Out[9]:
['two', 'three', 4, 5]

In [10]:
m_list3[:4]


Out[10]:
['one', 'two', 'three', 4]

In [11]:
'hello' + 'world'


Out[11]:
'helloworld'

In [12]:
m_list + m_list2 + m_list3


Out[12]:
[1, 2, 3, 'potter', 1.23, 'v', 217, 'one', 'two', 'three', 4, 5]

In [13]:
m_list3 + ['Vinay']


Out[13]:
['one', 'two', 'three', 4, 5, 'Vinay']

In [14]:
m_list = m_list + m_list2

In [15]:
m_list


Out[15]:
[1, 2, 3, 'potter', 1.23, 'v', 217]

In [17]:
m_list3 * 2


Out[17]:
['one', 'two', 'three', 4, 5, 'one', 'two', 'three', 4, 5]

In [18]:
m_list3 * 3


Out[18]:
['one',
 'two',
 'three',
 4,
 5,
 'one',
 'two',
 'three',
 4,
 5,
 'one',
 'two',
 'three',
 4,
 5]

In [19]:
m_list4 = [1,2,3]

In [20]:
m_list4.append('appendMe')

In [21]:
m_list4


Out[21]:
[1, 2, 3, 'appendMe']

In [22]:
m_list4.append(2.0232)

In [23]:
m_list4


Out[23]:
[1, 2, 3, 'appendMe', 2.0232]

In [26]:
m_list4.pop()


Out[26]:
2.0232

In [27]:
m_list4


Out[27]:
[1, 2, 3, 'appendMe']

In [28]:
m_list4.pop(2)


Out[28]:
3

In [29]:
m_list4


Out[29]:
[1, 2, 'appendMe']

In [30]:
m_list4[1]


Out[30]:
2

In [31]:
m_list4[89]


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-31-f9fe36636b0e> in <module>()
----> 1 m_list4[89]

IndexError: list index out of range

In [32]:
new_list = ['a', 'e', 'x', 'b', 'c']

In [33]:
new_list


Out[33]:
['a', 'e', 'x', 'b', 'c']

In [35]:
new_list.reverse()

In [36]:
new_list


Out[36]:
['c', 'b', 'x', 'e', 'a']

In [38]:
new_list.sort()

In [39]:
new_list


Out[39]:
['a', 'b', 'c', 'e', 'x']

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]:
[[1, 2.3], [4, 5, 6], [7, 8, 9]]

In [45]:
matrix[0]


Out[45]:
[1, 2.3]

In [46]:
matrix[0][0]


Out[46]:
1

In [47]:
matrix[2][2]


Out[47]:
9

In [48]:
matrix[1][2]


Out[48]:
6

In [52]:
# list comprehensions

first_col = [row[0] for row in matrix]

In [53]:
first_col


Out[53]:
[1, 4, 7]

In [ ]:

Dictionaries


In [54]:
m_dict = {'key1':'value', 'key2':217}

In [55]:
m_dict


Out[55]:
{'key1': 'value', 'key2': 217}

In [56]:
m_dict[0]


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-56-44652b751c13> in <module>()
----> 1 m_dict[0]

KeyError: 0

In [57]:
m_dict['key1']


Out[57]:
'value'

In [58]:
m_dict['key2'] = m_dict['key2'] - 17

In [59]:
m_dict


Out[59]:
{'key1': 'value', 'key2': 200}

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]:
{'animal': 'Phoenix'}

In [64]:
d['wizard'] = 'Potter'

In [65]:
d


Out[65]:
{'animal': 'Phoenix', 'wizard': 'Potter'}

In [67]:
d2 = {'k1':{'nestkey':{'subnestkey': 'value'}}}

In [68]:
d2


Out[68]:
{'k1': {'nestkey': {'subnestkey': 'value'}}}

In [72]:
d2['k1']


Out[72]:
{'nestkey': {'subnestkey': 'value'}}

In [73]:
d2['k1']['nestkey']


Out[73]:
{'subnestkey': 'value'}

In [74]:
d2['k1']['nestkey']['subnestkey']


Out[74]:
'value'

In [75]:
d2['k1']['nestkey']['subnestkey'].upper()


Out[75]:
'VALUE'

In [76]:
d3 = {}

In [77]:
d3['k1'] = 1

In [78]:
d3['k2'] = 2

In [79]:
d3['k3'] = 3

In [80]:
d3


Out[80]:
{'k1': 1, 'k2': 2, 'k3': 3}

In [81]:
d3.keys()       # returns a list of all keys in the dict


Out[81]:
['k3', 'k2', 'k1']

In [82]:
d3.values()     # returns a list of all values in the dict


Out[82]:
[3, 2, 1]

In [84]:
d3.items()      # returns a list of tuples(key:value) n the dict


Out[84]:
[('k3', 3), ('k2', 2), ('k1', 1)]

In [85]:
d3['k1'] = 0     # dictionaries are mutable

In [86]:
d3


Out[86]:
{'k1': 0, 'k2': 2, 'k3': 3}

In [ ]:

Tuples


In [2]:
m_tuple = (1, 2, 3)

In [3]:
m_tuple


Out[3]:
(1, 2, 3)

In [4]:
len(m_tuple)


Out[4]:
3

In [5]:
m_tuple2 = ('one', 2)

In [6]:
m_tuple2


Out[6]:
('one', 2)

In [8]:
m_tuple2[0]


Out[8]:
'one'

In [9]:
m_tuple2[-1]


Out[9]:
2

In [13]:
# returns the index of the input if that input exists inside the tuple
m_tuple2.index('one')


Out[13]:
0

In [12]:
m_tuple2.index(2)


Out[12]:
1

In [14]:
# returns the count of the input inside the tuple
m_tuple2.count('one')


Out[14]:
1

In [15]:
m_tuple3 = (1, 1, 1, 2, 3, 2, 1, 3, 2, 1)

In [16]:
m_tuple3.count(1)


Out[16]:
5

In [17]:
m_tuple3.count(2)


Out[17]:
3

In [18]:
m_tuple3.count(3)


Out[18]:
2

In [19]:
# tuples are immutable but lists are mutable

m_tuple3[0] = 123


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-48c7ddc0d819> in <module>()
      1 # tuples are immutable but lists are mutable
      2 
----> 3 m_tuple3[0] = 123

TypeError: 'tuple' object does not support item assignment

In [ ]:

Files


In [23]:
pwd


Out[23]:
u'/media/potter217/Saililashah/Projects/Lumos/Project_Python/py_bootcamp'

In [26]:
m_file = open('test.txt')

In [27]:
m_file


Out[27]:
<open file 'test.txt', mode 'r' at 0x7f8ec8243810>

In [28]:
m_file.read()


Out[28]:
'Hello Rustom Potter'

In [29]:
m_file.read()


Out[29]:
''

In [31]:
m_file.seek(0)

In [32]:
m_file.read()


Out[32]:
'Hello Rustom Potter'

In [33]:
m_file.read()


Out[33]:
'\nNamaste!!'

In [34]:
m_file.read()


Out[34]:
''

In [36]:
m_file.seek(0)

In [37]:
m_file.read()


Out[37]:
'Hello Rustom Potter\nNamaste!!'

In [38]:
m_file.readline()


Out[38]:
''

In [39]:
m_file.seek(0)

In [40]:
m_file.readline()


Out[40]:
'Hello Rustom Potter\n'

In [41]:
m_file.seek(0)

In [42]:
m_file.readlines()


Out[42]:
['Hello Rustom Potter\n', 'Namaste!!']

In [43]:
m_file.seek(1)

In [44]:
m_file.readlines()


Out[44]:
['ello Rustom Potter\n', 'Namaste!!']

In [46]:
%%writefile newtest.txt
firstLine
second line


Writing newtest.txt

In [47]:
m_newtestFile = open('newtest.txt')

In [48]:
m_newtestFile.readlines()


Out[48]:
['firstLine\n', 'second line']

In [49]:
ls


dataStructures.ipynb*  master/  newtest.txt*  objects.ipynb*  test.txt*

In [50]:
for line in open('newtest.txt'):
    print line


firstLine

second line

In [ ]:

Sets & Booleans


In [51]:
m_set = set()

In [52]:
m_set


Out[52]:
set()

In [53]:
m_set.add(1)

In [54]:
m_set


Out[54]:
{1}

In [55]:
m_set.add('123')

In [56]:
m_set


Out[56]:
{1, '123'}

In [58]:
m_set.add(1)

In [59]:
m_set


Out[59]:
{1, '123'}

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]:
[1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 2, 21, 1]

In [64]:
# contains only UNIQUE elements
set(m_listSET)


Out[64]:
{1, 2, 3, 21}

In [65]:
# BOOLEANS

In [66]:
m_bool = True

In [67]:
m_bool


Out[67]:
True

In [68]:
1  > 2


Out[68]:
False

In [69]:
1 > -1


Out[69]:
True

In [70]:
0 > 0


Out[70]:
False

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]:
'Rustom'

In [76]:
2**38


Out[76]:
274877906944

In [ ]: