Python 101: Learning by doing. 
Looping through a dictionary (aka map):

Python 2.7.5
>>> map = {50+i:i for i in range(10)}
>>> map
{50: 0, 51: 1, 52: 2, 53: 3, 54: 4, 55: 5, 56: 6, 57: 7, 58: 8, 59: 9}
>>> for key, value in map.iteritems():
... print key,'-',value
...
50 - 0
51 - 1
52 - 2
53 - 3
54 - 4
55 - 5
56 - 6
57 - 7
58 - 8
59 - 9
>>>

Python 3.3.2
>>> map = {50+i:i for i in range(10)}
>>> map
{50: 0, 51: 1, 52: 2, 53: 3, 54: 4, 55: 5, 56: 6, 57: 7, 58: 8, 59: 9}
>>> for key, value in map.items():
... print(key,' - ',value)
...
50 - 0
51 - 1
52 - 2
53 - 3
54 - 4
55 - 5
56 - 6
57 - 7
58 - 8
59 - 9
>>>


Fetching url data:

Python 2
[aesteban@localhost corpemployees-filtered]$ python
Python 2.7.5 (default, Apr 10 2015, 08:09:05)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import urllib
>>> urllib.urlopen('https://graph.facebook.com/?id=http://www.barney.com').read()
'{"id":"http:\\/\\/www.barney.com","shares":792}'

Python 3
[aesteban@localhost corpemployees-filtered]$ python3
Python 3.3.2 (default, Dec 4 2014, 12:49:00)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import urllib.request
>>>
>>> urllib.request.urlopen('https://graph.facebook.com/?id=http://www.barney.com').read()
b'{"id":"http:\\/\\/www.barney.com","shares":792}'


Comments
Comments are not available for this entry.
2024 By Angel Cool