In [1]:
my_dict = {'key' : 1, 'key2' : 2}

In [2]:
my_dict


Out[2]:
{'key': 1, 'key2': 2}

In [3]:
my_dict['key']


Out[3]:
1

In [5]:
my_dict.fromkeys()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-f410f13265eb> in <module>()
----> 1 my_dict.fromkeys()

TypeError: fromkeys expected at least 1 arguments, got 0

In [6]:
my_dict.viewkeys()


Out[6]:
dict_keys(['key2', 'key'])

In [9]:
my_dict.viewitems()


Out[9]:
dict_items([('key2', 2), ('key', 1)])

In [10]:
tup = (1, 3, 6)

In [12]:
tup.count(1)


Out[12]:
1

In [13]:
tup.count("test")


Out[13]:
0

In [14]:
tup.index(1)


Out[14]:
0

In [16]:
tup.index(3)


Out[16]:
1

In [17]:
len(tup)


Out[17]:
3

In [20]:
for (item in tup) {
    print(item)
}


  File "<ipython-input-20-dc6466b0c9d2>", line 1
    for (item in tup) {
                      ^
SyntaxError: invalid syntax

In [21]:
tup


Out[21]:
(1, 3, 6)

In [22]:
tup[0]


Out[22]:
1

In [23]:
x = set()

In [24]:
x


Out[24]:
set()

In [ ]:


In [25]:
x.add(1, 2)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-e4b9e3584b3b> in <module>()
----> 1 x.add(1, 2)

TypeError: add() takes exactly one argument (2 given)

In [26]:
x.add(1)

In [27]:
x


Out[27]:
{1}

In [28]:
x[0]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-1ae75c28907a> in <module>()
----> 1 x[0]

TypeError: 'set' object does not support indexing

In [29]:
x.update(1)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-d52ae0c230d7> in <module>()
----> 1 x.update(1)

TypeError: 'int' object is not iterable

In [30]:
x[1]


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-30-98e225784849> in <module>()
----> 1 x[1]

TypeError: 'set' object does not support indexing

In [31]:
li = [1, 1, 1, 2, 3, 3, 3, 4]

In [32]:
li_set = set(li)

In [33]:
li_set


Out[33]:
{1, 2, 3, 4}

In [34]:
my_string = "hello this is a string"

In [35]:
first = my_string[0]

In [36]:
first


Out[36]:
'h'

In [37]:
last = my_string[1:-1]

In [38]:
last


Out[38]:
'ello this is a strin'

In [39]:
last = my_string[-2:-1]

In [40]:
last


Out[40]:
'n'

In [41]:
last = my_string[-1:]

In [42]:
last


Out[42]:
'g'

In [43]:
first


Out[43]:
'h'

In [44]:
middle = my_string[1:-1]

In [45]:
middle


Out[45]:
'ello this is a strin'

In [46]:
short_string = "ab"

In [47]:
short_string[0]


Out[47]:
'a'

In [48]:
short_string[-1:]


Out[48]:
'b'

In [49]:
short_string[1:-1]


Out[49]:
''

In [50]:
single_letter = "a"

In [52]:
single_letter[-1:]


Out[52]:
'a'

In [53]:
single_letter[1:-1]


Out[53]:
''

In [54]:
my_string


Out[54]:
'hello this is a string'

In [55]:
my_string = "hello"

In [70]:
def string_splosion(str): 
    return_string = ""
    for i in range(len(str)):
        print(str[:i])
        return_string += str[:i]
        
    print(return_string + str)

In [71]:
string_splosion("Code")


C
Co
Cod
CCoCodCode

In [110]:
# http://codingbat.com/prob/p145834
def last2(str):
    pattern = str[-2:]
    test_string = str[:-1]
    print("Pattern: %s" % pattern)
    counter = 0
    for i in range(len(test_string)):
        substr = test_string[i:i+2]
        print("Loop: {x}, substring: {y}".format(x=i, y=substr))
        if substr == pattern:
            counter+=1
            print("matched")
    return counter

In [84]:
last2("Hello")


He
el

In [112]:
'code'[0]


Out[112]:
'c'

In [77]:
"code"[3:100]


Out[77]:
'e'

In [111]:
last2("xxxx")


Pattern: xx
Loop: 0, substring: xx
matched
Loop: 1, substring: xx
matched
Loop: 2, substring: x
Out[111]:
2

In [5]:
# dictionary comprehensions (not incredibly common)

sq = {'val {0}'.format(x):x**2 for x in range(10)}
print sq


{'val 8': 64, 'val 9': 81, 'val 0': 0, 'val 1': 1, 'val 2': 4, 'val 3': 9, 'val 4': 16, 'val 5': 25, 'val 6': 36, 'val 7': 49}

In [11]:
{k:v**2 for k,v in zip(['a', 'b'], range(2)) }


Out[11]:
{'a': 0, 'b': 1}

In [14]:
dict(zip(['a', 'b'], [0, 1]))


Out[14]:
{'a': 0, 'b': 1}

In [ ]: