Session 3



In [1]:
range(0, 9)


Out[1]:
range(0, 9)

In [2]:
for i in range(0, 9):
    print(i)


0
1
2
3
4
5
6
7
8

In [4]:
list(range(0, 9))


Out[4]:
[0, 1, 2, 3, 4, 5, 6, 7, 8]

In [5]:
int('1')


Out[5]:
1

In [6]:
str(1)


Out[6]:
'1'

In [7]:
int('a')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-b3c3f4515dd4> in <module>()
----> 1 int('a')

ValueError: invalid literal for int() with base 10: 'a'

In [8]:
list('abcsd')


Out[8]:
['a', 'b', 'c', 's', 'd']

In [9]:
tuple('kathmandu')


Out[9]:
('k', 'a', 't', 'h', 'm', 'a', 'n', 'd', 'u')

In [10]:
list(('255', '255', '255', '0'))


Out[10]:
['255', '255', '255', '0']

In [11]:
tuple([255, 255, 254, 0])


Out[11]:
(255, 255, 254, 0)

In [12]:
tuple({'abc': 123})


Out[12]:
('abc',)

In [13]:
dict(('name', 'hari'))


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-52fe23237bf6> in <module>()
----> 1 dict(('name', 'hari'))

ValueError: dictionary update sequence element #0 has length 4; 2 is required

In [14]:
list({'abc': 123})


Out[14]:
['abc']

In [15]:
for item in {'a': 1}:
    print(item)


a

In [16]:
dict([('name', 'hari')])


Out[16]:
{'name': 'hari'}

In [ ]:


In [19]:
lst = []
for i in range(0, 50):
    if i % 5 == 0:
        lst.append(i ** 2)

In [20]:
lst


Out[20]:
[0, 25, 100, 225, 400, 625, 900, 1225, 1600, 2025]

In [21]:
# list comprehension
[i**2 for i in range(0, 50) if i % 5 == 0]


Out[21]:
[0, 25, 100, 225, 400, 625, 900, 1225, 1600, 2025]

In [23]:
lst = [i**2 if i % 2 == 0 else i for i in range(0, 9)]

In [24]:
lst


Out[24]:
[0, 1, 4, 3, 16, 5, 36, 7, 64]

In [26]:
i = 1
i**2 if i % 2 == 0 else i


Out[26]:
1

In [27]:
i = 2
i**2 if i % 2 == 0 else i


Out[27]:
4

In [28]:
# set comprehension
{i for i in range(0, 19) if i % 2 == 0 or i % 3 == 0}


Out[28]:
{0, 2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18}

In [29]:
# dictionary comprehension
{i:i**2 for i in range(0, 19) if i % 2 == 0 or i % 3 == 0}


Out[29]:
{0: 0,
 2: 4,
 3: 9,
 4: 16,
 6: 36,
 8: 64,
 9: 81,
 10: 100,
 12: 144,
 14: 196,
 15: 225,
 16: 256,
 18: 324}

In [ ]:


In [30]:
fp = open('untitled.txt', 'r')

In [31]:
fp


Out[31]:
<_io.TextIOWrapper name='untitled.txt' mode='r' encoding='UTF-8'>

In [32]:
open('notafile.txt', 'r')


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-32-2b76ba72e0a0> in <module>()
----> 1 open('notafile.txt', 'r')

FileNotFoundError: [Errno 2] No such file or directory: 'notafile.txt'

In [33]:
dir(fp)


Out[33]:
['_CHUNK_SIZE',
 '__class__',
 '__del__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__enter__',
 '__eq__',
 '__exit__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getstate__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__lt__',
 '__ne__',
 '__new__',
 '__next__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '_checkClosed',
 '_checkReadable',
 '_checkSeekable',
 '_checkWritable',
 '_finalizing',
 'buffer',
 'close',
 'closed',
 'detach',
 'encoding',
 'errors',
 'fileno',
 'flush',
 'isatty',
 'line_buffering',
 'mode',
 'name',
 'newlines',
 'read',
 'readable',
 'readline',
 'readlines',
 'seek',
 'seekable',
 'tell',
 'truncate',
 'writable',
 'write',
 'writelines']

In [34]:
fp.read()


Out[34]:
'euname,modified,linked_country,iso3,iso2,grc,isonum,country,imperitive\n,,Georgia,,,ABK,,Abkhazia,De Facto; Currency\nAFGHANISTAN,,,AFG,AF,AFG,004,Afganistan,\n,Added 26th October 2010,Finland,ALA,AX,ALA,248,Åland Islands,Postal\nAlbania,,,ALB,AL,ALB,008,Albania,\nALGERIA,,,DZA,DZ,ALG,012,Algeria,\n,,United States of America,ASM,AS,AMS,016,American Samoa,Geographical\nANDORRA,,,AND,AD,AND,020,Andorra,\nANGOLA,,,AGO,AO,ANG,024,Angola,\n,,United Kingdom,AIA,AI,ANU,660,Anguilla,Geographical; Postal; Currency; Telephone\nANTIGUA AND BARBUDA,,,ATG,AG,ANT,028,Antigua & Barbuda,\nArgentina,,,ARG,AR,ARG,032,Argentina,\nArmenia,,,ARM,AM,ARM,051,Armenia,\nARUBA,,Netherlands,ABW,AW,ARU,533,Aruba,Geographical; Postal; Currency; Telephone\nAustralia,,,AUS,AU,AST,036,Australia,\nAustria,,,AUT,AT,AUS,040,Austria,\nAZERBAIJAN,,,AZE,AZ,AZE,031,Azerbaijan,\nBAHAMAS,,,BHS,BS,BAH,044,Bahamas,\nBAHRAIN,,,BHR,BH,BAR,048,Bahrain,\nBANGLADESH,,,BGD,BD,BAN,050,Bangladesh,\nBarbados,,,BRB,BB,BAB,052,Barbados,\nBELARUS,,,BLR,BY,BEO,112,Belarus,\nBelgium,,,BEL,BE,BEL,056,Belgium,\nBELIZE,,,BLZ,BZ,BEI,084,Belize,\nBenin,,,BEN,BJ,BEN,204,Benin,\n,,United Kingdom,BMU,BM,BER,060,Bermuda,Geographical; Postal; Currency; Telephone\nBHUTAN,,,BTN,BT,BHU,064,Bhutan,\nBOLIVIA,,,BOL,BO,BOL,068,Bolivia,\n,ISO codes added 28th Dec. 2010,Netherlands,BES,BQ,BON,535,Bonaire,Geographical; Postal; Currency; Telephone\nBosnia-Herzegovina,,,BIH,BA,BOS,070,Bosnia-Herzegovina,\nBOTSWANA,,,BWA,BW,BOT,072,Botswana,\nBrazil,,,BRA,BR,BRA,076,Brazil,\n,,"United Kingdom, United States of America",IOT,IO,BIO,086,British Indian Ocean Territory,Geographical; Postal; Telephone\nBritish Virgin Islands,,United Kingdom,VGB,VG,BVI,092,British Virgin Islands,Geographical; Postal; Currency; Telephone\nBRUNEI,,,BRN,BN,BRU,096,Brunei Darussalam,\nBulgaria,,,BGR,BG,BUL,100,Bulgaria,\nBURKINA FASO,,,BFA,BF,BUK,854,Burkina Faso,\nBURUNDI,,,BDI,BI,BUU,108,Burundi,\nCambodia,,,KHM,KH,CAM,116,Cambodia,\nCameroon,,,CMR,CM,CAE,120,Cameroon,\nCanada,,,CAN,CA,CAN,124,Canada,\nCAPE VERDE,,,CPV,CV,CAP,132,Cape Verde Islands,\n,,United Kingdom,CYM,KY,CAY,136,Cayman Islands,Geographical; Postal; Currency; Telephone\n"CENTRAL AFRICAN, REPUBLIC",,,CAF,CF,CEN,140,Central African Republic,\nChad,,,TCD,TD,CHA,148,Chad,\nCHILE,,,CHL,CL,CHI,152,Chile,\nCHINA,,,CHN,CN,CHN,156,China,\n,,Australia,CXR,CX,CHR,162,Christmas Island,Geographical\n,,Australia,CCK,CC,COC,166,Cocos (Keeling) Islands,Geographical\nColombia,,,COL,CO,CLO,170,Colombia,\nCOMOROS,,,COM,KM,COM,174,Comoros,\nCONGO,,,COG,CG,CNG,178,Congo (Brazzaville),\n"CONGO, DEMOCRATIC REPUBLIC OF",,,ZAR,CD,ZAI,180,Congo (Kinshasa),\nCOOK ISLANDS,,New Zealand,COK,CK,COO,184,Cook Islands,Geographical; Postal; Telephone\nCosta Rica,,,CRI,CR,COS,188,Costa Rica,\nCroatia,,,HRV,HR,CRO,191,Croatia,\nCUBA,,,CUB,CU,CUB,192,Cuba,'

In [35]:
fp.read()


Out[35]:
''

In [36]:
fp.seek(0)


Out[36]:
0

In [37]:
fp.read()


Out[37]:
'euname,modified,linked_country,iso3,iso2,grc,isonum,country,imperitive\n,,Georgia,,,ABK,,Abkhazia,De Facto; Currency\nAFGHANISTAN,,,AFG,AF,AFG,004,Afganistan,\n,Added 26th October 2010,Finland,ALA,AX,ALA,248,Åland Islands,Postal\nAlbania,,,ALB,AL,ALB,008,Albania,\nALGERIA,,,DZA,DZ,ALG,012,Algeria,\n,,United States of America,ASM,AS,AMS,016,American Samoa,Geographical\nANDORRA,,,AND,AD,AND,020,Andorra,\nANGOLA,,,AGO,AO,ANG,024,Angola,\n,,United Kingdom,AIA,AI,ANU,660,Anguilla,Geographical; Postal; Currency; Telephone\nANTIGUA AND BARBUDA,,,ATG,AG,ANT,028,Antigua & Barbuda,\nArgentina,,,ARG,AR,ARG,032,Argentina,\nArmenia,,,ARM,AM,ARM,051,Armenia,\nARUBA,,Netherlands,ABW,AW,ARU,533,Aruba,Geographical; Postal; Currency; Telephone\nAustralia,,,AUS,AU,AST,036,Australia,\nAustria,,,AUT,AT,AUS,040,Austria,\nAZERBAIJAN,,,AZE,AZ,AZE,031,Azerbaijan,\nBAHAMAS,,,BHS,BS,BAH,044,Bahamas,\nBAHRAIN,,,BHR,BH,BAR,048,Bahrain,\nBANGLADESH,,,BGD,BD,BAN,050,Bangladesh,\nBarbados,,,BRB,BB,BAB,052,Barbados,\nBELARUS,,,BLR,BY,BEO,112,Belarus,\nBelgium,,,BEL,BE,BEL,056,Belgium,\nBELIZE,,,BLZ,BZ,BEI,084,Belize,\nBenin,,,BEN,BJ,BEN,204,Benin,\n,,United Kingdom,BMU,BM,BER,060,Bermuda,Geographical; Postal; Currency; Telephone\nBHUTAN,,,BTN,BT,BHU,064,Bhutan,\nBOLIVIA,,,BOL,BO,BOL,068,Bolivia,\n,ISO codes added 28th Dec. 2010,Netherlands,BES,BQ,BON,535,Bonaire,Geographical; Postal; Currency; Telephone\nBosnia-Herzegovina,,,BIH,BA,BOS,070,Bosnia-Herzegovina,\nBOTSWANA,,,BWA,BW,BOT,072,Botswana,\nBrazil,,,BRA,BR,BRA,076,Brazil,\n,,"United Kingdom, United States of America",IOT,IO,BIO,086,British Indian Ocean Territory,Geographical; Postal; Telephone\nBritish Virgin Islands,,United Kingdom,VGB,VG,BVI,092,British Virgin Islands,Geographical; Postal; Currency; Telephone\nBRUNEI,,,BRN,BN,BRU,096,Brunei Darussalam,\nBulgaria,,,BGR,BG,BUL,100,Bulgaria,\nBURKINA FASO,,,BFA,BF,BUK,854,Burkina Faso,\nBURUNDI,,,BDI,BI,BUU,108,Burundi,\nCambodia,,,KHM,KH,CAM,116,Cambodia,\nCameroon,,,CMR,CM,CAE,120,Cameroon,\nCanada,,,CAN,CA,CAN,124,Canada,\nCAPE VERDE,,,CPV,CV,CAP,132,Cape Verde Islands,\n,,United Kingdom,CYM,KY,CAY,136,Cayman Islands,Geographical; Postal; Currency; Telephone\n"CENTRAL AFRICAN, REPUBLIC",,,CAF,CF,CEN,140,Central African Republic,\nChad,,,TCD,TD,CHA,148,Chad,\nCHILE,,,CHL,CL,CHI,152,Chile,\nCHINA,,,CHN,CN,CHN,156,China,\n,,Australia,CXR,CX,CHR,162,Christmas Island,Geographical\n,,Australia,CCK,CC,COC,166,Cocos (Keeling) Islands,Geographical\nColombia,,,COL,CO,CLO,170,Colombia,\nCOMOROS,,,COM,KM,COM,174,Comoros,\nCONGO,,,COG,CG,CNG,178,Congo (Brazzaville),\n"CONGO, DEMOCRATIC REPUBLIC OF",,,ZAR,CD,ZAI,180,Congo (Kinshasa),\nCOOK ISLANDS,,New Zealand,COK,CK,COO,184,Cook Islands,Geographical; Postal; Telephone\nCosta Rica,,,CRI,CR,COS,188,Costa Rica,\nCroatia,,,HRV,HR,CRO,191,Croatia,\nCUBA,,,CUB,CU,CUB,192,Cuba,'

In [38]:
fp.seek(0)


Out[38]:
0

In [39]:
fp.readline()


Out[39]:
'euname,modified,linked_country,iso3,iso2,grc,isonum,country,imperitive\n'

In [40]:
line = fp.readline()

In [41]:
line


Out[41]:
',,Georgia,,,ABK,,Abkhazia,De Facto; Currency\n'

In [42]:
line = line.strip()

In [43]:
line


Out[43]:
',,Georgia,,,ABK,,Abkhazia,De Facto; Currency'

In [44]:
line = line.strip(',')

In [45]:
line


Out[45]:
'Georgia,,,ABK,,Abkhazia,De Facto; Currency'

In [46]:
items = line.split(',')

In [47]:
items


Out[47]:
['Georgia', '', '', 'ABK', '', 'Abkhazia', 'De Facto; Currency']

In [48]:
import csv

In [67]:
fp.seek(0)


Out[67]:
0

In [50]:
headers = 'euname,modified,linked_country,iso3,iso2,grc,isonum,country,imperitive\n'

In [51]:
headers = headers.strip().split(',')

In [52]:
headers


Out[52]:
['euname',
 'modified',
 'linked_country',
 'iso3',
 'iso2',
 'grc',
 'isonum',
 'country',
 'imperitive']

In [68]:
reader = csv.DictReader(fp)

In [69]:
reader


Out[69]:
<csv.DictReader at 0x103ee8ac8>

In [64]:
dir(reader)


Out[64]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__next__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_fieldnames',
 'dialect',
 'fieldnames',
 'line_num',
 'reader',
 'restkey',
 'restval']

In [70]:
reader.fieldnames


Out[70]:
['euname',
 'modified',
 'linked_country',
 'iso3',
 'iso2',
 'grc',
 'isonum',
 'country',
 'imperitive']

In [71]:
countries = list(reader)

In [72]:
countries


Out[72]:
[{'country': 'Abkhazia',
  'euname': '',
  'grc': 'ABK',
  'imperitive': 'De Facto; Currency',
  'iso2': '',
  'iso3': '',
  'isonum': '',
  'linked_country': 'Georgia',
  'modified': ''},
 {'country': 'Afganistan',
  'euname': 'AFGHANISTAN',
  'grc': 'AFG',
  'imperitive': '',
  'iso2': 'AF',
  'iso3': 'AFG',
  'isonum': '004',
  'linked_country': '',
  'modified': ''},
 {'country': 'Åland Islands',
  'euname': '',
  'grc': 'ALA',
  'imperitive': 'Postal',
  'iso2': 'AX',
  'iso3': 'ALA',
  'isonum': '248',
  'linked_country': 'Finland',
  'modified': 'Added 26th October 2010'},
 {'country': 'Albania',
  'euname': 'Albania',
  'grc': 'ALB',
  'imperitive': '',
  'iso2': 'AL',
  'iso3': 'ALB',
  'isonum': '008',
  'linked_country': '',
  'modified': ''},
 {'country': 'Algeria',
  'euname': 'ALGERIA',
  'grc': 'ALG',
  'imperitive': '',
  'iso2': 'DZ',
  'iso3': 'DZA',
  'isonum': '012',
  'linked_country': '',
  'modified': ''},
 {'country': 'American Samoa',
  'euname': '',
  'grc': 'AMS',
  'imperitive': 'Geographical',
  'iso2': 'AS',
  'iso3': 'ASM',
  'isonum': '016',
  'linked_country': 'United States of America',
  'modified': ''},
 {'country': 'Andorra',
  'euname': 'ANDORRA',
  'grc': 'AND',
  'imperitive': '',
  'iso2': 'AD',
  'iso3': 'AND',
  'isonum': '020',
  'linked_country': '',
  'modified': ''},
 {'country': 'Angola',
  'euname': 'ANGOLA',
  'grc': 'ANG',
  'imperitive': '',
  'iso2': 'AO',
  'iso3': 'AGO',
  'isonum': '024',
  'linked_country': '',
  'modified': ''},
 {'country': 'Anguilla',
  'euname': '',
  'grc': 'ANU',
  'imperitive': 'Geographical; Postal; Currency; Telephone',
  'iso2': 'AI',
  'iso3': 'AIA',
  'isonum': '660',
  'linked_country': 'United Kingdom',
  'modified': ''},
 {'country': 'Antigua & Barbuda',
  'euname': 'ANTIGUA AND BARBUDA',
  'grc': 'ANT',
  'imperitive': '',
  'iso2': 'AG',
  'iso3': 'ATG',
  'isonum': '028',
  'linked_country': '',
  'modified': ''},
 {'country': 'Argentina',
  'euname': 'Argentina',
  'grc': 'ARG',
  'imperitive': '',
  'iso2': 'AR',
  'iso3': 'ARG',
  'isonum': '032',
  'linked_country': '',
  'modified': ''},
 {'country': 'Armenia',
  'euname': 'Armenia',
  'grc': 'ARM',
  'imperitive': '',
  'iso2': 'AM',
  'iso3': 'ARM',
  'isonum': '051',
  'linked_country': '',
  'modified': ''},
 {'country': 'Aruba',
  'euname': 'ARUBA',
  'grc': 'ARU',
  'imperitive': 'Geographical; Postal; Currency; Telephone',
  'iso2': 'AW',
  'iso3': 'ABW',
  'isonum': '533',
  'linked_country': 'Netherlands',
  'modified': ''},
 {'country': 'Australia',
  'euname': 'Australia',
  'grc': 'AST',
  'imperitive': '',
  'iso2': 'AU',
  'iso3': 'AUS',
  'isonum': '036',
  'linked_country': '',
  'modified': ''},
 {'country': 'Austria',
  'euname': 'Austria',
  'grc': 'AUS',
  'imperitive': '',
  'iso2': 'AT',
  'iso3': 'AUT',
  'isonum': '040',
  'linked_country': '',
  'modified': ''},
 {'country': 'Azerbaijan',
  'euname': 'AZERBAIJAN',
  'grc': 'AZE',
  'imperitive': '',
  'iso2': 'AZ',
  'iso3': 'AZE',
  'isonum': '031',
  'linked_country': '',
  'modified': ''},
 {'country': 'Bahamas',
  'euname': 'BAHAMAS',
  'grc': 'BAH',
  'imperitive': '',
  'iso2': 'BS',
  'iso3': 'BHS',
  'isonum': '044',
  'linked_country': '',
  'modified': ''},
 {'country': 'Bahrain',
  'euname': 'BAHRAIN',
  'grc': 'BAR',
  'imperitive': '',
  'iso2': 'BH',
  'iso3': 'BHR',
  'isonum': '048',
  'linked_country': '',
  'modified': ''},
 {'country': 'Bangladesh',
  'euname': 'BANGLADESH',
  'grc': 'BAN',
  'imperitive': '',
  'iso2': 'BD',
  'iso3': 'BGD',
  'isonum': '050',
  'linked_country': '',
  'modified': ''},
 {'country': 'Barbados',
  'euname': 'Barbados',
  'grc': 'BAB',
  'imperitive': '',
  'iso2': 'BB',
  'iso3': 'BRB',
  'isonum': '052',
  'linked_country': '',
  'modified': ''},
 {'country': 'Belarus',
  'euname': 'BELARUS',
  'grc': 'BEO',
  'imperitive': '',
  'iso2': 'BY',
  'iso3': 'BLR',
  'isonum': '112',
  'linked_country': '',
  'modified': ''},
 {'country': 'Belgium',
  'euname': 'Belgium',
  'grc': 'BEL',
  'imperitive': '',
  'iso2': 'BE',
  'iso3': 'BEL',
  'isonum': '056',
  'linked_country': '',
  'modified': ''},
 {'country': 'Belize',
  'euname': 'BELIZE',
  'grc': 'BEI',
  'imperitive': '',
  'iso2': 'BZ',
  'iso3': 'BLZ',
  'isonum': '084',
  'linked_country': '',
  'modified': ''},
 {'country': 'Benin',
  'euname': 'Benin',
  'grc': 'BEN',
  'imperitive': '',
  'iso2': 'BJ',
  'iso3': 'BEN',
  'isonum': '204',
  'linked_country': '',
  'modified': ''},
 {'country': 'Bermuda',
  'euname': '',
  'grc': 'BER',
  'imperitive': 'Geographical; Postal; Currency; Telephone',
  'iso2': 'BM',
  'iso3': 'BMU',
  'isonum': '060',
  'linked_country': 'United Kingdom',
  'modified': ''},
 {'country': 'Bhutan',
  'euname': 'BHUTAN',
  'grc': 'BHU',
  'imperitive': '',
  'iso2': 'BT',
  'iso3': 'BTN',
  'isonum': '064',
  'linked_country': '',
  'modified': ''},
 {'country': 'Bolivia',
  'euname': 'BOLIVIA',
  'grc': 'BOL',
  'imperitive': '',
  'iso2': 'BO',
  'iso3': 'BOL',
  'isonum': '068',
  'linked_country': '',
  'modified': ''},
 {'country': 'Bonaire',
  'euname': '',
  'grc': 'BON',
  'imperitive': 'Geographical; Postal; Currency; Telephone',
  'iso2': 'BQ',
  'iso3': 'BES',
  'isonum': '535',
  'linked_country': 'Netherlands',
  'modified': 'ISO codes added 28th Dec. 2010'},
 {'country': 'Bosnia-Herzegovina',
  'euname': 'Bosnia-Herzegovina',
  'grc': 'BOS',
  'imperitive': '',
  'iso2': 'BA',
  'iso3': 'BIH',
  'isonum': '070',
  'linked_country': '',
  'modified': ''},
 {'country': 'Botswana',
  'euname': 'BOTSWANA',
  'grc': 'BOT',
  'imperitive': '',
  'iso2': 'BW',
  'iso3': 'BWA',
  'isonum': '072',
  'linked_country': '',
  'modified': ''},
 {'country': 'Brazil',
  'euname': 'Brazil',
  'grc': 'BRA',
  'imperitive': '',
  'iso2': 'BR',
  'iso3': 'BRA',
  'isonum': '076',
  'linked_country': '',
  'modified': ''},
 {'country': 'British Indian Ocean Territory',
  'euname': '',
  'grc': 'BIO',
  'imperitive': 'Geographical; Postal; Telephone',
  'iso2': 'IO',
  'iso3': 'IOT',
  'isonum': '086',
  'linked_country': 'United Kingdom, United States of America',
  'modified': ''},
 {'country': 'British Virgin Islands',
  'euname': 'British Virgin Islands',
  'grc': 'BVI',
  'imperitive': 'Geographical; Postal; Currency; Telephone',
  'iso2': 'VG',
  'iso3': 'VGB',
  'isonum': '092',
  'linked_country': 'United Kingdom',
  'modified': ''},
 {'country': 'Brunei Darussalam',
  'euname': 'BRUNEI',
  'grc': 'BRU',
  'imperitive': '',
  'iso2': 'BN',
  'iso3': 'BRN',
  'isonum': '096',
  'linked_country': '',
  'modified': ''},
 {'country': 'Bulgaria',
  'euname': 'Bulgaria',
  'grc': 'BUL',
  'imperitive': '',
  'iso2': 'BG',
  'iso3': 'BGR',
  'isonum': '100',
  'linked_country': '',
  'modified': ''},
 {'country': 'Burkina Faso',
  'euname': 'BURKINA FASO',
  'grc': 'BUK',
  'imperitive': '',
  'iso2': 'BF',
  'iso3': 'BFA',
  'isonum': '854',
  'linked_country': '',
  'modified': ''},
 {'country': 'Burundi',
  'euname': 'BURUNDI',
  'grc': 'BUU',
  'imperitive': '',
  'iso2': 'BI',
  'iso3': 'BDI',
  'isonum': '108',
  'linked_country': '',
  'modified': ''},
 {'country': 'Cambodia',
  'euname': 'Cambodia',
  'grc': 'CAM',
  'imperitive': '',
  'iso2': 'KH',
  'iso3': 'KHM',
  'isonum': '116',
  'linked_country': '',
  'modified': ''},
 {'country': 'Cameroon',
  'euname': 'Cameroon',
  'grc': 'CAE',
  'imperitive': '',
  'iso2': 'CM',
  'iso3': 'CMR',
  'isonum': '120',
  'linked_country': '',
  'modified': ''},
 {'country': 'Canada',
  'euname': 'Canada',
  'grc': 'CAN',
  'imperitive': '',
  'iso2': 'CA',
  'iso3': 'CAN',
  'isonum': '124',
  'linked_country': '',
  'modified': ''},
 {'country': 'Cape Verde Islands',
  'euname': 'CAPE VERDE',
  'grc': 'CAP',
  'imperitive': '',
  'iso2': 'CV',
  'iso3': 'CPV',
  'isonum': '132',
  'linked_country': '',
  'modified': ''},
 {'country': 'Cayman Islands',
  'euname': '',
  'grc': 'CAY',
  'imperitive': 'Geographical; Postal; Currency; Telephone',
  'iso2': 'KY',
  'iso3': 'CYM',
  'isonum': '136',
  'linked_country': 'United Kingdom',
  'modified': ''},
 {'country': 'Central African Republic',
  'euname': 'CENTRAL AFRICAN, REPUBLIC',
  'grc': 'CEN',
  'imperitive': '',
  'iso2': 'CF',
  'iso3': 'CAF',
  'isonum': '140',
  'linked_country': '',
  'modified': ''},
 {'country': 'Chad',
  'euname': 'Chad',
  'grc': 'CHA',
  'imperitive': '',
  'iso2': 'TD',
  'iso3': 'TCD',
  'isonum': '148',
  'linked_country': '',
  'modified': ''},
 {'country': 'Chile',
  'euname': 'CHILE',
  'grc': 'CHI',
  'imperitive': '',
  'iso2': 'CL',
  'iso3': 'CHL',
  'isonum': '152',
  'linked_country': '',
  'modified': ''},
 {'country': 'China',
  'euname': 'CHINA',
  'grc': 'CHN',
  'imperitive': '',
  'iso2': 'CN',
  'iso3': 'CHN',
  'isonum': '156',
  'linked_country': '',
  'modified': ''},
 {'country': 'Christmas Island',
  'euname': '',
  'grc': 'CHR',
  'imperitive': 'Geographical',
  'iso2': 'CX',
  'iso3': 'CXR',
  'isonum': '162',
  'linked_country': 'Australia',
  'modified': ''},
 {'country': 'Cocos (Keeling) Islands',
  'euname': '',
  'grc': 'COC',
  'imperitive': 'Geographical',
  'iso2': 'CC',
  'iso3': 'CCK',
  'isonum': '166',
  'linked_country': 'Australia',
  'modified': ''},
 {'country': 'Colombia',
  'euname': 'Colombia',
  'grc': 'CLO',
  'imperitive': '',
  'iso2': 'CO',
  'iso3': 'COL',
  'isonum': '170',
  'linked_country': '',
  'modified': ''},
 {'country': 'Comoros',
  'euname': 'COMOROS',
  'grc': 'COM',
  'imperitive': '',
  'iso2': 'KM',
  'iso3': 'COM',
  'isonum': '174',
  'linked_country': '',
  'modified': ''},
 {'country': 'Congo (Brazzaville)',
  'euname': 'CONGO',
  'grc': 'CNG',
  'imperitive': '',
  'iso2': 'CG',
  'iso3': 'COG',
  'isonum': '178',
  'linked_country': '',
  'modified': ''},
 {'country': 'Congo (Kinshasa)',
  'euname': 'CONGO, DEMOCRATIC REPUBLIC OF',
  'grc': 'ZAI',
  'imperitive': '',
  'iso2': 'CD',
  'iso3': 'ZAR',
  'isonum': '180',
  'linked_country': '',
  'modified': ''},
 {'country': 'Cook Islands',
  'euname': 'COOK ISLANDS',
  'grc': 'COO',
  'imperitive': 'Geographical; Postal; Telephone',
  'iso2': 'CK',
  'iso3': 'COK',
  'isonum': '184',
  'linked_country': 'New Zealand',
  'modified': ''},
 {'country': 'Costa Rica',
  'euname': 'Costa Rica',
  'grc': 'COS',
  'imperitive': '',
  'iso2': 'CR',
  'iso3': 'CRI',
  'isonum': '188',
  'linked_country': '',
  'modified': ''},
 {'country': 'Croatia',
  'euname': 'Croatia',
  'grc': 'CRO',
  'imperitive': '',
  'iso2': 'HR',
  'iso3': 'HRV',
  'isonum': '191',
  'linked_country': '',
  'modified': ''},
 {'country': 'Cuba',
  'euname': 'CUBA',
  'grc': 'CUB',
  'imperitive': '',
  'iso2': 'CU',
  'iso3': 'CUB',
  'isonum': '192',
  'linked_country': '',
  'modified': ''}]

In [73]:
afg = None
for row in countries:
    if row['iso3'] == 'AFG':
        afg = row

In [74]:
afg


Out[74]:
{'country': 'Afganistan',
 'euname': 'AFGHANISTAN',
 'grc': 'AFG',
 'imperitive': '',
 'iso2': 'AF',
 'iso3': 'AFG',
 'isonum': '004',
 'linked_country': '',
 'modified': ''}

final output should be

[('AFG', 'Afganistan'), ('NPL', 'Nepal') ...]

In [76]:
csvfile = open('untitled.txt', 'r')

In [77]:
csvfile


Out[77]:
<_io.TextIOWrapper name='untitled.txt' mode='r' encoding='UTF-8'>

In [78]:
csvreader = csv.DictReader(csvfile)

In [79]:
country_list = []
for country in csvreader:
    if country['iso3'] and country['country']:
        country_tup = (country['iso3'], country['country'])
        country_list.append(country_tup)

In [80]:
country_list


Out[80]:
[('AFG', 'Afganistan'),
 ('ALA', 'Åland Islands'),
 ('ALB', 'Albania'),
 ('DZA', 'Algeria'),
 ('ASM', 'American Samoa'),
 ('AND', 'Andorra'),
 ('AGO', 'Angola'),
 ('AIA', 'Anguilla'),
 ('ATG', 'Antigua & Barbuda'),
 ('ARG', 'Argentina'),
 ('ARM', 'Armenia'),
 ('ABW', 'Aruba'),
 ('AUS', 'Australia'),
 ('AUT', 'Austria'),
 ('AZE', 'Azerbaijan'),
 ('BHS', 'Bahamas'),
 ('BHR', 'Bahrain'),
 ('BGD', 'Bangladesh'),
 ('BRB', 'Barbados'),
 ('BLR', 'Belarus'),
 ('BEL', 'Belgium'),
 ('BLZ', 'Belize'),
 ('BEN', 'Benin'),
 ('BMU', 'Bermuda'),
 ('BTN', 'Bhutan'),
 ('BOL', 'Bolivia'),
 ('BES', 'Bonaire'),
 ('BIH', 'Bosnia-Herzegovina'),
 ('BWA', 'Botswana'),
 ('BRA', 'Brazil'),
 ('IOT', 'British Indian Ocean Territory'),
 ('VGB', 'British Virgin Islands'),
 ('BRN', 'Brunei Darussalam'),
 ('BGR', 'Bulgaria'),
 ('BFA', 'Burkina Faso'),
 ('BDI', 'Burundi'),
 ('KHM', 'Cambodia'),
 ('CMR', 'Cameroon'),
 ('CAN', 'Canada'),
 ('CPV', 'Cape Verde Islands'),
 ('CYM', 'Cayman Islands'),
 ('CAF', 'Central African Republic'),
 ('TCD', 'Chad'),
 ('CHL', 'Chile'),
 ('CHN', 'China'),
 ('CXR', 'Christmas Island'),
 ('CCK', 'Cocos (Keeling) Islands'),
 ('COL', 'Colombia'),
 ('COM', 'Comoros'),
 ('COG', 'Congo (Brazzaville)'),
 ('ZAR', 'Congo (Kinshasa)'),
 ('COK', 'Cook Islands'),
 ('CRI', 'Costa Rica'),
 ('HRV', 'Croatia'),
 ('CUB', 'Cuba')]

In [81]:
fp.close()

In [82]:
csvfile.close()

In [ ]:


In [93]:
with open('newfile.csv', 'w') as csvfile:
    fieldnames  = ['FirstName', 'LastName', 'Age']
    csvwriter = csv.DictWriter(csvfile, fieldnames=fieldnames)
    csvwriter.writeheader()
    csvwriter.writerow({'FirstName': 'J', 'LastName': 'k', 'Age': 20})
    csvwriter.writerow({'LastName': 'M', 'FirstName': 'n', 'Age': 24})
    csvwriter.writerows([
            {'LastName': 'A', 'FirstName': '', 'Age': 24},
            {'LastName': 'B', 'FirstName': 'L', 'Age': 25},
            {'LastName': 'B', 'FirstName': 'H', 'Age': 27}
    ])

In [ ]:


In [ ]:

Classes and Objects


                           Dogs
                             |
                    -------------------
                    |                 |
                  Domestic           Wild
         ------------------
         |                |
       Semi Wild         Feral ...   
             Transport
               |
-----------------------------------
|                |              |

Land Water Air | -------------- ........... | | Buses Train ...


In [98]:
class Animal:
    age = None

In [109]:
class Dog(Animal):
    pass

In [100]:
german_shepherd = Dog()

In [101]:
german_shepherd


Out[101]:
<__main__.Dog at 0x104296da0>

In [102]:
german_shepherd = Dog()

In [103]:
german_shepherd


Out[103]:
<__main__.Dog at 0x104296e48>

In [106]:
german_shepherd.age = 22

In [107]:
german_shepherd.age


Out[107]:
22

In [110]:
class Dog(Animal):
    age = 18

In [111]:
german_shepherd = Dog()

In [113]:
german_shepherd.age


Out[113]:
18

In [114]:
german_shepherd.age = 21

In [116]:
german_shepherd.age


Out[116]:
21

In [117]:
tibetan_husky = Dog()

In [118]:
tibetan_husky.age


Out[118]:
18

In [124]:
class Animal:
    age = None
    
    def is_alive(self):
        return True
    
    def sound(self):
        return 'Cry'

In [125]:
class Cat(Animal):
    
    def sound(self):
        return 'Meow'

In [126]:
cat = Cat()

In [127]:
cat.is_alive()


Out[127]:
True

In [128]:
cat.age

In [129]:
cat.sound()


Out[129]:
'Meow'

CaseStudy: Library Management



In [164]:
class Patron:
    firstname = None
    lastname = None
    address = None
    books = []
    
    def __repr__(self):
        return '{} {}'.format(self.firstname, self.lastname)
    
    def has_book(self, bookid):
        return bookid in self.books

In [165]:
class Student(Patron):
    faculty = None
    year = None
    
    def __init__(self, firstname, lastname, faculty, year):
        self.firstname = firstname
        self.lastname = lastname
        self.faculty = faculty
        self.year = year

In [166]:
class Staff(Patron):
    department = None

In [167]:
class Book:
    bookid = None
    isbn = None
    name = None
    
    def __init__(self, isbn, name):
        self.isbn = isbn
        self.name = name
        self.bookid = 'MYLIB' + ':' + self.isbn
    
    def __repr__(self):
        return self.bookid

In [168]:
class Teacher(Patron):
    faculty = None
    
    def __init__(self, firstname, lastname, faculty):
        self.firstname = firstname
        self.lastname = lastname
        self.faculty = faculty

In [ ]:


In [169]:
english = Book(isbn='abc-8236482374', name='Learning Engligh in 21 days.')

In [170]:
math = Book(isbn='mth-283482347', name='Quick Calculus.')

In [171]:
nepali = Book(isbn='npl-293492834', name='बकमपबसिकमपवबसिकमप.')

In [172]:
class Shelve:
    books = []
    lended_books = []
    
    def is_book_available(self, bookid):
        return bookid in self.books
    
    def is_book_lendable(self, bookid):
        return bookid not in self.lended_books

In [173]:
my_library = Shelve()

In [152]:
# Shelve.is_book_available(my_library, 'abc-123')


Out[152]:
False

In [174]:
my_library.is_book_available('abc-123')


Out[174]:
False

In [175]:
my_library.books = [english, math, nepali]

In [176]:
my_library.books


Out[176]:
[MYLIB:abc-8236482374, MYLIB:mth-283482347, MYLIB:npl-293492834]

In [ ]:
ram

In [177]:
ram = Student('Ram', 'Thapa', 'Biology', '2nd')

In [178]:
ram


Out[178]:
Ram Thapa

In [179]:
ram.has_book('ajsdajsd')


Out[179]:
False

In [ ]:


In [ ]: