In [115]:
range(0,9)
Out[115]:
In [116]:
for i in range(0,9):
print(i)
In [117]:
list(range(0,9))
#range is iterator type
In [ ]:
int('1')
In [ ]:
str(1)
In [ ]:
int(a)
#throws error
In [ ]:
list ('abcd')
#iteratorr type
In [ ]:
tuple('kathmandu')
#iterator type can only be passed
In [ ]:
list(('255','255','255','0'))
In [ ]:
tuple([255,255,254,0])
In [ ]:
#tuple and list are linked data structure
In [ ]:
tuple({'abc':123})
In [ ]:
dict(('name','hari'))
In [ ]:
list({'abc':123})
In [ ]:
for item in {'a':1}:
print(item)
In [ ]:
dict([('name','hari')])
In [ ]:
lst=[]
for i in range (0,50):
if i % 5==0:
lst.append(i**2)
In [ ]:
lst
In [ ]:
[i**2 for i in range (0,50) if i % 5 == 0]
# this is list comprehension
# initially for loop then goes to if and if statement the condition is true it comes to the 1st place
# working as sme as the upper loop
In [ ]:
lst =[i**2 if i%2==0 else i for i in range (0,9)]
#everything before for is a single expression
In [ ]:
lst
In [ ]:
i=1
i**2 if i%2==0 else i
# this is list comprehension
In [ ]:
In [ ]:
i=2
i**2 if i%2==0 else i
In [ ]:
{i for i in range(0,19)if i % 2 == 0 or i % 3 == 0 }
#this is set comprehension
In [ ]:
{i : i**2 for i in range(0,19)if i % 2 == 0 or i % 3 == 0 }
#this is dictionary comprehension
In [ ]:
# to read csv file
In [ ]:
fp= open('netdata.txt','r')
In [ ]:
dir (fp)
In [ ]:
fp.read()
In [ ]:
fp.read()
#doesnt show nything because the pointer is atEOF
In [ ]:
fp.seek(0)
#it places the pointer to the given position in this case 0
In [ ]:
In [ ]:
fp.read()
In [ ]:
fp.seek(0)
In [ ]:
fp.readline()
In [ ]:
fp= open('notepad1.txt','r')
In [ ]:
line=fp.readline()
In [ ]:
line
In [ ]:
line = line.strip()
#strips the line feed ie /n
In [ ]:
In [ ]:
line
In [ ]:
line=line.strip(',')
#removes comma at the start
In [ ]:
line
In [ ]:
items= line.split(',')
In [ ]:
items
In [ ]:
import csv
#
In [ ]:
fp.seek(0)
In [ ]:
In [ ]:
header='Sort Order,Common Name,Formal Name,Type,Sub Type,Sovereignty,Capital,ISO 4217 Currency Code,ISO 4217 Currency Name,ITU-T Telephone Code,ISO 3166-1 2 Letter Code,ISO 3166-1 3 Letter Code,ISO 3166-1 Number,IANA Country Code TLD\n'
In [ ]:
headers=header.strip().split(',')
In [ ]:
headers
In [ ]:
reader =csv.DictReader(fp)
In [ ]:
In [ ]:
reader
In [ ]:
dir(reader)
In [ ]:
reader.fieldnames
In [118]:
countries = list(reader)
#because is used bfore it was overwritten
In [ ]:
countries
In [ ]:
In [ ]:
for line in reader:
print(line)
#changed every line to dictionary
In [ ]:
countries=list(reader)
In [ ]:
var= none
for row in countries:
if row['Type']=='Independent State':
var= row
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: