Python3 has representation of String using unicode.Unicode is by default in python.
Python has dynamic typing e.g. print(3+"mangesh") will not work need to convert 3 to str(3)
In [1]:
print(3+"mangesh")
In [2]:
print(str(3)+"mangesh")
In [3]:
record={"name":"mangeesh","price":34,"country":"Brazil"}
Using format
In [4]:
print_statement="{} is my name,{} is the price,{} is the country"
In [5]:
print_statement.format(record["name"],record["price"],record["country"])
Out[5]:
CSV_Reading
In [21]:
import csv
#precision 2
with open("mpg.csv") as csvfile:
mpg=list(csv.DictReader(csvfile))
print(len(mpg))
print(mpg[0].keys())
print(mpg[0])
In [26]:
sum([float(k["displacement"]) for k in mpg] )/len(mpg)
Out[26]:
Sum over cylinders
In [32]:
cylinders=set([k["cylinders"] for k in mpg])
Out[32]:
In [37]:
avgMpgPerCylinderDict={}
for c in cylinders:
value=0
number=0
for k in mpg:
if k["cylinders"]==c:
value+=float(k["mpg"])
number+=1
avgCylinderDict[c]=value/number
avgCylinderDict
Out[37]:
In [41]:
modelyear=set([d["model_year"] for d in mpg])
modelyear
Out[41]:
In [53]:
avgMPGperModelYear=[]
for y in modelyear:
value=0
number=0
for k in mpg:
if k["model_year"]==y:
value+=float(k["mpg"])
number+=1
avgMPGperModelYear.append((y,value/number))
avgMPGperModelYear.sort(key=lambda x:x[1])
avgMPGperModelYear
Out[53]: